Menu Buttons?
Monkey Forums/Monkey Programming/Menu Buttons?| 
 | ||
| I want to be able to make buttons for a menu, and to feed in a function for the button to execute at create time. In Blitzmax I would use a function pointer. How is it best done in Monkey? | 
| 
 | ||
| Use your mouseX() and mouseY() with the button position. If(mouseX() and mouseY() is inside(button xy) and TouchDown(0)) a button click Endif | 
| 
 | ||
| Right but let's say we have button objects stored in a list and created at runtime. At runtime, how do we attach functions to those buttons? In Max you can pass a function pointer, eg "createbutton(x, y, myfunction)" What is the equivalent in Monkey? I know we don't have function pointers and that there are workarounds but I'm wondering what the best method would be in this case. | 
| 
 | ||
| 
Interface Callback
  Method Run:Void()
End
Class SampleFunction Implements Callback
  Method Run:Void()
    Print "Done"
  End
End
Class YourButtonClass
  Field funcPointer:Callback
  
  Method SetCallbackWhenDone:Void (c:Callback)
    funcPointer = c
  End
  Method Update:Void()
    If someCondition
      funcPointer.Run()
    End
  End
End
The SampleFunction class acts like a callback function. Now you can set it like this: button.SetCallbackWhenDone (New SampleFunction) | 
| 
 | ||
| Forgot to thank you for your good help. I've been reading the eckels pattern books, lots of good stuff in there. | 
| 
 | ||
| you can also use recursion. you can call a function based on a string matching its name; I created a class that mimicked function pointers. It wasn't much slower than direct calling; I was storing as much of the information as possible in the instance... | 
| 
 | ||
| So to use recursion thus you would call a manager function with the wrapper name and it would then call itself with the inner private name of the function?  I supPose doing it this way you could construct functions in various ways at runtime, moving the function addressing from namespace into string space. .. |