zheng.guan Posted November 4, 2014 Share Posted November 4, 2014 I want to setcallback to a WidgetLabel in a user defined class , but the error that can not find function shows . I have a look at the widget demo "sprite_02", i think waht i do is the same as it. So, what should i do to solve this ? Thanks Link to comment
christian.wolf2 Posted November 4, 2014 Share Posted November 4, 2014 HI, are you using the game framework? If so, for that you have to modify the callback a little bit. Here is the code I used for all types of widget functions: Game _game = curLevel.getGame(); //curLevel is a reference to my actual level string prefix; if (_game != NULL) prefix = _game.getLogicNamespaceName(); else log.message("No Game defined!"); WidgetButton newButton = new WidgetButton(ui,"Simple Button"); newButton.setCallback(GUI_CLICKED,prefix + "::YourCustomClass::RedirectYourFunction",this); Be careful that you can only have access to public function. I always used redirect-function to get access to private functions. For the above exsample you have to use: RedirectYourFunction(YourCustomClass yourclass){yourclass.YourFunction;}; YourFunction() { // do whatever you want } One last hint: If you are using the game framework, keep in mind to free the callback-function before destroying the class. Simply use "NULL" as callback-function name to avoid crashing unigine. 1 Link to comment
zheng.guan Posted November 5, 2014 Author Share Posted November 5, 2014 HI, are you using the game framework? If so, for that you have to modify the callback a little bit. Here is the code I used for all types of widget functions: Game _game = curLevel.getGame(); //curLevel is a reference to my actual level string prefix; if (_game != NULL) prefix = _game.getLogicNamespaceName(); else log.message("No Game defined!"); WidgetButton newButton = new WidgetButton(ui,"Simple Button"); newButton.setCallback(GUI_CLICKED,prefix + "::YourCustomClass::RedirectYourFunction",this); Be careful that you can only have access to public function. I always used redirect-function to get access to private functions. For the above exsample you have to use: RedirectYourFunction(YourCustomClass yourclass){yourclass.YourFunction;}; YourFunction() { // do whatever you want } One last hint: If you are using the game framework, keep in mind to free the callback-function before destroying the class. Simply use "NULL" as callback-function name to avoid crashing unigine. thanks, it worked ! :D Link to comment
zheng.guan Posted November 6, 2014 Author Share Posted November 6, 2014 thanks, it worked ! :D Sorry, it crashed ! I havn't understand that "use NULL as callback-function name" . Can you show a example with code ? Thank very much ! Link to comment
christian.wolf2 Posted November 6, 2014 Share Posted November 6, 2014 Can you show a example with code ? Sure. The problem is, that a callback-function still exist after deleting a button e.g. and you don't get a hint about existing "ghost"-callbacks. So I used the following code, to scan all childs of an widget and delete the callback-function. void DeleteChildCallbacks(Widget parentWidget) { if (parentWidget.getType() == WIDGET_BUTTON) parentWidget.setCallback(GUI_CLICKED,NULL); forloop(int i = 0; parentWidget.getNumChilds();1) { Widget childWidget = parentWidget.getChild(i); //log.message("Widget %s have %d childs, that will be deleted",childWidget.getTypeName(),childWidget.getNumChilds()); if (childWidget.getNumChilds() > 0) DeleteChildCallbacks(parentWidget.getChild(i)); else if (childWidget.getType() == WIDGET_BUTTON) childWidget.setCallback(GUI_CLICKED,NULL); //that's the interesting line for you } } Best regards, Christian Link to comment
Recommended Posts