What is a Behaviour Tree?

A Behavior Tree (BT) is a hierarchical structure that defines the decision-making process of an AI agent. It consists of nodes that represent tasks or behaviors, which are executed based on certain conditions. The tree is traversed from the root node down to the leaf nodes, with each node returning Success, Failure, or Running status. Behavior Trees help you structure the decision-making logic of your agent, making complex behaviors manageable and understandable via visual representation.

Main Concepts

1. Action Nodes

Action nodes are the leaf nodes of the Behavior Tree. They represent the actual tasks or behaviors that the AI agent can perform. When an action node is executed, it attempts to carry out a specific action, such as moving to a target, attacking an enemy, or playing an animation.

2. Composite Nodes

Organizational nodes that control the execution flow of their child nodes. There are several types of composite nodes, including:

  • Sequence: Executes its child nodes in order until one fails. If all child nodes succeed, the sequence node succeeds.

  • Selector: Attempts to execute its children one by one until one succeeds. It's used for selecting between different behaviors based on their success.

  • Parallel: Executes all its child nodes simultaneously and succeeds if a specified number of child nodes succeed.

For example you can have a "FindAndAttackEnemy" sequence node with two child nodes: "SearchForEnemy" and "AttackEnemy". The sequence node would first execute the "SearchForEnemy" node to locate an enemy. If an enemy is found, it would then execute the "AttackEnemy" node. If both child nodes succeed, the sequence node succeeds.

3. Decorator Nodes

Modifiers that alter the behavior or outcome of their child nodes. They can be used to repeat actions, invert success/failure, or conditionally execute based on certain criteria. Common types of decorator nodes include:

  • Inverter: Inverts the success/failure status of its child node.

  • Repeater: Repeats the execution of its child node a specified number of times or until a condition is met.

  • Conditional: Executes its child node only if a specified condition is true.

A repeater node can execute the "AttackEnemy" node multiple times, allowing the AI agent to perform consecutive attacks.

4. Blackboard

The Blackboard is a global data store that allows nodes to share information and state across the Behavior Tree. It acts as a key-value store, where nodes can read and write data. The Blackboard is useful for storing variables such as target positions, health values, or any other relevant information that nodes might need to access.

Last updated