Finite State Machine

In "Stilla Natt" we knew we wanted a better system for generating AI behavior than in our previous project, "Spite". We wanted a system where it would be easy to add, remove, and rearrange behaviors. This is where my journey into the Finite State Machine (FSM) begun! The system consists of two main parts: the State Machine and the Action Manager.

This is an overview of how a state executes in my FSM.

State Machine

The state machine keeps track of which state we are in, and checks for any transitions into other states. Its job is to see whether a transition has been triggered or not, and return Actions to run accordingly. All actions, states, and transitions are linked together similar to a linked list. This makes it easy to both iterate through the list, and to manipulate the order of the list as the transitions execute and states change.

States, Actions, Transitions, and Conditions

A state consists of a number of actions set to execute in a predefined order, along with one or more transitions. The transition holds a target state to transition into, along with one or many conditions it checks to see if it is allowed to transition into the next state. These conditions can be very simple, for example a bool check. When all required conditions are met, the transition will tell the system which state to transition into. The system then executes the according Exit Actions, and transitions into the next state.

The actions can be run in succession, or simultaneously, by adding them to different compounds. The most common are Selector, Sequence, and Combination.

The State class

The Action class

The Transition class

The base Condition class

Example of a bool condition

Action Manager

The Action Manager takes in the action(s) that the state machine returns, checks if the actions has been interrupted (can be from a transition triggering), adds all actions that can run to a linked list, then runs all of the active actions in order. Here the benefit of keeping actions in a linked list really shines!

Things to improve

The State Machine runs great, but it's missing a way for our Level Designers to create states and actions. In the near future, I will be adding a visual node system to the State Machine. It can get tedious on larger projects when you have to add everything together with code, as seen in the state below.