🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Is there a bug with function callbacks?

Started by
6 comments, last by BlackMoons 10 years, 2 months ago

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

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?

I am not an expert, but I guess that's what delegates are for (see http://www.angelcode.com/angelscript/sdk/docs/manual/doc_datatypes_funcptr.html for details). In your case, if I read well, simply writing the following should probably work:

SetCallback(UICallbackFunction(this.myFunction));

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.

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

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.

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.

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

Sounds good, thanks!! :)

"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.

Lead Coder/Game Designer for Brutal Nature: http://BrutalNature.com

This topic is closed to new replies.

Advertisement