The while statement does what you ask: while something is true, do this.
You could use it to wait until something happens:
if fall_strength >= 120:
while not is_on_floor():
pass # Wait until is on floor
print("fall damage taken")
But this would lock up your game until the character hits the floor, and if not, cause a “busy wait”, wasting resources. If you’re using KinematicBody, try using get_floor_velocity():
if is_on_floor() and get_floor_velocity().length >= 120:
print("fall damage taken")
Thanks so much
GodotUser21 | 2022-09-02 10:54
this actually lags the whole game and the game will freeze
GodotUser21 | 2022-09-03 14:15
Did you do the second alternative? The first one will most likely freeze your game.
Ev1lbl0w | 2022-09-03 14:28
while doesn’t stop and take a break every time it loops (unless you tell it to sleep), it just jumps back and continues executing itself. Picture your while block pasted as many times as it loops. If it loops infinitely i.e. while(true), you are trying to execute an infinite string of code which will freeze the thread.
ynot01 | 2022-09-04 00:35
I am using a kinematic body 2d
GodotUser21 | 2022-09-04 09:17
Can you post the new code you’re using, please?
Ev1lbl0w | 2022-09-04 10:05
func fall_damage_taken():
if fall_strength >= 100:
will_die = true
if fall_strength < 100 and not is_on_floor():
will_die = false
if will_die == true and is_on_floor():
pass #play death animation
get_tree().reload_current_scene()
print("fall damage taken")
will_die = false
this works fine but is it possible to use while loops for this?
GodotUser21 | 2022-09-05 14:49
It that works, I’d leave it at that. while loops are rarely used in the scenario you describe. Have you looked at the signal system in Godot? it’s the best way you have to “wait until something happens” in your game without freezing it with a while loop.