How to make a finite state machine

A finite-state machine (FSM) is a mathematical model of computation used to design both computer programs and sequential logic circuits. It is conceived as an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. A particular FSM is defined by a list of its states, and the triggering condition for each transition. -Wikipedia

if your control scripts are a hot mess of nested if statements and bool toggles, then an FSM is for you.

A state machine works by having a bunch of different states that it can transition to. each of the states can have different behaviours. An example of this is an automatic door. While it is closed, it can either stay closed or open, but while it is open it can only stay open or close. Another example is in a pet game. It may have an ‘energy stat’ that is constantly having an amount taken away from it every second. While the pets’ FSM is in the ‘idle’ state, this number could be 1. While it is playing, this number could be 5 (as it is exerting energy) and while it is sleeping, the number could be -2 (regaining energy). When your entities behavior starts to get more and more complex, the use of one or several FSMs becomes more and more necessary. They are also very useful for menu’s, and if you use Unity, you may have seen one already (Mechanim, Unity’s built-in animation control system)

So let’s draw out a simple state machine for an automatic door. Doing this is good practice, as a larger state machine will very quickly grow to a size larger than your head can contain.

Draw out your state machine on paper. Start by drawing each state that the machine’s entity can be in. For a door, this would be Open, Opening, Closed and Closing. Then write down the Inputs that the machine will receive. For my example, this would be from the sensor, and would be ‘Detected movement’ and ‘Lost movement’ (or simply signal on/off). There is also Input from the door itself, once it has completed the task of opening and closing, which we will name ‘Completed’. Now for each state, we draw transition lines to other states, based on those inputs. If the door is Open, and we get the ‘Lost movement’ input, the door should close, so we transition to Closing. once we are in Closing, and the task of closing has finished, we get the Completed input, so we make a transition line from Closing to Closed, with the Completed input. If we are in the Closed state, and we get theDOORfsm ‘Detected movement’ input, the opposite happens. If we are in the Closing state and receive the ‘Detected movement’ Input, we can transition directly to the Opening state, and vice versa. Here is what it should look like by now:

Once you have that down, you need to make up the framework for an FSM, or download one someone else has already prepared. Once you have that, you can set up all the states and transitions in the same way that you drew out on paper.

This is a framework for an FSM for use in unity, with an example on how to use it.

Here is a step by step guide on creating a state machine by Jesse Warden

Advertisements

2 thoughts on “How to make a finite state machine

  1. Not only did that explain the problems I had with FSM’s in general, it was also a great read. Great links at the end there as well, definitely bookmarking for future reference!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s