Tutorial

Finite State Machines [Part 2]

Deeper inside FSM

Finite State Machines are a sequential logic that are very low level. They can be used for simple decision-making logic. Let’s give a human example of an FSM:

  • Input: Flips the switch
  • State: Light Bulb is in the state of “On”
  • Output: Light Bulb will now produce light for the room

Inputs are any form of stimulus from the user that can trigger a change of state based on the condition needed to perform that states. Think about switching a light on:

States as you can see, describes the current condition of the FSM. Until the user makes another input, the light bulb won’t change its state to “Off”.

Outputs are linked to the FSM’s state depending on how the FSM is programmed by the programmer.

FSM Implementation

This is my way of implementing a dynamic FSM in Unity. This will require us to have the main FSM class per object that holds the states; FSM State for the object that holds the actions;  and FSM Actions that does the outputs of the state.

Now that we’ve introduced the FSM to you, let’s now do some scripting. Let’s do them one by one. First, let’s organize our unity project.

fsm1It’s up to you where to place them. I always separate my game-only-assets to other assets. We’re going to put this FSM in our common folder(I always have the “Common” folder because I have my own library that I use in every games) because this FSM system will be dynamic and you will be able to use everything to every games.

The reason why we have a folder for actions is to compile all the actions that we make, and create a library of actions for other kinds of games.

Let’s now create our main FSM class, and put it in the FSM folder under common, and let’s call it FSM.cs.

FSM.cs

Couple of things, I’m a namespace fanatic and it’s a great practice to segregate your codes if you’re not using any of them. Again, I have my Common library which is why I placed it under Common. You can create your own namespace if you want to.

As you can see, we’re not inheriting from MonoBehaviour class because we will have an update function that will be called under the AI’s update instead to update the FSM.

Now let’s make some variables and functions for our FSM.

Just a tip, if you think that the code is starting to look dirty because of all the summary, just enable the code colding of monodevelop. Just go to Tools/Options/Text Editor/General, and check “Enable Code Folding”

1f6YhKn0Eab-kWruxMd5SxtZwbhJ3QJ0etcD6jlg

Now we can fold the summaries in the code itself.

142BcsOI5X_HbKXwAr70JOlyX13ApnXzOxFkFyZM

Before we go any further, let’s create the FSMState, and FSMAction because we need to create functions in the FSM that will return them. Let’s start with the FSMAction.

FSM Action Impementation

FSMAction will be the base class of our actions, hence the virtual functions. If you’re not familiar with virtual functions, here’s a Unity Tutorial that talks about Polymorphism.

Initial FSM State Implementation

Let’s now start the FSMState and let’s call this FSMState.cs:

Now that we’ve started on everything, let’s finish up the FSM class.

Finishing FSM Implementation

Let’s add the ff variables in our class.

The name of course will be the name of the FSM. The stateMap will contain a key which will be the event id of the FSMState, and the FSMState itself so that we could bind them together, and transition to another state via SendEvent() function.

Of course the name of the FSM.

We will create an action processor. This will enable us to dynamically call different actions that is inside the state, and do the action. If you’re not familiar with delegates, refer to this.

Let’s now fill up the constructor that we did before and make an initializer for our FSM.

Constructors as you know, makes creating instances of classes easier and much more clean, and less coupled since the variables of this class are set to private. Keeping unnecessary variables unseen for the other programmers.

We will now add a FSMState parameter to ChangeToState(), EnterState(), and ExitState()

Now it’s important to check the states to avoid jumbling them and avoid errors.

Enter, Exit, and Update will be the same and will be handled by our action processor.

Finishing FSM State Implementation

Now that we’ve created our loop. Let’s start writing our FSMState. First its constructor.

Before we go any further, let’s talk about transitions. Transitions by its name, is the change between two states. We’ll use a string to act as a key to change between states. All of these are contained in a dictionary.

Now let’s make a function that adds actions to our state.

Let’s now add the event handlers for the FSM, and the FSMState. Let’s first go back to the FSM.cs and add new functions to the class.

Let’s finalize the FSMState by adding implementations for event handling.

Lastly, let’s create a function that will automatically create states for us under FSM.cs.

By calling this function, this will enable us to create States without creating on the AI class itself.

So there you have it. Our FSM system is now done. Look out for our next Blog tutorial for the implementation. Please do comment if you have questions regarding this topic.

On the next and final part of this tutorial, we will make two kinds of implementations for this FSM engine that we made. We will create one AI class that will write to our console indefinitely, and another AI that will move AND write to our console indefinitely.