Can't decrement a double jump variable inside of function

Godot Version v4.3.stable.official [77dcf97d8]

Question

I am attempting to implement a double jump feature for a 2d platformer. I’m trying to decrement a “current_air_jumps” variable inside of my jump function, but the value is reset every time the function is called. Any way I can fix this?
During testing, I have infinite double jumps and the print(current_air_jumps) returns 0 every time I double jump.

extends CharacterBody2D

@export var movementdata : PlayerMovementData
@onready var animated_sprite_2d =$AnimatedSprite2D
@onready var coyote_timer = $CoyoteTimer

func _physics_process(delta: float) → void:
apply_gravity(delta)

var direction := Input.get_axis("ui_left", "ui_right")
var jump := Input.is_action_just_pressed("ui_accept") or Input.is_action_just_pressed("ui_up")
var jump_release := Input.is_action_just_released("ui_accept") or Input.is_action_just_released("ui_up")

var current_air_jumps = 1

handle_jump(jump, jump_release, current_air_jumps)
handle_acceleration(direction, delta)
handle_air_acceleration(direction, delta)
apply_friction(direction, delta)
apply_air_resistance(direction, delta)
update_animations(direction)
handle_wall_jump(jump, jump_release, direction)

var was_on_floor = is_on_floor()
move_and_slide()
var just_left_ledge = was_on_floor and not is_on_floor() and velocity.y >= 0
if just_left_ledge:
	coyote_timer.start()

func handle_jump(jump, jump_release, current_air_jumps):

if is_on_floor() or coyote_timer.time_left > 0.0:
	current_air_jumps = movementdata.air_jumps
	if jump:
		velocity.y = movementdata.jump_velocity
if not is_on_floor():
	if jump_release and velocity.y < 0:
		velocity.y = velocity.y / 3
		
	if jump and current_air_jumps > 0:
		current_air_jumps -= 1
		velocity.y = movementdata.jump_velocity * 0.8
		print(current_air_jumps)

Make the variable a member variable of your script by putting it outside of any function. Currently it’s a local variable.

1 Like

Thanks man! I had it in the handle_jump function before I posted the issue and moved it in to the _physics_process function instead of at the top smh.
The issues fixed now with my variable out here:

extends CharacterBody2D

@export var movementdata : PlayerMovementData
@onready var animated_sprite_2d =$AnimatedSprite2D
@onready var coyote_timer = $CoyoteTimer
var current_air_jumps = 1

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.