Jump to content
  • Advertisement
Sign in to follow this  
DevilWithin

Is there a bug with function callbacks?

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

Hey,

 

My use case is really simple, I have a button object, which holds a asIScriptFunction pointer with its callback in a script, called when the button is clicked. If the button is clicked and the function is NULL, it does nothing, if it is a valid function, it calls it so the script can react to the event.

 

I did everything like the manual but I get a compilation error. First, I registered my funcdef:

 

asx.get()->RegisterFuncdef("void UICallbackFunction()");

 

Then, I register my UI connect function to bind buttons to script functions: 

 

asx.get()->RegisterObjectMethod("UICanvas", "void connect(const string& in, UICallbackFunction @cb)", asMETHOD(UICanvasScriptInterface, connectClick), asCALL_THISCALL);

 

The connectClick() method has the following signature: void connectClick(const String& name, asIScriptFunction* callback)

 

And, in the script, I am doing this: ui.connect("myButton", onMyButtonClicked); , where onMyButtonClicked is a void onMyButtonClicked() function. It all seems pretty much like the manual, except im registering the callback through a method instead of a global function.. right?

 

What is wrong? My compilation error is as follows:

ERR: No matching signatures to 'UICanvas::connect(const string, onMyButtonClicked)'

INFO: Candidates are:

INFO: void UICanvas::connect(const string&in, UICallbackFunction@)

 

The funcdef indeed has the same signature as the target callback function..

 

EDIT: I actually tried to make it all like the manual: http://www.angelcode.com/angelscript/sdk/docs/manual/doc_callbacks.html . Same problem :(

 

My regards,

Artur

Edited by Grimshaw

Share this post


Link to post
Share on other sites
Advertisement

I am sorry. I completely forgot I was working inside a script class. It works with global functions, but not with methods.

 

class MyScriptClass

{

    void myFunction()

    {

         print("This is the callback function");

    }

 

    void onInit()

    {

          SetCallback(myFunction); // How can I do this?

    }

}

 

Is there a way I can pass the in-class void function to the application side? How would the registration work in the c++ side?

Share this post


Link to post
Share on other sites

gjl is correct. This is what delegates is for.

 

I'll need to improve the compiler message though, since it should indicate the problem was that you were trying to get a class method as a function pointer, which is invalid.

Share this post


Link to post
Share on other sites

Thanks guys. Indeed, that seems to have done the trick. But do I really have to make the explicit cast? I'd be happier with writing this.myFunction instead, why the cast here and not in the global function? I feel tempted to use a string instead to lookup the methods, even though it might be a little bit slower.

Share this post


Link to post
Share on other sites

The delegates are objects so they need to instantiated. This is different from global functions in which case you're taking the address of the function itself.

Share this post


Link to post
Share on other sites

"The delegates are objects"

Yea I learned this one the hard way last week. Be warned that a delegate will not == another delegate that points to the same function and class unless it actually is the same delegate object. They also maintain a ref to the object.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!