+1 vote

Lets say i wanted my character to be able to jump a certain amount of times while in the air

var jumps = 2
var jump_dec = -1

if is_on_floor() and Input.is_action_just_pressed("move_jump"):
    velocity.y = jump_height
if not is_on_floor() and Input.is_action_just_pressed("move_jump"):
    velocity.y = jump_height
    jump_dec - jumps and print(jumps)

and when the character is out of jumps, this input("move_jump") is deactivated until the character is on the floor again

my mind is so exhausted trying to figure out why i can get the number to go down

in Engine by (30 points)
edited by

1 Answer

0 votes
Best answer

Typed directly into the forum response and untested, but something like this should work I think...

var maxJumps = 2
var remainingJumps = maxJumps

func _process(delta):
    if is_on_floor():
        remainingJumps = maxJumps
    if Input.is_action_just_pressed("move_jump"):
        if remainingJumps > 0:
            remainingJumps -= 1
            velocity.y = jump_height
by (10,936 points)
selected by

the number is staying a 2 when i tell it to print remaining jumps

I don't even how to fix this problem

the number stays at 2

the number stays at 2

You still need to call move_and_slide(velocity). Otherwise your character will stay on the floor, thus remainingJumps will be set to maxJumps (i.e. 2) each frame.

im not sure exactly what you mean but ive been able to jump

my character only has 2 air jumps, 3 jumps in total.
the print only happens when in the air and jumping yet it still stays 2

did i miss something?

func _process(delta):
var maxJumps = 2
var remainingJumps = maxJumps

if is_on_floor():
    remainingJumps = maxJumps
if is_on_floor() and Input.is_action_just_pressed("move_jump"):
    velocity.y = jump_height
if not is_on_floor() and Input.is_action_just_pressed("move_jump"):
    if remainingJumps > 0:
        remainingJumps - 1
        velocity.y = jump_height
        print(remainingJumps)

Whatever you put into _process is called each frame. If you put this part:

var maxJumps = 2
var remainingJumps = maxJumps

in, then you're resetting the counter to 2 each frame. Take a closer look at jgodfrey's answer: the variables are defined outside of the method!

Also you're doing

remainingJumps - 1

instead of

remainingJumps -= 1

Both lines are subtracting 1 from remainingjumps, but the latter is saving the result whereas the first does absolutely nothing with it, thus has no effect.

oooooh, now i know

its working now (its counting down)

1 question

is there a code i can write that can disable a input (temporarily) until i touch ground?
or is that not possible yet?

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 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 webmaster@godotengine.org with your username.