Design Patterns: State Pattern

The State Design Pattern revolves around controlling behaviour through the use of a currently active state, rather than individual conditional checks.

Vincent Taylor
2 min readFeb 25, 2022

Today’s Objective: Give a brief overview of the State Design Pattern in video game development.

States Overview:

A State-based software design means that the behaviour of that piece of software is determined based on the currently active state from a predefined set of states. These states are for the general behaviour groups that the software can be in. For example, in a video game the player could be in a “Running” state, a “Walking” state, or an “Idle” state. The behaviour determined by the active state would determine how fast the player moves and what animation plays.

An obvious example from Unity of a State Pattern or a “State Machine” is Unity’s Animation System, which uses animations as state nodes, and transitions between those nodes to switch the current animations.

Transitions Overview:

For a State pattern to function, the active state needs to change. For that purpose, there are usually transitions between states which determine how and when the active state can change to a different state.

These transitions can go both or just one way, and can have a number of conditions attached to them, indicating when the transition can occur. In Unity, some common transition conditions are:

  • An animation clip finishes playing.
  • A destination/goal is reached.
  • An event occurs.
  • A player input is detected.

Types of State Patterns:

Apart from the basic State Pattern where a single layer of states controls behaviour, there are other forms of the State Pattern that function slightly different.

For example, the Hierarchical State Pattern or Sub-State Pattern. This pattern is like the normal State Pattern, but each state can have sub-states which operate mostly the same but with key differences.

--

--

Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.