0 votes

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():
    print(timer)
    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

Godot version 3.4.4
in Engine by (12 points)

Another valid way to fix this would be:

if timer >= 1:
    # do stuff

1 Answer

+1 vote

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.

by (12,869 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.