0 votes

In max_jumps I set the maximum number of jumps. But in the end he jumps 3 times, that is, he will always jump 1 time more than specified in max_jumps. Wtf?

Print output if I pressed UP button 3 times:


Player script:

extends KinematicBody2D

var motion = Vector2()
var player = {
    "speed": 20000,
    "gravity": 2500,
    "jump": -40000,
    "count_jumps": 0,
    "max_jumps": 2

func _physics_process(delta):
    # movement
    if Input.is_action_pressed("ui_left"):
        motion.x = -player.speed * delta
    elif Input.is_action_pressed("ui_right"):
        motion.x = player.speed * delta
        motion.x = 0

    # jump
    if Input.is_action_just_pressed("ui_up") && player.count_jumps < player.max_jumps:
        player.count_jumps += 1
        motion.y = player.jump * delta

    if is_on_floor():
        player.count_jumps = 0

    motion.y += player.gravity * delta
    motion = move_and_slide(motion, Vector2.UP)
in Engine by (15 points)

1 Answer

+1 vote
Best answer

The fact that you get 2 1's in your output is a clue to the problem. Looking at your code, I assume this is what's happening...

  • count_jumps starts as 0
  • You jump...
  • count_jumps increments to 1 (and prints the first 1 in the output)
  • Next, you check is_on_floor(). But, that's only updated after calling move_and_slide, which you haven't done yet). So, I'd guess that returns true and you reset count_jumps to 0.
  • Now, you call move_and_slide(), which updates is_on_floor() to return false
  • Now, you jump again
  • That increments count_jumps to 1 (again, and prints 1 again)
  • The next is_on_floor() check (correctly) returns false, so you don't reset count_jumps. From this point forward, things work as you expect, but you've already gotten an extra, first jump due to the incorrect call order of move_and_slide() and is_on_floor().

Bottom line, you need to call move_and_slide() before the result of is_on_floor() is accurate. So, you'll need to refactor / reorder some of your code.

by (13,794 points)
selected by

thank! while I was waiting for an answer, I already guessed this myself, moved the section with the code is_on_floor() higher than the jump check code and everything worked =)

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.