Additional parameters on Suscribe or at least know which device triggered an event

I have this code. An array of buttons and when a button is pressed I want to display in a hud_message_device which button was pressed.

# A Verse-authored creative device that can be placed in a level
UI_Announcer := class(creative_device):
    
    @editable
    HudMessageDevice : hud_message_device = hud_message_device{} 

    @editable
    Waypoints : []button_device = array{} 

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
    
        for( Waypoint: Waypoints):
            Waypoint.InteractedWithEvent.Subscribe(OnButtonInteractedWith)

    OnButtonInteractedWith(Player:agent):void= 
        Print("button pressed")
        HudMessageDevice.SetText(SetMessage("Button XXX pressed"))
        HudMessageDevice.Show()
        
    SetMessage<localizes>(MyMessage:string) : message = { "{MyMessage}" }

Can we add customer parameters on suscribe or at least in function OnButtonInteractedWith know which button was pressed beside knowing which player ?

Hey @K-MUS,

There are a couple ways to do this in Verse. Here is one way using the concurrency system.

# A Verse-authored creative device that can be placed in a level
UI_Announcer := class(creative_device):
    
    @editable
    HudMessageDevice:hud_message_device = hud_message_device{} 

    @editable
    Waypoints:[]button_device = array{} 

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void =
        for (I->Waypoint: Waypoints):
            OnButtonInteractedWith(Waypoint, I)

    OnButtonInteractedWith(Button:button, ButtonIndex:int)<suspends>:void = 
        # Suspends this function until Button.InteractedWithEvent is signaled
        Agent:agent = Button.InteractedWithEvent.Await()

        # ALTERNATIVE: If you don't care about the Agent:agent that triggered the button you can just await the event
        # Button.InteractedWithEvent.Await()

        # The inputs to the function are still around, so we can reference them after the InteractedWith event occurs
        Print("Button {ButtonIndex} pressed")
        HudMessageDevice.SetText(SetMessage("Button {ButtonIndex} pressed"))
        HudMessageDevice.Show()
        
    SetMessage<localizes>(MyMessage:string) : message = { "{MyMessage}" }
1 Like

Thanks, that’ll work.

Is there a way of getting the name of the device in the editor. Because in this way I will get the index number as they where put in the array in the editor.

Guide to Event Subscribing with Additional Parameters (Handler Functions) I’ve written a guide that provides an alternate way to subscribe with additional parameters

1 Like

Is there a way of getting the name of the device in the editor. Because in this way I will get the index number as they where put in the array in the editor.

There is currently no way to get the editor display name for a device in Verse code.

Thanks, this is exactly what I was looking for.