Topics / Building a game

Core concepts

State machine

A state machine is a way to say a thing is in exactly one state at a time (idle, walking, talking, sleeping) with rules for moving between states. It keeps character behaviour from turning into a tangle of ifs.

Watch: see it in 30 seconds

Video coming soon
A 30-second visual explanation

Why it matters for your game

  • 'The villager should wander, then go home at night, then sleep' is a state machine. Describe behaviour as states and transitions and your AI will build it cleanly.
  • Animations follow states: the walk cycle plays in the walking state, stops when the state changes.
  • When a character gets stuck doing two things at once (walking while sleeping), a state was entered without leaving the previous one.

How it comes up when you talk to your AI

  • 'States', 'transitions', 'enter and exit', 'animation state machine' or 'animator', 'behaviour tree' (the bigger cousin for complex AI).
  • Ask your AI to list a character's states before writing the code; the list is the design.

Ask your AI about it

Paste this into the assistant you build with. It is written to get a useful answer for your own project, not a textbook page.

Explain state machines for game characters with a villager that wanders, works, goes home and sleeps. Draw the states and transitions as a list, then show how the animation is tied to each state.

Related terms