# Behaviour Tree

# What is a Behaviour Tree?

- Behaviour tree is a tree structure for clarifying behaviour priorities and conditions and facilitating control.
- Let’s consider an enemy AI, for example.
  - It approaches if the distance between the enemy and the player is within 10 meters.
  - Otherwise, it moves on a fixed route.
- Behavior to approach the player can be said to have a high priority in comparison with the behavior of moving the fixed route.
- Temporarily assembling with Arbor’s Behaviour Tree will be as follows.
  (Commentary later)
  - The higher the priority of the node on the left, the lower the priority on the right.
  - Also, by adding a script called Decorator to the node, we judge the condition and control the node to be executed.

# Node component

## Root Node

It is the first active node to run the behavior tree.

You can connect with one child node.

## Composite Node

It has one or more child nodes and controls which child nodes to execute.

How to control is described in CompositeBehaviour script.

You can also add Decorator scripts and Service scripts to be described later.

### Built-in CompositeBehaviour

- Selector
  Execute the child nodes in order from the left, terminate when the child node returns success , and return success.
  If all child nodes return failure, it returns failure.
  It is used when you want to find a successful node in order from the left and you do not want to run subsequent nodes.
- Sequencer
  Execute the child nodes in order from the left, terminate when the child node returns failure , and return a failure.
  If all child nodes return success, success is returned.
  It is used when you want to execute all as long as the child node returns success.

For reference on the built-in CompositeBehaviour see here.

Arbor Reference : Built-in CompositeBehaviour

## Action Node

The node that performs the action.

Write the ActionBehaviour script to see what action to take.

Also, we can add Decorator script and Service script here as well.

For reference on built-in ActionBehavior see here.

Arbor Reference : Built-in ActionBehaviour

For creating ActionBehaviour script, click here.

Customize ActionBehaviour

## Decorator script

It is a script that can perform condition judgment, suspend or interrupt an executing node, repeat judgment at the end timing.

This script is used in addition to composite nodes and action nodes.

For reference of the built-in Decorator, please refer to here.

Arbor Reference : Built-in Decorator

For creating Decorator script, click here.

Customize Decorator

### About AbortFlags

Abort or interrupt target is controlled by AbortFlags.

- Self
  Performs condition determination while its node or its subordinate node is executing.
  If the result of condition judgment is false, abort and return failure to the parent node.
- LowerPriority
  A node with a lower priority on the right side than its own node performs a condition determination during execution.
  If the result of the condition judgment is true, the currently executing node is suspended, and its own node becomes active.

It can be set by combining the above two flags.

Also, regardless of AbortFlags, condition judgment is performed once when its own node becomes active, and if it is false, it will be returned as a failure as it is.

## Service script

Service scripts can be added to composite nodes and action nodes, scripts that can do something while the node is active.

Although it is not used in the example Behaviour Tree, For example, it is suitable for describing auxiliary processing such as adding a Service that stores the distance to the player in Parameter to the Selector node.

For creating Service script, click here.

Customize Service

# Explanation of execution

Let’s take a look at the previous example again.

- Beginning with the root node.
- With the Selector node, we will look for the successful node of the child node and execute it from left to right.
- In the Agent Move To Transform node, since the ParameterCheck decorator has been added, judgment is made on the condition of Parameter.
- As an example, the distance to the player is created assuming that it is stored in the Distance parameter.
- Assuming Distance is 15, the ParameterCheck decorator returns false, so the Agent Move To Transform node fails and returns to Selector.
- The Selector executes the next Agent Move On Waypoint node because the Agent Move To Transform node failed.
- The Agent Move On Waypoint action continues to run repeatedly unless an interruption or the like occurs because Type is set to Cycle.
- Let’s set the Distance parameter to 10 or less.
- Agent Move To Transform interrupts the current node because it matches the ParameterCheck condition of the node.
- Again by making the Distance parameter greater than 10 you can see that the Agent Move To Transform node is interrupted and the Agent Move On Waypoint node becomes active.
  How the active node changes by Distance parameter

As you can see, the execution priority gets lower as going to the right, and you can see that interrupts etc. can be done by Decorator of the node with high priority.

Even if it is a simple example like this time, even a finite state machine can be assembled without making it particularly complicated,
In the case where the number of states is large and the condition is likely to be complicated, it becomes possible to combine state transitions that are easier to maintain because the condition tree can be described on the execution node side without the transition lines crossing in the behavior tree in the behavior tree.

# How to use Behaviour Tree

For details on how to use, please click here

How to use Behaviour Tree

# Script customization

For customization with various scripts, please see here

Customize