Can anybody tell me why this crashes godot?

Godot Version

4.2.2

Question

i have this code that is only supposed to check every second, but instead seems to go off every frame, can anybody tell me why this is happening?

	while body_entered:
		if body.is_in_group("Player") and !on_cooldown:
			print("test1")
			on_cooldown = true
			body.health = body.health - damage
			await get_tree().create_timer(1.0)
			on_cooldown = false
			print("test2")

You should not be using “while” for something that you intend to have checked every second. At what point do you expect for “body_entered” to become false?

A “while” loop causes the code to repeat forever until the condition at the top becomes equal to false (or you use break or continue). This means your signal starts the loop and then it never stops. This means nothing else in the game will happen either - if it’s not inside the while loop - which makes your game appear to freeze up (really it’s just running that tiny bit of code over and over).

Additionally, “body_entered” is a signal (assuming you’re using it as expected) so you need to watch for the signal to be emitted. There should be no need to check it every second because the engine will tell you if it gets triggered. Can you share more of your code?

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.