Godot Version
4.3
Question
I can’t seem to figure out a logic error that is occurring with the state machine I am trying to make for my game, apologies for the long post but there is a lot of logic involved to get into the issue.
the issue I am getting is that when the player is in the ground state and jumps the transition from the ground state to the air state is not happening, from debugging I can see that the Air state node is being called as the next_state however the player can jump infinitely which shouldn’t be happening, I am very new to coding in gd script and can’t seem to figure out why this is happening. There are no other syntax errors and this is the only logic error. below I will attach the relevant code however I am happy to attach any more code that will help with solving the issue. Thank you in advance!
extends Node
class_name CharacterStateMachine
@export var character : CharacterBody2D
@export var current_state : State
var states : Array[State]
func _ready():
for child in get_children():
if(child is State):
states.append(child)
#sets the states up with they need to function
child.character = character
else:
push_warning("Child " + child.name + "is not a state")
func _phsyics_process(delta):
if (current_state.next_state != null and current_state.next_state != null):
switch_states(current_state.next_state)
current_state.state_process(delta)
func check_if_can_move():
return current_state.can_move
func switch_states(new_state : State):
if (current_state != null):
current_state.on_exit()
current_state.next_state = null
current_state = new_state
current_state.on_enter()
func _input(event : InputEvent):
current_state.state_input(event)
extends State
class_name GroundState
@export var jump_strength := 300.0
@export var air_state : State
# Reference to the state machine
var state_machine : CharacterStateMachine
func state_input(event: InputEvent):
if (event.is_action_pressed("Jump")):
jump_test()
func jump_test():
character.velocity.y = -jump_strength
print(next_state)
next_state = air_state
the issue is here as when next_state is printed it says that the Air node is being referenced however it is not actually entering the air state
extends Node
class_name State
@export var can_move : bool = true
var character : CharacterBody2D
var next_state : State
func state_process(delta):
pass
func state_input(event : InputEvent):
pass
func on_enter():
pass
func on_exit():
pass
extends Label
@export var state_machine : CharacterStateMachine
func _process(delta):
text = "State : " + state_machine.current_state.name
in this state label it doesn’t transition to the air state