Jump to content
  • Advertisement
Sign in to follow this  
saejox

(void*) as Parameter and @this handle

This topic is 2242 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

Hi,

I would like to use function such as


r = engine->RegisterObjectMethod("EventManager", "int RegisterSomeCallback(?&in ,const string &in)", asMETHODPR(EventManager, RegisterSomeCallback, (void *,const string &), int), asCALL_THISCALL);assert( r >= 0 );


Angelscript Code

class Npc
{
int callbackid;
Npc()
{
// pass object pointer to engine with function name
callbackid = EM.RegisterSomeCallback(@this, "CallBackFunction");
}

void CallBackFunction()
{
print("Success!");
}

~Npc()
{
EM.UnregisterCallback(callbackid);
}
}


passing @this as a pointer is not possible as i figured. or am i doing it wrong?
also is ?&in correct way to register void* parameter?

thank you. Edited by saejox

Share this post


Link to post
Share on other sites
Advertisement
Another related question smile.png

is it possible to pass an object function's pointer to another Angelscript function or to C++ ? Edited by saejox

Share this post


Link to post
Share on other sites
You can pass @this to a ?&in parameter. AngelScript will then pass a pointer to the handle as the argument.

Note that the ?&in parameter in AngelScript is translated to 2 distinct parameters in the registered function, the first is the void* that receives the pointer to the value, and the second is an int that receives the typeId of the value.

Manual: The variable parameter type

Your function RegisterSomeCallback must then be implemented as:


int EventManager::RegisterSomeCallback(void *ptr, int typeId, const string &methodName)
{
// Make sure it is a handle that is received
if( typeId & asTYPEID_OBJHANDLE )
{
// Get the address of the object
void *obj = *reinterpret_cast<void**>(ptr);

// Get the object type
asIObjectType *type = engine->GetObjectTypeById(typeId);

// Get the method
asIScriptFunction *func = type->GetMethodByName(methodName.c_str());
if( func )
{
// Keep a reference to the object for the callback
engine->AddRefScriptObject(obj, type);

...
}
}
}



No, it is currently not possible to take the pointer of a class method in AngelScript. In a future version I'll implement something similar to delegates to support this.

Share this post


Link to post
Share on other sites
i thought typeid was optional and engine registers only handle if it doesn't exist. silly me.

i did it like this (might be useful for someone in the future)

int EventManager::RegisterSomeCallback(void *ptr, int typeId, const string &methodName)
{
if( typeId & asTYPEID_OBJHANDLE )
{
// cast it to asIScriptObject
asIScriptObject*obj = *static_cast<asIScriptObject**>(ptr); // static_cast works just fine


// Get the object type
asIObjectType *type = obj->GetObjectType(); // faster than engine->GetObjectTypeById lookup i think

// Get the method
asIScriptFunction *func = type->GetMethodByName(methodName.c_str());
if( func )
{
obj->AddRef() // dont need engine here neither
}

}
}


Thank you. It works beautifully! Edited by saejox

Share this post


Link to post
Share on other sites
That works too. Though you may want to add a check to make sure the handle is really to a script object, so the application doesn't crash in case the script calls the function with a handle to a registered object instead.


// Make sure the value is a handle to a script object
if( (typeId & asTYPEID_OBJHANDLE) && (typeId & asTYPEID_SCRIPTOBJECT) )
{
...
}

Share this post


Link to post
Share on other sites

That works too. Though you may want to add a check to make sure the handle is really to a script object, so the application doesn't crash in case the script calls the function with a handle to a registered object instead.


// Make sure the value is a handle to a script object
if( (typeId & asTYPEID_OBJHANDLE) && (typeId & asTYPEID_SCRIPTOBJECT) )
{
...
}



thank you.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!