When it comes to building an AI system, the game developer is presented with a lot of options. Be it a State Machine, a Behavior Tree, or even a Utility AI.
And these choices can be overwhelming…
By the end of this article you will get a clear picture of what is an AI System, what are the most common ways to implement decision making accompanied by their PROs and CONs.
The AI is much more…
Before we look into different decision systems, it’s important to note that…
… An AI System is much more than just its decision-making component. While an important part, implementing an FSM or a BT won’t make your AI Agent come to life. If you don’t know what FSMs / BTs are, don’t worry! Keep reading to find out.
Here are some of the other systems that are needed for a fully functional AI:
Pathfinding
When the AI receives an order to move from point A to point B a pathfinding algorithm is called. Its role is to inspect the environment around and provide a possible path that can be taken, without colliding with obstacles or other entities.
There are a lot of pathfinding algorithms, but by far the most popular in gamedev is A*.
Actions
Shooting, taking cover and leaping over obstacles. These are just some of the actions that can be performed by an AI.
Actions are a necessity. They have similar implementations to the regular player controller character since the decision-making part of the AI acts as a puppeteer. While, in the player’s case, it’s the human that has a similar role.
Besides pathfinding and actions, a complete AI system needs animations, sounds, effects, and much more.
Finite State Machines
Finite State Machines or FSMs are probably the first to appear in the Game AI world. They have their roots in math as many other systems found in a game.
At the core, a FSM is composed of STATES and TRANSITIONS. A state can hold an individual action as well as a sequence, depending on the developer’s needs. A state can connect with other states via transitions.
A transition is a link between states that can have one or more conditions. The power comes when adding relevant conditions to gain control of what the system can do.
One of the main pros of this system is the easiness to pick up and use. Create states and connect them. That’s it.
If you use a visual editor, this is even simpler. Most modern game engines already contain a State Machine implementation for AI, Animations or UI. They can even be repurposed from one use-case to another.
While all of this sounds great, what is the main drawback of using such a system? Well, as I discussed in this article, the complexity of a FSM can go out of control by adding more states and more transitions.
You should consider using FSM in the following situations:
- Your AI is not too complex – It will save you time and effort in implementation.
- You want to have lost of AI instances at the same time e.g. zombies, hordes.
- The AI designer has coding skills – To change what’s inside a state, coding skills are required.
If you want to know more about Finite State Machines, I’ve also written “Are State Machines still Relevant for AI?“.
Behavior Trees
Modern games have new challenges. And FSM quickly got out of hand with complex systems.
This is why HALO creators decided to make a new system: enter Behavior Trees (BT).
A BT is also based on the data structure Graph, more precisely it’s particular Tree. A node sits at its root while the branching nodes are used to define actions, conditions and other flow control instructions. By just looking at this new system, it can be very similar to a programming language.
Behavior Trees fix some of FSM’s shortcomings. By using them an AI system not only it can be maintained more easily, but it can also be extended as adding more functionality does not increase the level of complexity to a point where it’s not manageable.
One other advantage of Behavior Trees is that it allows non-coders to design AI systems. By having predefined states, flow control instructions and conditions, the designer can leverage the power of a BT to create their vision without the help of a programmer.
You should consider using BTs for the following:
- Your AI Agents have more complex behaviors.
- You want to empower the non-coders in your team to create the systems themselves.
- Your game has a limited number of AI Instances as this system needs more resources than a FSM.
Other options
GOAL – ORIENTED ACTION PLANNING (GOAP)
Popularized with the game F.E.A.R. in 2005, this approach of making AI combines the states from Finite State Machines but opts to generate the transitions at runtime with a planning algorithm.
This will create one of the best Game AI Systems in the world.
Implementing and using GOAP is not something to start with as a beginner. If you are just starting out, it’s better to learn FSM/BT first and only when you master them go to the next level.
In most cases, GOAP is overkill as it both presents a challenge to the developer as to the resource consumption (processor power). If you really want to use it in your project, make sure that the game design does not require many AI instances at the same time.
Should you use GOAP as a beginner? Most likely your game will be fine with either FSM/BT.
UTILITY AI
But what if you are developing a card game, turn based game or a RPG with a lot of spellcasting? Then neither FSM nor BT are good options as they can’t handle nicely a way to pick from multiple similar actions in an interesting way.
This is where the Utility AI shines. Imagine that the AI Agent has multiple similar actions and it needs a quick answer on what to pick from that list. The Utility AI will rank and sort to provide the closest action to yield the best outcome.
In some cases, this AI might provide the same output for the same inputs. If this happens too often, the system will become predictable and the player will loose interest in it. Make sure to add some randomness to spice it up.
Should you use Utility AI? If you are making one of the game types stated above, then there is no other way. If you are making a First Person Shooter, then there is no need for this at all.
Final Thoughts
Developing an AI System for a game has a lot of challenges. I hope that this article cleared some misunderstandings about picking the right decision making component for your game.
In most cases, you will be likely covered by FSM or BTs and you should check what your engine of choice supports out of the box. E.g. Unreal has BTs and Unity / Godot do have FSMs but they need to be repurposed for AI (there are tutorials on this, don’t worry).
While implementing these, don’t forget about the other aspects of a proper AI: pathfinding, actions, animations, etc.
Good luck!
All the best,
Adrian.
0 Comments