Hello everyone
I’ve been googling for about a week now and I still can’t fully understand how state machines truly work ,
Let’s say you have an Idle, Move, Crouch states for your character,
State machine’s main principle is that it can be in only one state at a time, So:
let’s say you are in the Idle state; if the player pressed the ctrl key you want to switch to the Crouch state,
the problem is that you require movement while Crouch state is active so how should Crouch and Move be active at the same time.
And I found something called Hierarchical FSM that makes the Idle, Move, Crouch states implement a state called Grounded but I still can’t understand how if the player is not Grounded that the children states (Idle, Move, Crouch) will not be also active.
And actually not just the Crouch state requires movement while it’s active (Jump, Attack and many others also).
Any help is appreciated.
You always solve problems like this in FSMs by either adding states or redefining states. You are correct you cannot be in move state and crouch state at the same time. Instead you would add some additional states to your FSM:
- Idle
- Moving
- CrouchedIdle
- CrouchedMoving
Now you’ve broken the Crouch state into CrouchedIdle and CrouchedMoving states. Each of these states has its own animation and transition rules.
There is no limitation that “the character is only allowed to move in a single state”.
Thanks for you reply.
But doesn’t that make my code very repetitive?
Coding the same movement code in every state.
Just put the movement code into a method and reuse that same method in the two states. Let it take a movement speed parameter or whatever if they need to have different speeds etc…
Thank you man I’ll see what I can do.
A good introcution to FSM’s gives the book "Programming Game AI by Example " by Mat Buckland.
A shortened version of the FSM-chapter can be read online.
1 Like