DE EN FR ES IT

Game AI & real-time strategy

Finite state machine (FSM)

Also: FSM, state machine

A finite state machine (FSM) models behaviour as a finite set of states, exactly one of which is active, plus rules that switch to another state when certain events occur. In games it classically drives enemies (“patrol”, “chase”, “attack”), but also menus, animation and game flow.

Background

State machines are the oldest and most widespread form of game AI. They are easy to understand, easy to debug and fast: each state has its own code for enter, update and exit, and a table defines which event leads where.

As the number of states grows, though, the web of transitions gets tangled, because every new state needs links to many old ones. The usual remedies are hierarchical state machines, where states contain machines of their own, and pushdown automata that remember where to return after an interruption. For complex behaviour, behaviour trees and utility AI later became popular.

Typical bugs are states with no way out, transitions that flip back and forth within the same tick, and forgotten clean-up on exit, such as an animation that keeps playing.

How we do it

The Wurzelwerk engine’s runtime is itself a state machine. It always has exactly one pending state: speak a line, walk, chapter card, dialogue with choices, prompt puzzle, ending, or nothing (ready for the next input). The renderer only reads that state and sends back commands such as interact, choose or advance.

Because the logic is cut so cleanly, the very same runtime also runs without graphics in automated playtesting. And because game progress consists of flags, inventory and current scene, the whole state space of a game can be searched. Our founder also knows state machines for units from his work as an AI developer for real-time strategy in 2000.

Related terms