How to Create an Accurate Timer For Lower-Tiered Devices?

Godot Version

4.3

Question

New to Godot and I’m stuck trying to adapt my game so that it works across all types of Android devices. Low-end devices have been been source of great, great, colossal pain. I want to display to the player a simple timer: minutes and seconds.

The player is challenged to do certain things within a certain amount of time, so to see the timer being jumpy/inaccurate would lead to a poor experience for players.

Attempt #1:
I added a simple 1 second timer, some simplified code like so:

Timer.start()

func timer_end():
       time += 1
       %clock.text = displayTimeAsText(time)
       Timer.start()

Easy enough I thought, but when testing the time was all over the place. After 1 minute it was off by 6 seconds in one test and 13 seconds in another.

Attempt #2:

var time_elapsed:float = 0.0
var counter:int = 1

func _process(delta):
     time_elapsed += delta
			
     if floor(time_elapsed) == counter:		
	     %clock.text = displayTimeAsText(counter)
	     counter += 1

Improved, but timing was still off. Attempt #3 was to try this on the _physics_process(delta) but I had the same results.

Attempt #4:

var time_elapsed:float = 0.0
var counter:int = 1000
var start_time:int

func start_game():
       start_time = Time.get_ticks_msec()

func _process(delta):
        time_millisec = Time.get_ticks_msec() - startTime
		
        if floor(time_millisec) >= counter:
		     counter += 1000
             %clock.text = displayTimeAsText(counter/1000)

After 1 minute, attempt #4 gave me the most accurate results, but the clock was jumping, the count up was noooooot smooth at all. And with all my testing, I wasn’t even playing the game. I was just starting the game and letting the clock go. With no other activities the clock still was jumpy, so my fear is that during game play with more going on the clocks performance would only get worse on these low-tier devices.

Am I missing something basic here? And words would be helpful. Thanks.

The accumulation method will always introduce errors, so abandon that approach. It’s also overcomplicating a simple process. You were on the right track, though:

time_millisec = Time.get_ticks_msec() - startTime
%clock.text = displayTimeAsText(time_millisec)

That’s it.

1 Like

Thank you for your response. I get your suggestion and I’m implemented. The simplified version still yeilds the correct overall time, but it still is a bit jerky in sections during the count. A few places where it sloes down a tick, and then catches up. The count up for timer is still not as smooth as it needs to be.

At this point I think it is the device’s hardware. My game functions properly on other devices, this tablet might have more issues than just being “low-tier.”