how do i get a VAR to increment/decrement by using a Func and more

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Traitors Avatar

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

:bust_in_silhouette: Reply From: jgodfrey

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

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

Traitors Avatar | 2020-04-13 21:09

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.

njamster | 2020-04-13 22:10

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)

Traitors Avatar | 2020-04-13 22:23

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.

njamster | 2020-04-13 22:35

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?

Traitors Avatar | 2020-04-13 23:08