REM/scripts/state_machine/state_machine.gd
2026-03-26 20:37:29 -06:00

34 lines
822 B
GDScript

class_name StateMachine
extends Node
@export var CURRENT_STATE: State
var states: Dictionary = {}
func _ready():
for child in get_children():
if child is State:
states[child.name] = child
##child.transition.connect(on_child_transition)
else:
push_warning("incompatible child in state machine")
CURRENT_STATE.enter()
## Does update need the delta? Godot claims update does not accept any arguments
func _process(_delta):
CURRENT_STATE.update()
func _physics_process(delta):
CURRENT_STATE.physics_update(delta)
func on_child_transition(new_state_name: StringName) -> void:
var new_state = states.get(new_state_name)
if new_state != null:
if new_state != CURRENT_STATE:
CURRENT_STATE.exit()
new_state.enter()
CURRENT_STATE = new_state
else:
push_warning("State does not exist")