🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
Exposing singleton methods
Is it possible to expose singleton methods to angle script for calling from a script? If so, how?
Cheers
I'm not sure I understand what you mean, I'm assuming you want to register a class where the user can not create it, therefore you can guarantee you only have one instance of the class. If so then you'd do it like this:
Then you would register your methods like normal, but no constructor/destructor. Then you can either return a handle of the class from a function, or register it as a global property.
RegisterObjectType("name", 0, asOBJ_CLASS);
Then you would register your methods like normal, but no constructor/destructor. Then you can either return a handle of the class from a function, or register it as a global property.
RegisterGlobalProperty("name classObj", &classObj);
Sorry I thought I was clear. Let me try again.
Suppose that I have a singleton class Log. Within my engine I am doing
Log::getInstance().log("lets rock!");
Now I want the ability to log to the same file from within my running script. So I assume I need to somehow expose this to anglescript. I just wasn't sure if this could be done, or how to do it.
From the script, I just want to be able to do
...
log("foo");
...
Possible?
Cheers
Suppose that I have a singleton class Log. Within my engine I am doing
Log::getInstance().log("lets rock!");
Now I want the ability to log to the same file from within my running script. So I assume I need to somehow expose this to anglescript. I just wasn't sure if this could be done, or how to do it.
From the script, I just want to be able to do
...
log("foo");
...
Possible?
Cheers
Sure, that's easy. First, write a wrapper. Something like
Then just bind that to angelscript. See, simple! :)
void log_wrapper(const std::string& str){ Log::get_instance()->log(str);}
Then just bind that to angelscript. See, simple! :)
Deyja is correct.
If you want the script to call the singleton's methods without informing the singleton pointer, then you must wrap the methods in global functions.
I have thought about adding support for registering object methods directly as global functions, by specifying which object to work on, but if I ever do that it will not be for quite some time.
Regards,
Andreas
If you want the script to call the singleton's methods without informing the singleton pointer, then you must wrap the methods in global functions.
I have thought about adding support for registering object methods directly as global functions, by specifying which object to work on, but if I ever do that it will not be for quite some time.
Regards,
Andreas
If you don't want to use wrappers, you could do what davekrusu said: Register the singleton as a global property and then call the methods directly on it. In the script it would then look something like this:
That's what I mean by the script informing the singleton pointer, i.e. it explicitly tells the VM what object to call the method on.
Regards,
Andreas
Log.write("foo");
That's what I mean by the script informing the singleton pointer, i.e. it explicitly tells the VM what object to call the method on.
Regards,
Andreas
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement