Chapter 3. Behavior Trees

When creating AI for game characters, we want them to appear to behave in realistic ways. This is done by defining different behaviors that a character can do, such as walking, patrolling, attacking, or searching for something, as well as how the character reacts to different items or events in the game environment. In addition to defining a character's behaviors, we need to define when the different behaviors occur. For example, instead of just following a path, we might want the character to change behaviors at different times. This chapter will look at the most popular way to define behaviors and when they occur: behavior trees. We have already looked at behavior trees in the previous chapters, but here, we will go into more detail.

In this chapter, we will learn about:

  • How behavior trees work
  • Implementing complex behavior trees
  • RAIN's behavior trees and the different options that we have to configure them
  • Setting up more advanced behavior trees with a character that has multiple objectives

An overview of behavior trees

For game AI, we need to define logic for the different AI entity characters in the game, that is, how they will act and react to different things in the game environment. The traditional and simpler way to do this is to use Finite State Machines (FSMs). In this approach, each character can be in a distinct state, and an FSM is a graph that defines states (nodes) and their transitions (edges). A simple example would be an enemy entity with two states, patrol and attack. The FSM will start in a patrol state, and when it gets close to a player, it transitions to an attack state. FSMs work for very simple state setups such as this, but they don't scale well, as the states and transitions have to be manually configured, usually through code. What if instead of the two states, our enemy character was more realistic and had 10 or even 100 different states, with many transitions between each? This becomes very difficult to manage and implement.

The popular alternative to FSMs is behavior trees. Behavior trees are a different way to define logic for characters that scale easily to having many states. Instead of defining states and transitions, behavior trees focus on defining behaviors, also called tasks, for characters. Each behavior is a node in the tree and can consist of different sub-behaviors; so, instead of a general graph, a tree is created of different behaviors, where each behavior is a node on the graph.

At every update for the character, the behavior tree is traversed, starting at the root node and searching down the tree. The different behavior nodes execute and return if the task is running, or has completed successfully or failed. If the node is in a running state, it is updated. Behavior trees are built by creating and configuring different behavior nodes.

We will focus on RAIN's behavior tree system in this chapter. We can use a different behavior tree system or create one from scratch; the basic logic is the same for all implementations. When using a behavior tree system, the most important thing to know are the different node types that we can use; so, let's look at RAIN's different behavior nodes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.11.226