How to Create a Timer on a thread

Godot Version

4.3

Question

I am troubleshooting my first game in Godot and I’ve come across an issue with very poor performance on lower-tiered android devices. I pinpointed the issue as the timer - the in game timer cannot function on the main thread. I’ve posted about my timer issue in this forum before, but that was before I know the issue was not really the timer, but the fact that the timer needed to be placed on a background thread.

I’ve put together something rough like so:

var counter:int = 0

func main_timer_start():
	
	for x in 10000:
		await get_tree().create_timer(1.0).timeout
		time_counter += 1
		call_deferred("publish_time")

For simplicity sake this gets the job done. I call the “main_timer_start” function via a thread. Although this did improve my game’s performance I am not confident in this solution. I am unsure if this good code - is it wise practice to create a new timer every second? My inexperience betrays me!

I tried creating a 1 second timer node. But I couldn’t call one_second_timer_node.start() via a background thread. I figure I could use call_deferred - but wouldn’t that just be bouncing the call back to the main thread - which would defeat the purpose of using a background thread for all the time calculations?

When I originally posted about this I thought the timer node was inaccurate, so I asked about timer accuracy. I got a great answer to calculate the time via the process func. Something like this:

func _process(delta):
	
	match(state):
		TimeGo:
			time_millisec = Time.get_ticks_msec() - startTime
			%TimeLabel.text = getTime(time_millisec/1000)

Again, this is too much to run on the main thread. However, I can’t think of a way to run this on a background thread using delta. Doesn’t seem possible.

So, so far all that I have that works is to create a new timer every second inside of a thread. Is there a better, more efficient, way to do this? Am I committing some programming sin? Or is my method OK to get the job done? Any insight would be helpful - thanks.

1 Like

Can you provide a little more background on the nessesity of the 1 second timer? timers have an auto-play (autostart) feature meaning you could not write any code. depending on your project.

I added a Timer Node to the tree and set the Wait Time to 1 second. Then I call the timer - timer.start() when I need to. I attached a function to the timeout() - let’s call it “update_time()”. In that function I update the time display and then I call to start the timer again - timer.start(). This gives me a 1 second timer. Hope that is clearer.

1 Like

in my understanding this line of your code

await get_tree().create_timer(1.0).timeout

is actually creating a new/different timer outside of the one you have in your scene. Since I use this line in nodes that don’t have timers at all. create_timer summons another timer and completes your task. You can check out the structure of your tree at runtime by using remote it’ll show you what is running in the game