Godot Version
4.0
Question
I have been working on a FSM and am at a total loss for why my states wont switch, i have print statements to tell me when the states switch. What im trying to do is make it so that when my characters velocity.x > 0, it will switch to the walking state and if its not then it will go into the idle state
Here is my state machine
extends Node
@export var initial_state : State
var current_state : State
var states : Dictionary = {}
func _ready():
for child in get_children():
if child in get_children():
states[child.name] = child
child.Transitioned.connect(on_child_transition)
print(child)
if initial_state:
initial_state.Enter()
current_state = initial_state
func _process(delta):
if current_state:
current_state.Update(delta)
func _physics_process(delta):
if current_state:
current_state.Physics_Update(delta)
func on_child_transition(_assumed_current_state, new_state_name: String) -> void:
var new_state = states.get(new_state_name)
if !new_state:
return
if current_state:
current_state.Exit()
new_state.Enter()
current_state = new_state
Here is my state script
extends Node
class_name State
@warning_ignore("unused_signal")
signal Transitioned
func Enter():
pass
func Exit():
pass
func Update(_delta: float):
pass
func Physics_Update(_delta: float):
pass
Here is my PlayerWalk state
extends State
class_name PlayerWalk
@onready var state_machine = $".."
@onready var player_animated = $"../../PlayerAnimated"
@export var player = CharacterBody2D
func Enter():
player_animated.animation = "Running"
print("Walk")
func Physics_Update(_delta: float):
if player.velocity.x == 0:
print("Idle")
Transitioned.emit(self, "PlayerIdle")
Here is my PlayerIdle state
extends State
class_name PlayerIdle
@onready var state_machine = $".."
@onready var player_animated = $"../../PlayerAnimated"
@export var player = CharacterBody2D
func Enter():
player_animated.animation = "Idle"
print("idle")
func Physics_Update(_delta: float):
if player.velocity.x > 0:
print("Walk")
Transitioned.emit(self, "PlayerWalk")