What are State Machines?

Chase Williams
Developing Developers
2 min readNov 3, 2017

Finite State Machines are mathematical models that represent different “states” depending on a specific set of available inputs. The model can only be exist in one state at a time, and an external input will cause a transition to occur, with which the model switches over to another input.

An example would be a traffic light. These can be abstracted as a simple three-state machine: green, yellow, and red. The default state can be assigned to green, which will only transition when the sensors pick up cars in a perpendicular road (external input). The model will then transition to yellow for a predetermined number of seconds, and automatically transition to red. Red, similarly, then transitions to yellow and then green when given sensor feedback.

https://3.bp.blogspot.com/-Bgm-rjMuZwo/V2gRqVqNN9I/AAAAAAAAHpM/fuIXktohDu8g5NeBXXOgv7OhzmhGruhxgCLcB/s1600/traffic_light_FSM.png

In game design, state machines are often utilized in order to create AI for various enemies. This makes sense, as enemies often follow pre-scripted actions: remaining idle, attacking, following the player, fleeing, etc. By following the design principles of a finite state machine, AI design actually becomes a lot easier. For my game, I want to create a hover drone that attacks the player when he or she enters its vicinity. So, it’s default state will be idle, or perhaps attached to the ceiling. When the player enters its field of vision and is close enough (external input), it transitions to attack, in which it attempts to maintain a steady distance from the player and shoots at the player. That’s it — just two states can describe the actions of my hover drone enemy.

While it may not seem immediately useful to use state machines, by doing this, transitions can also beckon calls to, say, animator changes, sound effects, and cleaner code construction that makes for an all around quicker implementation process. Additionally, this design process supports much more complicated AI, such as a boss enemy, which may have several phases and thus many states. Controlling these all through a diagram of transitions and states helps makes the creation of said AI manageable.

--

--