That would be weird but does?? seem like it’d be the problem at this point… Tried to fiddle with it a bit further with that in mind but still couldn’t get it to work, so chose to move on and attempt with the functions you suggested. Which after a few mistakes I did get it to work for the most part and this is genuinely a lot less complex and easier to read, assuming I set it up mostly right ha!
Yeah most of what I’m making right now is probably more complex than it needs to be, I’m still pretty new to GDScript and code as a whole, but this is helping a lot I appreciate it greatly.
getting my mind more open to how flexible this stuff really is lol
This is what I currently have now with the new stuff implemented. The only things I’m having a problem with now are where I should calculate gravity, if I put move_and_slide() in the right place, and considering how to keep left/right movement available while in the jump states (unless you simply include the direction calc in the jump state along with velocity.y = jump) since I noticed once you’re in it you just are stuck at whatever angle you were going at while moving ha
With gravity specifically I’m mostly uncertain since I’d assume in the apply_state function, but since apply_state doesn’t have delta in it’s prams it doesn’t like it and I don’t have the knowledge to know how it’d be done differently. Did put it in _physics_process once just to see if the jump/double jump was recognized and can at least say it is now HA just with the side effect of once you’ve jumped you will simply go flying off into space.
extends CharacterBody2D
# Conditional variables
var can_double_jump = true
var can_sprint = true
var walk_speed = 400
var sprint_boost = 600
var jump = -400.0
# Available states
enum States {idle, walk, sprint, jump, doublejump}
var active_state = States.walk
func change_state(new_state):
if new_state != active_state:
print("changed state from %s to %s" % [States.find_key(active_state), States.find_key(new_state)])
active_state = new_state
func _physics_process(delta: float) -> void:
if !is_on_floor():
velocity += get_gravity() * delta
set_state()
apply_state()
# match the state you should be in and switch states
func set_state():
match active_state:
States.walk: # walk state and where it can go
if Input.is_action_just_pressed("jump") and is_on_floor():
change_state(States.jump)
if Input.is_action_pressed("Sprint") and can_sprint == true:
change_state(States.sprint)
States.sprint: # sprint stte and where it can go
if Input.is_action_just_pressed("jump") and is_on_floor():
change_state(States.jump)
if !Input.is_action_pressed("Sprint"):
change_state(States.walk)
States.jump: # jump state and where it can go
if Input.is_action_just_pressed("jump") and can_double_jump == true:
change_state(States.doublejump)
if is_on_floor():
change_state(States.walk)
States.doublejump: # double jump state and where it can go
if is_on_floor():
change_state(States.walk)
# match the state behavior
func apply_state():
match active_state:
States.walk: # walk state behvaior
var direction := Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * walk_speed
else:
velocity.x = move_toward(velocity.x, 0, walk_speed)
States.sprint: # sprint state behavior
var direction := Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * sprint_boost
else:
velocity.x = move_toward(velocity.x, 0, sprint_boost)
States.jump: # jump state behavior
velocity.y = jump
States.doublejump: # double jump state behavior
velocity.y = jump
move_and_slide()
also realized while typing I could probably have the direction var in the apply_state function so it doesn’t need to be duplicated in both walk and sprint state but regardless of that; It definitely is to start a lot more legible, thank you 
EDIT: did some further messing around to try and figure stuff out myself
added delta to the prams of apply_state, realized why it didn’t like it was me not adding it to the use of apply_state in change_state (oops lmao), so I have the gravity calc + move_and_slide() in apply_state now
then added a if !Input.is_action_just_pressed(“jump”) to the jump state in set state, and that now has it all working
Not sure if it could be done better or differently though so still curious! but wanted to note I got it working fully in some way (plus that fixed the movement issue from above HA)