Advertisement

Register object behavior with pointer to instance function

Started by February 28, 2013 04:49 PM
3 comments, last by WitchLord 11 years, 6 months ago

Is it possible to pass a pointer to an instanced class method (&creationsClass.createScriptObject) when registering an object behavior?

I need specific behavior for certain script objects when they're created. I'm trying to avoid using static variables and classes as much as possible which doesn't allow me to simply create a static variable to store the object type for the garbage collector and many other things I need to instantiate a script object.

I kind of want to do something like this (but obviously it doesn't work):


#define asInstanceMethod(c,m) asSMethodPtr<sizeof(c)>::Convert((&c.m))

void Texture::registerAS(asIScriptEngine* engine, ScriptObjectCreationCache& creationCache) {
    int r;
    static const char* const strComponent = "Texture";
    r = engine->RegisterObjectType(strComponent, sizeof(Texture), asOBJ_REF | asOBJ_GC); assert(r >= 0);
    r = engine->RegisterObjectBehaviour(strComponent, asBEHAVE_FACTORY, "Texture@ f()", asInstanceMethod(creationCache, TSFactory), asCALL_CDECL); assert( r >= 0 );
    r = engine->RegisterObjectBehaviour(strComponent, asBEHAVE_FACTORY, "Texture@ f(const string &in)", asInstanceMethod(creationCache, TSFactoryArg), asCALL_CDECL); assert( r >= 0 );
    
    registerMembers<Texture>(engine, strComponent);
}

I would greatly appreciate any additional info.

edit: I left a pretty crappy post earlier because I had to quickly go somewhere.. I fixed it up now ;)

You mean something like this:

 
class MySingleton
{
// Class method to be called from script as if a global function
void MyGlobalFunc(int arg1, int arg2);
};
 
MySingleton single;
 
// Registering the singleton's method as if a global function
r = engine->RegisterGlobalFunction("void MyGlobalFunc(int, int)", asMETHOD(MySingleton, MyGlobalFunc), asCALL_THISCALL_ASGLOBAL, &single); assert( r >= 0 );

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement

Sort of... I ended up hacking it in by adding an extra parameter to the registerObjectBehavior function so now I can do this:


void SpriteScripted::registerAS(asIScriptEngine* engine, ScriptObjectCreationCache& creationCache) {
    int r;
    static const char* const strComponent = "Sprite";
    r = engine->RegisterObjectType(strComponent, sizeof(SpriteScripted), asOBJ_REF | asOBJ_GC); assert(r >= 0);
    r = engine->RegisterObjectBehaviour(strComponent, asBEHAVE_FACTORY, "Sprite@ f()", asMETHOD(ScriptObjectCreationCache, SSFactory), asCALL_THISCALL_ASGLOBAL, &creationCache); assert( r >= 0 );
    ....
}

it seems to work but I still have yet to run the angel script unit tests.

Yes, that should work. I didn't think about the factory functions when I added the support for asCALL_THISCALL_ASGLOBAL. I'll have this added for 2.27.0.

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 support for asCALL_THISCALL_ASGLOBAL in behaviours and also the string factory has been added in revision 1646.

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