Events on UI MenuBar in LUA

  1. 6 months ago

    hi,

    I have a problem understanding how to bind certain actions to UI Events using Lua.
    Basically, I have a menu bar with different entries and those entries again have several items. I want to specify what happens for each item if it is selected (so at a Click Event).
    I have looked into the c++ Version of the Code, with the handleEvents function and if-Statements checking which field was selected and so on, but I don't know how to do it using Lua.

    I have managed fine to add actions to UIButtons, like so:

    class "Click" (EventHandler)
    function Click:Click()
        EventHandler.EventHandler(self)
    end
    function Click:on_click_event_OkButton(e)
    	
    	window.hideWindow()
    	
    end
    
    
    okButton:addEventListener(click, Click.on_click_event_OkButton, UIEvent.CLICK_EVENT)

    But whenever I tried adding such a function to a MenuBarEntry or MenuBarEntryItem or just the MenuBar, it didn't work.

    Could someone give me a hint in the right direction? I feel like I have already spent too much time on trying to figure this out myself.

    thanks

  2. fodinabor

    9 Feb 2017 Administrator

    did you have a look at: http://polycode.org/learn/events#lua ?
    you don't have to create an extra class for every handled event - just declare a function and register a callback for that:

    okButton:addEventListener(nil, onOkButton, UIEvent.CLICK_EVENT)

    that function should look like the onCollision here:
    https://github.com/ivansafrin/Polycode/blob/master/Examples/Lua/Game_Demos/DemoPlatformer/Scripts/Main.lua#L51

    you can then cast the event parameter:

    local uiEvent = safe_cast(event, UIEvent)

    to get access to ui event specific information.

    by using get uiEvent:getDispatcher() you should be able to do the same differentiation as in C++

    Hope I could help!

  3. Yeah, I did have a look at that, but thank you, I think that might help, I'm gonna try and see if I can make anything work now. Thank you.

  4. Deleted 6 months ago by ariIntern
  5. It seems like using UIEvent.OK_EVENT makes things work.

 

or Sign Up to reply!