• Advertisement
Sign in to follow this  

Any sample for callbacks ?

This topic is 1560 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,

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
 
 
 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 !!!

Share this post


Link to post
Share on other sites

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 ?

Share this post


Link to post
Share on other sites

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

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

Share this post


Link to post
Share on other sites

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

Share this post


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

  • Advertisement