🎉 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!

Singletons

Started by
5 comments, last by WitchLord 19 years, 4 months ago
Hey guys, Ive spent the past few days converting my scripting engine over from Lua/LuaBind to AS. So far the process has been relatively painless with everything compiling perfectly and majority of code needing relatively little modification. Overall im extremely pleased with AS. Is it currently possible to use singleton classes within AS or is this functionality still on the horizon? Normally in c++ the call would be, SomeClass::getSingleton().someFunction(); Its then exposed using, RegisterObjectType("SomeClass", sizeof(SomeClass), 0); RegisterObjectMethod("SomeClass", "void someFunction( void ), asMETHOD(SomeClass,SomeFunction), asCALL_THISCALL); This compiles fine however when the script is executed it results in a memory conflict and crashes (when it tries to store the data passed into a class variable). I could just create wrapper functions that expose themselves and in turn use the normal call, however im trying to avoid doing this. Any help is greatly appreciated, Thanks
Advertisement
Yes, it is possible to use singletons in AngelScript. Here's an example that ought to work:

C++ code:

// Register the type with a size of zero to prevent the script from declaring new variables of the typeengine->RegisterObjectType("Singleton", 0, 0); // Register the methods as normalengine->RegisterObjectMethod("Singleton", "void SomeFunction()", asMETHOD(Singleton,SomeFunction), asCALL_THISCALL);// Register the singletonSingleton obj;engine->RegisterGlobalProperty("Singleton obj", &obj);


AngelScript:

void Func(){  // Use the registered singleton  obj.SomeFunction();}


I wrote this from memory so the code might not compile right away. If you have any doubts let me know. And if something isn't working that you think should be working, don't hesitate to tell me because it is possible that it is a bug (though I hope not).

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

Strangely enough I was heading down the same path with that solution already as I was going to register global instances of each singleton anyway.
Im fairly sure im doing something wrong with the way ive done it, but each time I register the global property I hit a compile error:
error C2664: 'asIScriptEngine::RegisterGlobalProperty' : cannot convert parameter 2 from 'ScriptEngine' to 'void *'

I've tried passing different forms of the same thing but they all end up with this error. I can remember having a similar problem in the past, but I only woke up about 10 mins ago and cant think of it atm.
If I remember and solve it i'll post here, otherwise any help is appreciated.
Thanks again
Make sure you are using
engine->RegisterGlobalProperty("ScriptEngine obj", &obj);

and not
engine->RegisterGlobalProperty("ScriptEngine obj", obj);

(note the &)
The exact code im using is:
ScriptEngine ScriptEngineObj = ScriptEngine::getSingleton();
engine->RegisterGlobalProperty("ScriptEngine _script", &ScriptEngineObj);

so other than the fact that im setting the Obj to the singleton (which would be needed anyway), the code should work as far as I can see?
Solved the problem, I finally sat down and gave the problem more than 5 min attention. Simply put, a typo earlier resulted in the singletons I was registering to be out of their namespace. To make things worse I wasnt interpretting the errors the compiler was giving accurately so I didnt pick up on this untill now, but its all fixed now.
The singletons are working perfectly,
Thanks for all your help. AS is a great scripting interface.
Also for the record, I just finished running some speed tests and besides the obvious and blatant increase in speed during compile time over Lua/Luabind the scripts are executing about 15% faster aswell. Great Job!
That's great to hear. Thanks for sharing it with us :D

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