Any sample for callbacks ?

Started by
4 comments, last by WitchLord 10 years, 6 months ago

Hi,

Do you have any sample explaning callbacks and funcdefs?

Because I tried and still have crashes :-(

I try to register a callback with

r = engine->RegisterFuncdef("void OnCompleteHandler()");

and then a property with

r = engine->RegisterObjectProperty("Test", "OnCompleteHandler @onComplete", asOFFSET(Test, onCompleteHandler));

In my Test class I have a property declared this way :

asIScriptFunction *onCompleteHandler;

In my script I have a function

void MyCompleteHandler()
{
Log("Done");
};
but when this line is executed
@myTest.onComplete = @MyCompleteHandler; //<- crash when i try to set my property with my function handle.
I always crash in asCScriptEngine::CallObjectMethod because it tries to execute a release method on a object that doesn't exists.
Any help appreciated.
Thanks in advance
Advertisement

Did you set onCompleteHandler = null in the constructor for your Test class?

If the pointer has any other value than null, i.e. 0, then the handle assignment will attempt to call Release on the pointer before updating it with the new handle.

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

Thx a lot ! You were right.

Now even if everybody knows my dirty secret ( I'm a bad programmer who forget to initialize variables ;-) ) I'm happy because I got callbacks with a generic (ref handle) userdata argument.

It works great !!!

Hi again,

I tried to set my onCompleteHandler pointing to a class method but it doesn't work (error : invalid opration on method)

in the script i tried : @test.onCompleteHandler= @myObject.myMethod; (where myObject is a class instance)

any idea ?

Sorry , it works now, I just had to cast it like in the example

@test.onCompleteHandler= OnCompleteHandler(myObject.myMethod);

This is because for class methods you need to instanciate a delegate object to hold the object pointer together with the method pointer. The delegate object will then be treated as if it was a global function pointer and do the necessary translation to the class method when invoked.

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