Detecting time_left doesnt work

Godot Version

4.3 stable

Question

I wanted to make it so for this state it would flip horizontally halfway through (at 0.05 seconds) but it wouldn't do anything, It would detect when i made it >= or <= but never when at .005. How would I detect the timeleft at a specific time?

func update(delta: float) -> void:
	if $"../../Timer".time_left == .05:
		print("flips once and ONLY once")
		player.animated_sprite.flip_h = not player.animated_sprite.flip_h
	if $"../../Timer".is_stopped():
		print("Timer Ended")
		finished.emit("Idle")

NEVER compare floats like that, due to precision issues you have big chances to never reach this value, especially in timers because they have their count done subtracting from floats like 1/60 so its almost impossible timer_left be exactly 0.5. Create a variable already_fliped = false and do something like that:

var already_flipped = false


func update(delta: float) -> void:
	if $"../../Timer".time_left <= .05 and already_flipped == false:
		already_flipped = true
		# Do your stuff

See more: Floating-point error mitigation - Wikipedia

1 Like

I dont exactly understand how floats work, but atleast it does work !!! so thank you!!