2D Character StateMachine

Hi everyone, I got stuck in this obstacle while scripting. I am watching a youtube playlist of Micheal Games and I am following step by step what he does to learn as much as possible, but I got stuck. Below I will attach images of the script, in one script it tells me, “Could not find type “State” in the current scope.”
While in the other script it tells me, “Could not find base class “State”.”
I tried double-checking that the names were right, I changed a few things but it just gave me more problems, how can I continue?
Thanks in advance for the answers






It would be easier if you pasted the script instead of screenshots. Try reloading the project, it is strange that class_name State is greyed out, maybe pick a more specific name like PlayerState

2 Likes

class_name Player extends CharacterBody2D

var cardinal_direction : Vector2 = Vector2.DOWN
var direction : Vector2 = Vector2.ZERO

@onready var animation_player : AnimationPlayer = $AnimationPlayer
@onready var sprite : Sprite2D = $Sprite2D
@onready var state_machine : Player_StateMachine = $StateMachine

Called when the node enters the scene tree for the first time.

func _ready():
state_machine.Initialized(self)
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process( delta ):

direction.x = Input.get_action_strength("Right") - Input.get_action_strength("Left")
direction.y = Input.get_action_strength("Down") - Input.get_action_strength("Up")
pass

func _physics_process( delta ):
move_and_slide()

func SetDirection() → bool:
var new_dir : Vector2 = cardinal_direction
if direction == Vector2.ZERO:
return false

if direction.y == 0:
	new_dir = Vector2.LEFT if direction.x < 0 else Vector2.RIGHT
elif direction.x == 0:
	new_dir = Vector2.UP if direction.y < 0 else Vector2.DOWN
	
if new_dir == cardinal_direction:
	return false

cardinal_direction = new_dir
sprite.scale.x = -1 if cardinal_direction == Vector2.LEFT else 1
return true

func UpdateAnimation( state : String ) → void:
animation_player.play( state + “_” + AnimDirection() )
pass

func AnimDirection() → String:
if cardinal_direction == Vector2.DOWN:
return “Down”
elif cardinal_direction == Vector2.UP:
return “Up”
else:
return “Side”

class_name Player_StateMachine extends Node

var states : Array [ State ]
var prev_state : State
var current_state : State

Called when the node enters the scene tree for the first time.

func _ready():
process_mode = Node.PROCESS_MODE_DISABLED
pass # Replace with function body.

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
ChangeState( current_state.Process( delta ) )
pass

func _physics_process(delta):
ChangeState( current_state.Physics( delta ) )
pass

func _unhandled_input(event):
ChangeState( current_state.HandleInput( event ) )
pass

func Initialized( _player : Player ) → void:
states =

for c in get_children():
	if c is State:
		states.append(c)
		
if states.size() > 0:
	states[0].player = _player
	ChangeState( states[0] )
	process_mode = Node.PROCESS_MODE_INHERIT

func ChangeState( new_state : State ) → void:
if new_state == null || new_state == current_state:
return

if current_state:
	current_state.Exit()
	
prev_state = current_state
current_state = new_state
current_state.Enter()

class_name State extends Node

Stores a reference to the player that this State belongs to

static var player : Player

func _ready():
pass # Replace with function body.

What happens when the player enters this State?

func Enter() → void:
pass

What happens when the player exits this State?

func Exit() → void:
pass

What happens during the _process update in this State?

func Process( _delta : float ) → State:
return null

What happens during the _physics_process update in this State?

func Physics( _delta : float ) → State:
return null

What happens with input events in this State?

func HandleInput( _event : InputEvent ) → State:
return null

class_name State_Idle extends State

What happens when the player enters this State?

func Enter() → void:
player.UpdateAnimation(“Idle”)
pass

What happens when the player exits this State?

func Exit() → void:
pass

What happens during the _process update in this State?

func Process( _delta : float ) → State:
return null

What happens during the _physics_process update in this State?

func Physics( _delta : float ) → State:
return null

What happens with input events in this State?

func HandleInput( _event : InputEvent ) → State:
return null