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
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
ah ok
sorry, what do you mean by this?
cheers
Quote: without informing the singleton pointer,
sorry, what do you mean by this?
cheers
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
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
It's really just a matter of preference.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement