The question about timer_device api

The goal is this: I need a timer to count down the time, and then when the time is complete, call different functions depending on the current game state. So this timer would reset and ideally call an event after each countdown is completed.
Here is my code:

InitializeGeneralTimer():void=
        Print("Start Initialize Timer!")
        # Set the timer start at Lobby time
        GeneralTimer.SetActiveDuration(lobby_waiting_time)
        GeneralTimer.Start()
        
        # Register the timer compeleted event
        GeneralTimer.SuccessEvent.Subscribe(OnGeneralTimerCompletes)

OnGeneralTimerCompletes(Agent : ?agent):void=
        Print("On timer complete!")
        # Should switch the game stage to IN_GAME_STAGE
        if (GS = game_stage.LOBBY_STAGE):
            set GS = game_stage.IN_GAME_STAGE
            Print("Set GS to IN_GAME_STATE")
            # Start mini game
            StartGame()
        # Should switch the game stage to LOBBY_STAGE
        else:
            set GS = game_stage.LOBBY_STAGE
            Print("Set GS to LOBBY_STAGE")
            # End current game and teleport players back to lobby area
            BackToLobby()

StartGame():void=
        Print("Start Mini game!")

        # Need to set the timer to correct game time
        GeneralTimer.Reset()
        GeneralTimer.SetActiveDuration(game_time)
        GeneralTimer.Start()

BackToLobby():void=
        Print("Players back to Lobby!")

        # Need to set the timer to correct lobby time
        GeneralTimer.Reset()
        GeneralTimer.SetActiveDuration(lobby_waiting_time)
        GeneralTimer.Start()

But it seems that OnGeneralTimerCompletes is only triggered once after the first-time timer is complete, not after the second time. I don’t know why, is there something wrong with my code? Or am I misunderstanding the meaning of these interfaces?

1 Like

Shot in the dark here, and truthfully don’t see why you would have to but what if you re-subscribe the event when your timer completes?

Alternatively you could restructure your game loop, Start timer so players see the countdown on their HUDs → Sleep for TimerMaxDuration → Move players to the minigame → Sleep again and move players back to lobby or even better, have the mini-game set a logic variable on completion then in your game loop, use a loop and conditional statement → break to make the game wait to relocate players and start the process over again.

Your game loop would look like:

loop
    Lobby Time
    Sleep
    Relocate to minigame
    Loop till minigame ended state, then break
    Relocate to lobby
1 Like

I tried re-subscribe the event but it didn’t work.
Your suggestion is great. And I’m actually using a similar way to do this right now. I just check the remaining time in the loop and when it reaches the end call the function manually.
I just think if the timer_device works correctly as I thought it would be much easier.
Thanks for your reply!

1 Like

Hello

Does that timer device actually start again?

For me Start() seem not to work for some reason at first.

Then after too much time wasted on the timer, I found that a Sleep(0.2) before the Start() can actually fix that. (Sleep(0.1) did not work, Sleep(0.17) seems the minimal)

I hope that was helpful.

3 Likes

Thank you so much for replying. I think this is a bug from Epic. I’ll report this bug to them to see how it works. But currently, I’ll just use a loop to create a timer by myself I guess. Hope them fix this sooner.

Hi, I’m currently also facing this issue. Can you provide the syntax that you’re describing? I cant seem to get mine to work. Thanks in advance!

The Timer Device has a setting called “Completion Behavior” which is by default set to “Stop”, you want to set it to “Reset”. This was a long time from the original post by @landscapecmy but if you’re still looking for the answer to this, I’m 99% sure that’s what you all are dealing with.

1 Like

This seems to be the solution.

Creating a new function that delays at least 0.2 and then resets and starts the timer was the only way I could work around this problem, regardless of the device’s completion settings.