Trying to set up a state machine controlled character

Godot Version

4.3

Question

Hey there, I am an animator trying to make a demo to showcase my asset packs, but as I was following a tutorial. I have somehow ended up with my player seemingly idling indefinitely, ignoring both physics and inputs. I have revised and compared my code with the videos version some 4 or 5 times at this point, so I now come to you for help.

I suspect that the error is in either the player script:

class_name Player
extends CharacterBody2D

@onready var animation = $AnimationPlayer
@onready var state_machine = $StateMachine

func _ready() -> void:
	#Initialize the state machine, passing a reference of the player to the states,
	#that way they can move and react accordingly
	state_machine.init(self)

func _unhandled_input(event: InputEvent)-> void:
	state_machine.process_input(event)

func _physics_process(delta: float) -> void:
	state_machine.process_physics(delta)

func _process(delta:float) -> void:
	state_machine.process_frame(delta)

the state machine script:


extends Node

@export var starting_state:State

var current_state: State

#initialize the state machine by giving each child state a reference to the
#parent object it belongs to and enter the default starting_state
func init(parent: Player) -> void:
	for child in get_children():
		child.parent= parent
	
	#initialize to the default state
	change_state(starting_state)

#Change to the new state by first calling any exit logic on the current state.
func change_state(new_state: State) -> void:
	if current_state:
		current_state.exit()
	
	current_state = new_state
	current_state.enter()
	
#pass through functions for the Player to call,
#handling state changes as needed.
func process_physics(delta: float) -> void:
	var new_state = current_state.process_physics(delta)
	if new_state:
		change_state(new_state)

func process_input(event: InputEvent) -> void:
	var new_state = current_state.process_input(event)
	if new_state:
		change_state(new_state)

func process_frame(delta: float) -> void:
	var new_state = current_state.process_frame(delta)
	if new_state:
		change_state(new_state)

or the state script:


class_name State
extends Node

@export var animation_name: String
@export var move_speed: float = 800

var gravity: int= ProjectSettings.get_setting("physics/2d/default_gravity")

#hold a reference to the parent so that it can be controlled by the state
var parent:Player

func enter()-> void:
	parent.animation.play(animation_name)

func exit()-> void:
	pass

func process_input(event: InputEvent) -> State:
	return null

func process_frame(delta: float) -> State:
	return null

func process_physics(delta: float) -> State:
	return null

I really appreciate any help or input.

First I would recommend Godot’s animation tree it is far superior.

But looking at your code, It looks like you don’t implement event handling on the state node.

# player
func _unhandled_input(event: InputEvent)-> void:
	state_machine.process_input(event)

# state machine 
func process_input(event: InputEvent) -> void:
	var new_state = current_state.process_input(event)
    if new_state: # <-- always null, always false
		change_state(new_state)

# state
func process_input(event: InputEvent) -> State:
	return null

Since state returns null no matter what event, the state won’t change.

It looks like the same issue for physics too.

1 Like

Oh, I’ll definetly take a look at using Animation Tree. Thank you so much for the response!