Artificial Intelligence for Games 101 – Finite State Machines

Previous Post: Introduction to Artificial Intelligence

A Finite State Machine is a model that simulates sequential logic. It comes from two models created by Edward F. Moore and George H. Mealy. It generally consists of a series of States of which one of them is active until an event is received, causing it to change to a different state. This could be used in a game to make an enemy Idle then when an enemy is spotted change to an attack state.

The first use for FSMs was the 1998 Cult Classic “Half Life” by Valve Software. This game would use Finite State Machines to incorporate humanlike action between NPCs, allowing Combines to properly “communicate” between each other and give an almost real-life combat between the Player and the enemies. The use of Finite State Machines was so well done, it is now just incorporated into basically every modern game since the early 2000s.

Finite State Machines have various ways of being shown, sometimes through the use of UML Diagrams though this is mostly for the developers to know what they are needing to do for the AI Behaviours. An example of a FSM Diagram is shown below:

A Finite State Machine used for Ghosts in PacMan

A Finite State Machine requires at least 2 states and some method to transition between each state. Some states may only be able to transition in one direction, whereas some can transition either way.
The Diagram below shows how Unreal Engine uses Finite State Machines to deal with Animations. In this example, Idle is attached to Entry (allowing it to actually start animating), which then has a transition between jogging and crouching. Note, Jogging isn’t attached to Crouching as people don’t tend to crouch and jog at the same time – so to get around this, they have a “Crouch Walk” animation. Similarly there is a Jump animation, then a “Run Jump” animation just to make sure they flow properly between each other.

Finite State Machine being used in the Animation Blueprint of Unreal Engine 4

A Behaviour Tree is another variation of the Finite State Machine, though this is more focussed on the action of the AI.

Behaviour Tree in Unreal Engine 4

Most modern Game Engines like UE4 and Unity have some form of in-engine FSM design system. As I am mostly using Unreal, I will likely use this as my frame of reference, and will talk about this further in my Games report for each AI Scenario for my Final Project. Unreal Engine has Blueprints and C++ to create an FSM, but Blueprints is easiest to use as it can visually show you each State.

What to Read Next

AI Algorithms: Dijkstra and A Star
AI Algorithms: Flow Fields
Navigation Meshes

3 thoughts on “Artificial Intelligence for Games 101 – Finite State Machines

Leave a comment