The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

I am trying make a character take fall damage

my code:

velocity.y += gravity * delta
        fall_strength += 1
        if fall_strength >= 120:
            print("fall damage taken")

but if I do that when that, my character hasn't touched the ground and I took fall damage. I was thinking of a repeat until so

if fall_strength >= 120:
            repeat_until is_on_floor:
                        print("fall damage taken")

but there isn't this function on godot.
So how should i go about this???

Godot version v3.4.4
in Engine by (32 points)

1 Answer

0 votes

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 getfloorvelocity():

if is_on_floor() and get_floor_velocity().length >= 120:
    print("fall damage taken")
by (286 points)

Thanks so much

this actually lags the whole game and the game will freeze

Did you do the second alternative? The first one will most likely freeze your game.

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.

I am using a kinematic body 2d

Can you post the new code you're using, please?

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
    print("fall damage taken")
    will_die = false

this works fine but is it possible to use while loops for this?

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.

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.