Timer sometimes work/ sometimes not

Godot Version

v4.2.2.stable.official [15073afe3]


i have added code and timer property,

i have made sure that timer is same timer show in left side,

i have tried increasing priority also

some times it works fine and print(“timercallled”) run but some time
only this is not called, every time the timer is called the level reset.

the bugs occur randomly, i have tried checking if it happened 2nd time (thinking it might not be resetting properly) but that is not the case

The timer could be restarting if a player is entering multiple times. make sure to test if the timer is running before restarting it.

if timer.is_stopped():

floating point equality checks are generally a bad idea, this may not run as you expect it to.

if (Engine.time_scale == 0.5):

I tried
but still getting that issue,

even though i understand your floating point, and will not use this this, it has still not worked
also i have used print statement after
if (Engine.time_scale == 0.5):
which is not logging in console more that once, till the level_restart

First of all, why don’t you have a CollisionShape2D as a child of your KillZone/Area2D at the SceneTree? Do you add one as a child from another script? Because I didn’t understand how did you get the message “You Died!” on the console. Giving this information could help.

Second of all, you’re working with physics, so you gotta change the Process Callback (first Timer variable on the editor) from Idle to Physics.

In third place, I don’t know if you’re making this but, if you don’t stop the KillZone/Area2D from detecting another areas, the “on_body_entered” can still be triggered if it has a CollisionShape2D as a child. And, if you have more than one player and another player collide with this while the Timer is running, the Timer will restart. I don’t how your game works, but if you plan on adding a multiplayer system, you gotta keep that in mind.

@gertkeno is right about checking equality between two floating points, that’s like comparing equality between instances of a class. That’s not advised. Instead, you could make a range to check if your desired value is between two numbers, as I show below:

if(Engine.time_scale > .4 and Engine.time_scale < .6):

And a last advice for you, you don’t need to queue_free() the CollisionShape2D of your target body, you could just change the monitoring property of your KillZone to false, wich can prevent a lot of problems, including my third point.
Try the second advice and maybe the third one and let me know if it worked for you. If not, answer my first question, then maybe we can find out what is going on. Good luck on your project.

So basically, I have created a single kill zone that I attach to every enemy with an AreaCollisionShape2D inside.

In the left panel, the third node is the kill_zone which I mentioned earlier.

I tried changing the process callback to physics as well, but encountered the same issue.

Another point is that it would be difficult for me to change it everywhere because I am using this script in multiple areas. Changing just the collision for this enemy might not solve the problem.

I only have one player, so that cannot be the issue.

Regarding Engine.time_scale, I understand that point and will avoid using it again. However, this time it cannot be the issue because I have a print statement after it, and it’s not being called again in any case.

I am very sure that timer.start() is not being called more than once.

Thank you, Gertkeno and CodingAndHelping, for taking the time to help.

1 Like

Oh, I found the issue. What was happening is that I was calling queue_free() on the enemy after a delay, and sometimes it was getting called before the player script, so the logic to reset was not being executed. I wasn’t aware of this. Thank you again for helping me out.

1 Like

Good to know that you found the issue! Success to your game!