0 votes

I'm having issues with the while loop
More details
I'am trying to make a game and the player can change their scale.x and scale.y
Having normal animations in this case wouldn't be possible
So i did it with code but when i press the jump key the game ceashes
The code:

func  start_jump_anim():
     if Input.is_action_just_pressed("jump"):
          while jump_max_scale_x > jump_min_scale_x:
               self.scale.x += jump_anim_scale_x_increasing_speed
               while self.scale.x == jump_max_scale_x + self.scale.x:
                    end_jump_anim

func end_jump_anim():
     self.scale.x –= jump_anim_scale_x_changing_speed
     if self.scale.x == self.scale.x – jump_max_scale_x:
          self.scale.x = self.scale.x
Godot version 3.4
in Engine by (77 points)

1 Answer

+1 vote
Best answer

Consider using a Tween instead of modifying the properties manually; it is significantly less tedious and you also get some nice features such as non-linear interpolation, which is often times more visually pleasing.

If using a Tween is not an option, you should note that the entirety of the while loop runs inside a single frame; as such, the effects would occur instantaneously, not over a period of time. Consider something like this instead:

enum jump_states { NONE, INCREASING, DECREASING }
var jump_state = jump_states.NONE

func _on_jump_pressed():
    jump_state = INCREASING

func _physics_process(_d):
    if jump_state == jump_states.INCREASING:
        if scale.x < jump_max_scale_x:
            scale.x += jump_anim_scale_x_increasing_speed
        else:
            jump_state = DECREASING
    elif jump_state == jump_states.DECREASING:
        if scale.x > jump_min_scale_x:
            scale.x -= jump_anim_scale_x_changing_speed
        else:
            jump_state = NONE

(more or less pseudocode, you will have to adapt it to fit your needs)

Note that appending self. in front of local variables is unnecessary unless you are trying to call a setter or getter. Also note that function calls need the () to run.

by (1,295 points)
selected by

I totally forgot about the tween node
Anyway thx

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.