What is a finite-state machine (FSM) exactly?
A finite-state machine, also called FSM, is a mathematical model. It can be in only one state at a time, among a finite number of states.
If you need an example of a finite-state machine: in our case for Million Lords, to attack a city, you need to:
- Tap on your city
- Chose the “attack” option
- Select the city you want to attack
- Select the number of units you want to send.
Before each of these actions, you are in a different state. To change state, you need to satisfy a precise condition, like push a button, wait for a certain amount of time, etc …
Each state can correspond to different configurations, such as:
- stopping the camera from moving,
- allowing the camera to move,
- closing all open windows,
- and so on…
There can be many more, that you can activate on the arrival to the state, or as you leave the state.
In a nutshell, a finite-state machine is an ensemble composed of different states, each one containing entry, execution and exit properties, as well as one or several exit conditions. These exit conditions are what allows you to move to another state.
How do I create a finite-state machine?
You have several options to make a finite-state machine:
- Write a script that will be specific to your game. It can be a very good option for very simple games where there is no need to change how to make each action. It’s not very practical to maintain, but you can limit the finite-state machine to some functionalities only.
- Use a specialized library or an asset available on the video game engine you’re using.
In our case, with unity, there is already a finite-state machine managing object animations.
Animator on Unity
The Animator is a very powerful and useful object controller in Unity: it manages the different states, their entry/exit, the transitions between them following some configurable conditions.
You can associate a script to a state. That way, the script will manage its entry and exit events, and it can also link an animation that will play during the state. And all of this is in graphics mode, so it becomes much easier to use for non-programmers.
For Million Lords, we added an Animator component to the game manager. Then, we had to create each state node with the different transitions. For example, with the “Attack” action:
We start from NeutralState, (down on the left), which is as you’ve probably might have guessed) the neutral state, when no window is open in the game.
If you click on one of your own city, we enter SelectCity. The state SelectCity will log the selected city to arrive at state OpenCityActionPanel.
From here, we have 3 possibilities:
- To click on one of our own cities: in that case, we’ll go to CloseCityActionPanel,
- To click outside of the buttons to close the window,
- To click and “Attack” to go further in the action chain “attack”.
Node Inspector
In the Inspector window, the top part corresponds to the Animation options. You can leave this field empty if you just use the finite-state machine alone. In our case, we also use it for the entrance animation and to close the UI windows.
The field “Write Defaults” is used to put the component in its default position if there is no animation on the node.
In Million Lords, we must uncheck it on each node in order to keep the changes that happened on the previous state.
Below is the panel for the transitions towards the other nodes. You can assign several transitions to a single node with different conditions.
To conclude the end section concerns the selected transition:
The field “Has Exit Time” is to create a transition, but only at the end of the animation. In most cases concerning the finite-state machine, you need to uncheck it.
By default, “Transition Duration” isn’t at 0. If you don’t set it at 0, the transition will look irregular and jerky when a change of state happens.
With these simple settings, the Unity Animator becomes a very adequate and satisfactory finite-state machine. It’s reliable and it’s also more accessible, as it is configurable in a graphic way: a good thing for non-programmers.
If you liked this post check out our other articles and don’t forget to sign up for the beta of our game here !
Article written by Ralph Nicolas
Learn more here :