class_name PlayerStateMachine extends Node
@export var initial_state: State
var states: Dictionary = {}
@export var current_state: State
var current_state_name: String
@export var animatedsprite2D: AnimatedSprite2D
func _ready():
for child in get_children():
if child is State:
states[child.name.to_lower()] = child
child.state_transition.connect(ChangeState_to)
if initial_state:
initial_state.Enter()
current_state= initial_state
func _process(delta):
if current_state:
current_state.Process(delta)
#this function accepts parameter/argument of the new state
#we are calling a function, it will code, upon completion it will return a null or state
pass
func _physics_process(delta):
if current_state:
current_state.Physics(delta)
current_state.Next_Transitions()
pass
func _unhandled_input(event):
if current_state:
current_state.HandleInput(event)
pass
func ChangeState_to(state_name: String) -> void:
if state_name == current_state.name.to_lower():
return
var new_state = states.get(state_name.to_lower())
if !new_state:
return
if current_state:
current_state._on_exit()
new_state.enter()
current_state = new_state
class_name Idle_State extends State
@export var animated_sprite_2D: AnimatedSprite2D
@export var player: Player
var direction: Vector2
#what happens when the player enters state
func Enter() -> void:
pass
#what happens during the _process update in the state
func Process(_delta: float) -> State:
return null
#what happens during the _physics_process update in the state
func Physics(_delta: float) -> State:
direction = GameInputEvents.movement_input()
if direction == Vector2.UP:
animated_sprite_2D.play("Idle_Back")
elif direction == Vector2.RIGHT:
animated_sprite_2D.play("Idle_Right ")
elif direction == Vector2.DOWN:
animated_sprite_2D.play("Idle_Down ")
else:
animated_sprite_2D.play("Idle_Forward")
return null
#what happens with input events in the state
func Next_Transitions() -> State:
return null
#what happens whent the player exits state
func Exit() -> void:
pass
Question
I dont understand whats the actually problem is at the moment. Ive tried redoing the player state multiple times but I cant seem to understand the issue.