State Machine - What's the way to change state from any walking state to damage state

Godot Version

Godot 4.2.1

Question

Hello everybody!

I’m learning the state machine pattern, and I’m confused about how to change to a state that can be used in almost any other states.

For example, I have a character that can move around, idle, walk, run, jump, etc. This character can take damage from enemies.

I’m getting confused about how to switch to the damage state when taking damage. Who is responsible for monitoring the damage inflicted and changing to the damage state?

Thank you for your help :pray:

Personally I take a approach there the node or enemy handles its own status . The script that resides there need to have its state machine . Maybe have a signal come in and that changes the state from 1 to 2 ( 2 being the state of damage ) .

Just get a notepad and declare all your states , either with numbers or strings .

Then in some point in the code use a case statement to switch to that state .

2 Likes

Is the pattern allowing a parent script to change the state of the state machine?