Why is adding more parameters to a subscribe event so overly complex?

It feels like a basic thing to be able to have a button from an array that you want to save the index number and use it within a function, but its overly complex and unorthodox (especially for a beginner like me) to make a handler and subscribe to that instead.

Would be nice to have a way to easily add parameters to events so we can pass information, without any need to setup handlers (and ideally something that a beginner can understand, verse is already hard as it is)

3 Likes

I understand your frustration. Im also a beginner and its hard to learn everything when there isnt much out there yet. But from my understand it has to be that way. You dont want to accidently change something else by not clearly defining exactly what you want to change, how you want to change it, and even what can change it. (Newbe so i dont even know the terminology). The one thing I keep telling myself is verse is still very new. The fact it actually runs as good as it does is pretty amazing. With time it will get better. Ive been having really rough nights of trying to get my program to execute the way i think it should, but it wont. But when you finally get it to its so rewarding.

1 Like

Lambda functions (or anonymous functions) would be of great help for that. Something like :

SwitchIndex := 4
SwitchDevice.TurnedOnEvent.Subscribe(
    (Agent: agent):void = OnSwitchTurnedOn(SwitchIndex, Agent)
)

It’s implemented in most of the languages so I guess they’re planning on doing it someday.

3 Likes

My favorite way to do this is using Await instead of Subscribe:

OnBegin<override>()<suspends>:void=
    for (Index->Switch : Switches):
        spawn {AwaitSwitchTurnedOn(Switch, Index)}

AwaitSwitchTurnedOn<private>(Switch:switch_device, Index:int)<suspends>:void=
    loop:
        Agent:agent = Switch.TurnedOnEvent.Await()
        # Do something with Agent and Index
8 Likes

Interesting, does that work for existing devices in the world too?

In the example @Ep8Script showed, the Switches variable is an @editable variable containing an array of switch_device that are referenced through the Verse device in UEFN. So yeah, it works on existing devices.

I’d like to point out that this method can be more convenient and easy to use but it’s still not beginner friendly, also, it can only be triggered once per frame, whereas the Subscribe() method could be called many times on the same frame (correct me if I’m wrong)

EDIT: I was wrong :+1:

2 Likes

Verse is single-threaded, so as long as you don’t call any suspending functions afterwards without spawn, it will work just as well as Subscribe with each signal running in order.

2 Likes

Hey, what I meant by that is that using loop: in a function can only trigger once, since the loop has to silently Sleep(0.0) at the end of it.

Using Subscribe() will store your function inside the device and thus can be called (even if sequentially) many times on a single frame.

But I’m not sure so maybe someone could run the test though :man_shrugging:

When it loops, the function will call the Await again and then yield to whatever else needs to happen for that tick, you don’t need to use sleep there

Uhm sorry I’m not fully awake yet, what I meant is that the loop: cannot iterates twice on the same frame (when used with Await())

The example I used above has always worked for me, even in multiplayer :thinking: I might need to do some more testing

1 Like

@im_a_lama Yeah this works perfectly (the trigger is being triggered twice in one tick by the button press and the print runs both times):

test_device := class(creative_device):

    @editable
    Button<private>:button_device = button_device{}

    @editable
    Trigger<private>:trigger_device = trigger_device{}

    OnBegin<override>()<suspends>:void=
        sync:
            loop:
                Button.InteractedWithEvent.Await()
                Trigger.Trigger()
                Trigger.Trigger()
            loop:
                Trigger.TriggeredEvent.Await()
                Print("Trigger was triggered")
3 Likes

Thanks for running the tests for me, I was actually starting to think that I was wrong, it’s good to know, nice solution :smiley:

2 Likes