Reset Game Time When Button Pressed

I have a level that the character can walk around. When a button is pressed, a timer should start that counts how long it takes the character to get to a specific location. I have the timer functioning using Get Game Time and linking that to a UMG Timer. It works fine if I start it immediately on level load, but if I wait a few seconds, then trigger the timer, the total time counts the time since the level loaded in the timer. Is there a way to reset the game time when the button is pressed so it always starts counting up from 0? Or is there a better way to time this?

I originally tried doing this with event tick and a delay, but I wanted to be able to get the timer to show down to tenths, or hundredths of a second.

You can save initial time (when button is pressed) to the variable and then pass a difference between current time and initial time to your timer.

1.Okay so make a boolean variable called “TrackingTime”, a float variable called “CurrentTime”, another float called “TimeRate”, and one more float called “FinalTime”.

2.Set it up so whenever you press the button, it sets “TrackingTime” to true.

3.Then drag a branch out from your event tick. Connect up “TrackingTime” to the condition in the branch and from the true node of the branch, set “CurrentTime” as “CurrentTime” + “TimeRate”. This means that whenever “TrackingTime” is true, it will add the “TimeRate” value to the “CurrentTimeValue”. This also means that you will have to tweak the “TimeRate” value to get it to be close to realtime seconds.

4.Then whenever you want it to stop, whether it be from pressing another button, when you get to a box collision trigger etc, set “FinalTime” to the “CurrentTime” value, which will give you your score value that you’re looking for when you complete the task, then set “TrackingTime” to false, and then set “CurrentTime” to 0. This means that once you go back and try the timer again it will start at 0.

I believe this should give you the functionality you are looking for. If you any questions regarding my explanation feel free to ask as it is a bit much to swallow as far as information goes. Hope this helps tho :slight_smile:

could you show this in blueprint form (for a newbie like myself) please? Cheers

Something like this should work :stuck_out_tongue:

Instead of using tick, I would use a timer. Timers pretty much do exactly what you need in this case. Using timers avoids tick completely, allows you to control update frequency, and doesn’t run any logic when it’s not needed.

You will have two custom events (one to start the timer and one to end the timer), and one function that updates the recorded time every time is runs: