State Machine for Games: Summarizing and Simplifying

State machines (FSM) are the old school in game development. They are present since the beginning, when the game complexity starts to increase, requiring a little more logic to control the different possible game states or a player state. What few know is that a state machine can be used to control almost everything in a game, and contrary to what many think, it is not exactly an artificial intelligence.

State machines (FSM) are the old school in game development. They are present since the beginning, when the game complexity starts to increase, requiring a little more logic to control the different possible game states or a player state. What few know is that a state machine can be used to control almost everything in a game, and contrary to what many think, it is not exactly an artificial intelligence.

Leia esse artigo em português aqui .

What is a State Machine?

The state machine is a part of software engineering and is nothing more than a simple way of describing the functioning of a system using transition, states, and attributes. Figure 1 illustrates the basic feature of a state machine.

Figure 1 — Simple example of the representation of a state machine.

How can it be used in Digital Games?

Indigital games, FSM can be used for a variety of purposes, ranging from describing processes to controlling animations or NPCs (Non-player characters). Some examples of the main uses within digital games are mentioned below.

In the environment controller, for example, the states of day, night, sunset, or sunrise. Rainy weather, snowing, sun, are examples of states of the environment that can be controlled by a simple state machine like the example in Figure 2.

Figure 2 — Scheme of a simple state machine that controls the transition from day tonight.

Animation controller is the most common, for example, the game engine Unity itself offers a very good tool called Animator, which in fact is nothing more than a state machine that can fulfill much more than its main function (controlling animations ).

Figure 3 — Practical example of using state machines to animate a character in the Unity game engine.

Controller of menus, scenes, and screens is another utility of state machines. Not only in the planning of possible transitions but also in the implementation of the controllers. Agent and Bots Controller (NPCs), is one of the most important uses, in controlling enemies and game characters the state machines are responsible for generating behaviors and reactions to the game environment.

State-Based Pseudo Intelligent Machines?

Many call the state machines of artificial intelligence because they are widely used as decision controllers in agents within digital games. The truth is that this structure should not be considered an AI, as it is not dynamic, it does not have the ability to learn, among some of the main characteristics of intelligent systems. So, why are they still used so much to control NPCs and enemies instead of a neural network or genetic algorithms?

Figure 4 — Simple controller of an enemy in a digital game. According to the state machine, the bot must attack the enemy if it is within sight, otherwise, the bot must continue to patrol.

The answer is simple, agility, as it does not require much processing, the possibility of greater control, because you know how the agent will behave and there is no risk of strange or unwanted behavior, and finally, the ease of application. In addition, in most cases, it is enough to fulfill the purpose in question. But as not everything is flowers, there are some negative points when using this type of structure when controlling agents that should be more realistic or challenging. Repetition is the biggest of these problems because no matter how complex the structure of the state machine is, it will always be possible to find a pattern in behavior because it does not change.

Figure 5 — For the state machine to be able to control an agent, it must receive data from the environment to carry out state transitions.

Throughout our research, we will make use of state machines in various examples and practical applications alongside other techniques. We will also go deeper into the study of techniques to build finite state machines. Follow the survey by visiting the blog and signing up to receive new articles by email.

References to learn more:

AI for Games: THIRD EDITION (Ian Millington)

AI for Game Developers (David M. Bourg, Glenn Seeman), 2004.

O Animator Controller da Unity como uma Máquina de Estado (Irene Naya) Disponível em: <https://medium.com/@irenenaya/unitys-animator-controller-as-a-state-machine-5ef2f84e6c53>

How to create a finite state machine to load your game using JavaScript (Verónica Valls) Disponível em: <https://medium.com/game-development-stuff/how-to-create-a-finite-state-machine-to-load-your-game-using-javascript-e10a48e675b9>

Fun with finite state machines! (Moral Robots) Disponível em: <https://medium.com/@MoralRobots/fun-with-finite-state-machines-b0b2982c25d9>

Solve Jump Game Problem using State Machine (Tankery Chen) Disponível em: <https://medium.com/@tankery/solve-jump-game-problem-using-state-machine-4a3b1d6e5fc0>

Why Storing State Machines in ECS is a bad idea (Sander Mertens) Disponível em: <https://ajmmertens.medium.com/why-storing-state-machines-in-ecs-is-a-bad-idea-742de7a18e59>

Implementing a Finite State Machine Using C# in Unity — Part 1 (Shamim Akhtar) Disponível em: <https://faramira.com/implementing-a-finite-state-machine-using-c-in-unity-part-1/>

Finite-state machine (Wikipedia EN) Disponível em: <https://en.wikipedia.org/wiki/Finite-state_machine>

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.