if timer == 1 returns false when timer is 1

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gtpark0904

I have the following code in a node and have a timer node named “Monster_Timer” as its child node

func _on_Monster_Timer_timeout():
	if timer == 1:
		timer = 0
	timer += $Monster_Timer.wait_time

the wait time is 0.05

When I run the code, the number keeps rising even when it reaches 1.

What’s wrong with the code??

I solved the issue
First, the code works fine when the wait time is 0.5
So I changed the code into this:

if int(timer) == 1:
	timer = 0

I guess when the wait time gets small, something goes off

Another valid way to fix this would be:

if timer >= 1:
    # do stuff

jgodfrey | 2022-08-04 13:40

:bust_in_silhouette: Reply From: Calinou

This is either due to floating-point precision (use is_equal_approx() to compare floating-point numbers between each other), or because the timer is never going through the 1.0 value due to it being updated once per rendered frame only.