🎮Finite State Machine (FSM)
Also known as FSM, State Machine, Finite-State Machine, Finite Automaton
A thing that is always in one of a few states. Rules say when it changes.
Think of it like
Think of it like a traffic light. It is always in one colour, and it changes by fixed rules.
Example
Jack's enemy has three states. Patrol becomes chase when it sees the player, and chase becomes attack when close.
Why it matters
It keeps behaviour simple to read and debug. You always know what state a thing is in.
Definition
Example code
python
if state == "patrol" and sees_player:
state = "chase"
elif state == "chase" and close:
state = "attack"Common mistake
Key takeaways
- 1One state at a time. It is always in exactly one state.
- 2Clear rules. Each change has a reason.
- 3Not just games. Menus, parsers and traffic lights use them.