Equivalent to arrow functions in Verse?

Im wondering if there is an equivalent to arrow functions in verse. The use case I have is I want to call SpawnerDevice.SpawnedEvent.Subscribe(OnPlayerSpawned) but I want to add an additional parameter into the OnPlayerSpawned function call. It’d look something like this:

SpawnerDevice.SpawnedEvent.Subscribe((InAgent: agent) -> OnPlayerSpawned(SpawnerDevice, InAgent));

Is this possible? Is there a way to include additional data into these callbacks?

Hi,
it seems you’re referring to lambdas, which are currently not available in Verse.

Two common options to solve this are:

Custom event handler class

  • Use a custom event handler class which holds a reference to the sender device and a function with the correct signature for the type of event you’re handling. Then make an instance of that handler class, save the sender and subscribe the event to the custom handler’s function.
    For example, with a button_device:
my_event_handler := class:
    Sender:button_device
    HandleButtonInteractedWith(Agent:agent):void=
        Print("Button Pressed")

# ...inside your creative device...
    OnBegin<override>()<suspends>:void=
        EventHandler:my_event_handler := my_event_handler{Sender := Button}
        Button.InteractedWithEvent.Subscribe(EventHandler.HandleButtonInteractedWith)

You can check out an application of this in the Tagged Lights Puzzle tutorial.

Await the event

  • Create an async function that acts as a handler and uses the XEvent.Await() pattern. This is usually better for single-time activations (since you don’t have to unsubscribe the event or maintain state).
    Again, with a button_device and support for multiple activations:
    MyEventHandler(InButton:button_device)<suspends>:void=
        loop:
            Agent := InButton.InteractedWithEvent.Await()
            Print("Button Pressed")
  
    OnBegin<override>()<suspends>:void=
        spawn{MyEventHandler(Button)}
5 Likes

Oh perfect this works great! Thanks so much for the examples.