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.

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.

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.

  • 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.

--

--

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vincent Taylor

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