Engine.time_scale doesn't work with timers?

Godot Version

4.3 beta 2

Question

Despite everything i’ve read, Engine.time_scale seems to have no impact on timers created with get_tree().create_timer(). Is this a bug? Is there a way to get them in sync?

Example code:

func _physics_process(_delta: float) -> void:
	if !enemies_in_range.is_empty() and is_ready_to_fire:
		fire()

func fire():
	is_ready_to_fire = false
	# fire lasers and stuff
	get_tree().create_timer(0.25).timeout
	is_ready_to_fire = true

func _on_pause_button_pressed() -> void:
	Engine.time_scale = 0.0    # everything stops except firing

func _on_speed_2_button_pressed() -> void:
	Engine.time_scale = 2    # everything speeds up except firing

As an example, normally 2 green towers can kill 10 creeps. At double speed 7 make it through. At 5x speed the creeps are barely scratched. Fire rate doesn’t track Engine.time_scale at all.
image

You are not waiting for the timer to finish, you might notice a warning along the lines of “Statement has no effect” for your timer.

You must await it for the effect you want, or since this timer will be created often I’d recommend converting it to a timer node

func fire():
	is_ready_to_fire = false
	# fire lasers and stuff
	await get_tree().create_timer(0.25).timeout
	is_ready_to_fire = true

Oh. My. God. i have used await a thousand other places and somehow didn’t notice that it was missing there. And i’ve been looking at this for two days now. i’m going to crawl in a sewer now.

image

Technically i still have some problems (towers are slightly less effective at higher speeds) but the towers are clearly speeding up so i’ll need to investigate other possibilities…

That would be the difference between a Timer and using create_timer. If a Timer is not set to “Oneshot” it will carry over any elapsed time.

As an example, let’s say your timer is set to 100ms, and your timer has 2ms left. On the next frame (at 16ms per frame) create_timer it will be made anew back to 100ms ignoring the extra 14ms spent, but a Timer will carry over the elapsed time to 86ms.

For repeating timers like this I highly recommend using the nodes.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.