Sign in to follow this  

(void*) as Parameter and @this handle

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

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

Angelscript Code
[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);
}
}
[/CODE]

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
Another related question [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

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.

[url="http://www.angelcode.com/angelscript/sdk/docs/manual/doc_adv_var_type.html"]Manual: The variable parameter type [/url]

Your function RegisterSomeCallback must then be implemented as:

[code]
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);

...
}
}
}
[/code]


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)
[CODE]
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
}

}
}
[/CODE]

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.

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

Share this post


Link to post
Share on other sites
[quote name='Andreas Jonsson' timestamp='1336073403' post='4937188']
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.

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

thank you.

Share this post


Link to post
Share on other sites

This topic is 2048 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this