Calling class method registered as GlobalFunction ?

Started by
2 comments, last by WitchLord 17 years, 5 months ago
Hi all, Is there any way to register a function considered as global function in the scripts but using a (non static) method of a class (for example a derived asScriptEngine) ? In other words, is it possible to give the "this" address to a calling function. To be a bit clearer :), here's an example : registering a simple Message() function which require properties owned by the script engine, for example a header string given by the application should look something like: class MyScriptEngine : public asScriptEngine { .. blabla.. void Init() { ... RegisterGlobalFunction( "void Message( const string& in msg )", asMETHODPR( MyScriptEngine, Message, ( const string& ) const, void ), asCALL_CDECL ); } void Message( const string& msg ) const { // display m_msgHeader + msg } void SetMessageHeader( const string& msgHeader ) { m_msgHeader = msgHeader; } private: string m_msgHeader; }; I knwo there's a problem with the code above (the calling convention), but is there a way to give the instance of MyScriptEngine (ie this) required to run this function ? The only way I found until now is to declare (ducplicate) a static (singleton) method (or static global function) which have to now how to get a pointer on the MyScriptEngine instance, then call the method : static void MyScriptEngine::_Message( const string& msg ) { MyScriptEngine* pEngine = GetCurrentScriptEngine(); pEngine->Message( msg ); } then register this _Message() function using CDECL instead of Message() on. The problem comes mainly when you have thousands :) methods to redirect like this. In addition, I would like this Message() method being virtual for MyScriptEngine overriding... The idea behind this problem would maybe being able to "say" to a registered function to give the pointer of the asScriptEngine itself ? (some another calling convention type such CALL_ENGINE or CALL_THISENGINE) ? What do you think about it (I hope you see what I mean:)) ? Regards, Lbas
Lbas
Advertisement
Currently there is no way of doing what you want. But it is on the to-do list, at least as something to consider.

Instead of writing a thousand wrappers for you methods, perhaps you can write a couple of templates that automatically does the wrapping for you? I'm not sure how it would be done, but there is so much that can be done with templates that I find it difficult to think that this wouldn't be possible.

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

In fact, you're right, I'll try to investigate in templated function to simplify wrapping purposes.

But, there's not only wrapping issues with this model, but also to have to keep track on a global instance of the required class, which is not the best way to think in a OO model...

Thanks for your quick reply,
Best regards,
Lbas
Lbas
I may implement support for this in the future, mostly due to the easier management of the object pointer. I just raised the priority for this feature because of this, but it is still quite far down on the to-do list so for now you're better off finding a solution outside AngelScript.

Perhaps you can simply register the object with AngelScript and have the scripts call the class methods directly.

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

This topic is closed to new replies.

Advertisement