Timer waits for longer than intended

Godot Version

4.3

Question

I have a function that freezes time for a specified amount of time:

func freeze(time: float) -> void:
	_current_time_scale = Engine.time_scale
	Engine.time_scale = 0
	_freeze_timer = get_tree().create_timer(time, true, true, true)
	await _freeze_timer.timeout
	Engine.time_scale = _current_time_scale

_current_time_scale and _freeze_timer being declared outside of a function. I had this exact same code working before, then I retyped it and now it freezes for too long. It’s set to freeze for 0.2 seconds: freeze(0.2) but it ends up freezing for well over 2 seconds. I used breakpoints to see if the time to wait was set correctly, and it was.
image
After continuing the breakpoint, it doesn’t pause for nearly as long. so it’s only when uninterrupted by breakpoints when it glitches out. What is causing this and how can it be fixed?

I haven’t tried changing the time_scale but the docs do say

Note: It’s recommended to keep this property above 0.0, as the game may behave unexpectedly otherwise.

You could try 0.0 instead of just 0. Your issue may be as simple as the engine doesn’t like 0.

Will it fix your problem if you keep your timer in the process update loop instead of physics process update?
get_tree().create_timer(time, true, false, true)

That fixed it. Thanks!

1 Like