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.