• Advertisement
Sign in to follow this  

Register object behavior with pointer to instance function

This topic is 1699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ;)

Edited by izackp

Share this post


Link to post
Share on other sites
Advertisement

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 );

Share this post


Link to post
Share on other sites

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.

Edited by izackp

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

The support for asCALL_THISCALL_ASGLOBAL in behaviours and also the string factory has been added in revision 1646.

 

Regards,

Andreas

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement