How you can Subscribe from an event of your own function?

Hi. I am seeing this code in the tutorials:

basic_countdown_timer := class:
    <# This block runs for each instance of the countdown_timer class.
    We can setup the canvas once here. #>

    CountdownEndedEvent<public> : event(float) = event(float){}
    var RemainingTime<internal> : float = 0.0

    StartCountdown<public>() : void =
            spawn:
                RunCountdown()
    var TotalTime<private> : float = 0.0
    # The timer "precision": how often, in seconds, it ticks.
    TimerTickPeriod<private> : float = 1.0

    RunCountdown<private>()<suspends> : void =
        # We loop with the TimerTickPeriod.
        # The UI is also updated each time.
        loop:
            Sleep(TimerTickPeriod)
            set TotalTime += TimerTickPeriod
            set RemainingTime -= TimerTickPeriod
            # Timer End
            if (RemainingTime <= 0.0):
                CountdownEndedEvent.Signal(TotalTime)
                break

it is a basic timer, and as you can see there is this interesting line:

CountdownEndedEvent<public> : event(float) = event(float){}

and this one

CountdownEndedEvent.Signal(TotalTime)

In first place I though this could be used as a subscribable function, but I tried to subscribe and it is creating an error:

NewTimer : basic_countdown_timer = basic_countdown_timer{}
                NewTimer.CountdownEndedEvent.Subscribe()

Unknown member Subscribe in event(float).

How can you listen the event of the timmer ending?

1 Like

I had the same problem.

Events are actually meant to be used in async contexts. So what you need is a function that suspends to “Await” the event to fire. Check out this post I made on the pattern I follow: Events in Verse Pattern