Help me build an accurate countdown timer

Hey all, I’m back with a small request…

I need to rebuild my timer as it is not only slightly inaccurate, but I have no way of controlling it.
Basically, I have a custom event “LevelTimer” I set 2 variables for min and sec, decrement sec manually after a 1sec Delay, when both reach 0 → end the level.
The problem is that if the level is ended sooner,(for other reasons) I pause the game do some stuff in the EndLevel widget and when I stream the next level and unpause the game the original event continues to run from where it left off. Even if I reset the original min and sec variables.

I would like to build a more complex timer (based on world delta seconds) by using Set Timer by Event, be able to pause it and reset it on demand and constantly update a single variable for sec (float with 2 decimals).
I’ve checked countless tutorials on YT but none show how to actually control the timer, by stopping/resetting it on demand.
As always, any pointers are greatly appreciated

If you put the timer in the game instance, it can exist in between levels if you want, but also, you can always use it without having to put the code in each level.

When you start a timer by event, you get a reference to the timer, which you can use to stop it and restart it.

1 Like

Well, I have it in my GM and apparently, somehow, it still persists through streaming levels somehow.

Imagine this…
user completes Level1 → I pause the game → display EndLevel widget.
Play with some math inside the widget, if user clicks “Continue” → I load the next streaming level

When the new level loads → I reset the min and sec variables in GM → call the LevelTimer custom event from GM again and when that comes up, for a split second it shows the reset variables, but after that it continues from where it left off in the previous level.

Nonetheless, just by looking at your example I realized I can link my LevelTimer event to another event and by using set timer by event I can actually get the reference to my timer which opens up a whole new set of controls.
Thank you so much!

I still left the timer in my GM bp but when the level ends I no longer need to pause the game.
I just disable the input for the player → Pause the timer ->load the EndLevel widget
Do my math in there → when the user clicks “Continue” → Clear the old timer → load the new level → call the StartTimer event and it works perfectly.

I would still like to rebuild my timer function to use only seconds and milliseconds since I don’t really need minutes.
I don’t have a clue about using the delta seconds to count down from 60 using only seconds and milliseconds up to 2-3 decimals.

Anyways, @ClockworkOcean your answer got me in the right direction. Much appreciated good sir!

1 Like

The GM must be for the persistent level, which lives through all streaming.

I wouldn’t bother going down to milliseconds, there’s too much fluctuation. But seconds is good.

@ClockworkOcean
The only reason I would like to go with sec and milliseconds is that I want to use that number as a bonus multiplier.

Each target has a specific # of points associated with it.
Let’s say you have 10 targets in Level1.
2 players complete the level.
The way I want to differentiate which one is the winner is by multiplying the total points lets say 10targets * 10 points each = 100 points * (the time remainder for the level). The milliseconds could make the difference here.

Isn’t World Delta Seconds supposed to be super accurate independent of the user’s fps???
I just have no clue how to build a countdown timer using delta

Why worry about delta if you have a framerate independent timer? That’s the whole point. The timer couldn’t care less about your fps.

The only thing it cares about is when you access it. The thing is that the timer is capable of ticking between the frames, too.


Try this, see if it’s accurate enough for your needs:

1 Like

Ok, Let me show you how I have it set up…

This works, but I just realized I don’t ever need the minutes. I could however make use of the milliseconds… just no clue how to get that and incorporate it in my code.
Instead of 1:00 the timer could start at 60.00 seconds were .00 is the countdown for milliseconds

Ok, so I tried this…

This results in an infinite loop :frowning:

Wow… I’m an idiot… I don’t need to loop this back since the function runs from a Set Timer by Event.
This actually works! :smiley: Yay!

However, of course I pushed myself into another corner now…
How can I add my beeping sound for the last 10 sec? 10,9,8…0 (and not the decimals in between)??

Shouldn’t this work???

Hope this might help:


CountdownBeep

1 Like

@pezzott1
That looks pretty much like what I want… :slight_smile:
Couple of questions though…

  1. how do you collapse those math expressions?
  2. What if I want to have 2 decimals displayed?

Also, what are your defaults for time interval, default time and default beep at time set to?

That’s a Math Expression node.

Try this:

  • TimerInterval = 0.1
  • DefaultTime = 15.0 // Can be any value.
  • DefaultBeepAtTime = 10

Note that in this example if DefaultTime is less that DefaultBeep it will not work. It has to start equal or greater.

@pezzott1

Got it! It’s working now… slightly different than yours, but greatly inspired :smiley:

Why when comparing floats it was not working??? if 10.00 = 10.0 was not triggering the sound… but when ceiling it to an int and comparing to 10 it does???
Same thing for when the timer reached 0. If 0.00 = 0.0 was not changing the bool??? Why???

1 Like

Comparing floats can be tricky because of precision errors. Notice I used TimeLeft < 0.0 and not ==. :innocent:

Also keep in mind that between each timer loop it will not be exactly the value of the interval variable. It will always by greater by a few miliseconds. So don’t exepct it to run every 0.1 seconds, reality will be 0.1 + ms fraction to next frame.

Don’t think there is a way around that.

@pezzott1

I see! Good to know. Thank you so much for your time.

Just noticed that when I check if TimeLeft == 0 and Pause the timer… it actually pauses it at -00.01 :face_with_raised_eyebrow:

No way of actually stopping it at 00.00???

Guess it ticked 1 more time before PauseTimer actually kicked in.
I guess it’s not that big of a deal… I can re-Set it to 00.00 when that happens

Gotcha!