Passing Which Device in an array signaled an event

I’m trying to figure out which device in an array signaled an event but can’t find a nifty way to pass an agent into function after the device in the array gets signaled.

Anyone have a nifty solution to this?

Test := class(creative_device):

    @editable Muts : []mutator_zone_device = array{}
    var mutindex : []int = array{}
    var count : int = 0

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        InitializeArray()

    InitializeArray():void=
        for(device:Muts):
            set mutindex = mutindex + array{count}
            set count = count + 1

    IdentifyMut():void=
        for(i:mutindex):
            index := i
            if(Mut := Muts[i]):
                Mut.AgentEntersEvent.Subscribe(PassDevice(**AgentWouldGoHere**, index))

    PassDevice(Agent:agent, mutzone:int):void=
        block:

I usually add a utils.verse file and include this:

using { /Verse.org/Simulation }
 
(Listenable : listenable(agent)).SubscribeAgent(OutputFunc : tuple(agent, t)->void, ExtraData : t where t:type) : cancelable =
    Wrapper := wrapper_agent(t){ExtraData := ExtraData, OutputFunc := OutputFunc}
    Listenable.Subscribe(Wrapper.InputFunc)
 
wrapper_agent(t : type) := class():
    ExtraData : t;
    OutputFunc : tuple(agent, t) -> void
    InputFunc(Agent : agent):void = OutputFunc(Agent, ExtraData)
 

Then you could do this:

Mut.AgentEntersEvent.SubscribeAgent(PassDevice, index)

PassDevice(Agent:agent, mutzone:int):void=
    block:

The SubscribeAgent allow you to pass an aditional param to the subscribe method

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.