🎉 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

Started by
8 comments, last by WitchLord 17 years, 2 months ago
Is it possible to expose singleton methods to angle script for calling from a script? If so, how? Cheers
Advertisement
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:

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
Sure, that's easy. First, write a wrapper. Something like

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

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

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:

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

The word you want is probably 'specify'.
Is one method better than another?
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