Godot Version
Godot Version 4
Question
Hi guys this is a very stupid question . but I was experimenting with state machines and I stole this code from a tutorial https://www.youtube.com/watch?v=i0Y6anqiJ-g&t=264s now I didn’t understand it at first but I copied the code and started playing with it I understood the concept itself but I just cannot for the life of me figure out what i am doing wrong with the actual states themselves.
_____ State machine code checked with other videos and its pretty standard for a tutorial state machine
@iconiconiconicon(“res://Art/Icons/FSMSprite.png”)
extends Node
class_name FiniteStateMachine
var states : Dictionary = {}
var current_stat@export : St
@export ar current_state : State
@export var initial_state : State
func _ready():
for child in get_children():
if child is State:
states[child.name.to_lower()] = child
child.state_transition.connect(change_state)
if initial_state:
initial_state.Enter()
current_state = initial_state
#Call the current states update function continuosly
func _process(delta):
if current_state:
current_state.Update(delta)
#region State Management
#Use force_change_state cautiously, it immediately switches to a state regardless of any transitions.
#This is used to force us into a ‘death state’ when killed
func force_change_state(new_state : String):
var newState = states.get(new_state.to_lower())
if !newState:
print(new_state + " does not exist in the dictionary of states")
return
if current_state == newState:
print("State is same, aborting")
return
#NOTE Calling exit like so: (current_state.Exit()) may cause warnings when flushing queries, like when the enemy is being removed after death.
#call_deferred is safe and prevents this from occuring. We get the Exit function from the state as a callable and then call it in a thread-safe manner
if current_state:
var exit_callable = Callable(current_state, "Exit")
exit_callable.call_deferred()
newState.Enter()
current_state = newState
func change_state(source_state : State, new_state_name : String):
if source_state != current_state:
#print("Invalid change_state trying from: " + source_state.name + " but currently in: " + current_state.name)
#This typically only happens when trying to switch from death state following a force_change
return
var new_state = states.get(new_state_name.to_lower())
if !new_state:
print("New state is empty")
return
if current_state:
current_state.Exit()
new_state.Enter()
current_state = new_state
_____
Lets say i have two states Idle and Running, now do i make their class_names both States? I tried that and there was a (I am guessing) class naming error because you cant name both the same class so what am I doing wrong ?
here is the state code and its the same for both of them :
class_name State
extends Node
@export var parent : Player
@onready var animations = parent.get_node(“animations”)
@export var animation : String
func Enter():
animations.play(animation)
func Exit():
pass
signal state_transition
func Update(_delta:float):
pass
For both running and idle state its the code above