- Hands-On Artificial Intelligence with Unreal Engine
- Francesco Sapio
- 417字
- 2025-02-24 10:11:44
Behavior Tree components
If you go through the official documentation, you will find that there are five types of nodes (Task, Decorator, Service, Composite, and Root) available to use, depending on the type of behavior that you're trying to create (and subsequently, how the AI should act in the world). However, I'd like to reformulate this to you in order to make it easier to understand and hopefully more practical.
Besides the Root node, the only kind of nodes (that are not leaves) are the Composites nodes. Leaves are called Tasks. Decorators and Services are add-ons for either a Composite node or a Task leaf. Although Unreal lets you leave a Composite node as a leaf, you shouldn't, because it means that you can remove that node and the Behavior Tree will still work in the same way. Here is an example of a tree showing all the different types of node (actually, we will build this Behavior Tree later in this book):

When a tree is executing, you need to start following from the root node and go down the tree, reading the nodes from left to right. You traverse all the different branches (Composite nodes) in a specific way (we are going to see this in a little while), until we reach a leaf, which is a Task. In this case, the AI performs that Task. It is important to note that a Task can fail, for instance, if the AI is unable to complete it. The fact that a Task can fail will be useful for understanding how Composite nodes work. After all, a decision-making process is just choosing which task to perform to achieve a goal in a better way (e.g. killing the player). Thus, based on which task failed to execute (or, as we will see, a Decorator can make a task or a whole branch fail), the Composite nodes will determine the next tasks in the tree.
In addition, when you create your Behavior Tree, each node can be selected, and some settings to adjust the behavior of the node/leaf can be found in the Detail Panel. Moreover, since the order is important, the nodes in the Behavior Tree have numbers (in the top-right corner) to help you understand the ordering of the node (although it is always from top to bottom, left to right). The following screenshot shows where you can find these numbers:


Let's look at these components in detail and pay particular attention to Composite nodes.