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:

``````1
1
2
``````

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
else:
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
print(player.count_jumps)

if is_on_floor():
player.count_jumps = 0

#gravity
motion.y += player.gravity * delta
motion = move_and_slide(motion, Vector2.UP)
``````
in Engine

+1 vote

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 (21,864 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 =)