> For the complete documentation index, see [llms.txt](https://renownedgames.gitbook.io/ai-tree/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://renownedgames.gitbook.io/ai-tree/api/runtime/behaviourtree.md).

# BehaviourTree

The BehaviourTree class is responsible for defining, managing, and controlling the behavior trees used in AI logic.

## Public Methods

### **Clone()**

**Description:**\
Clones the entire behavior tree, including all nodes and settings.

**Returns:**\
`BehaviourTree` — A new cloned copy of the behavior tree.

**Example:**

```csharp
BehaviourTree clonedTree = originalTree.Clone();
```

***

### **GetBlackboard()**

**Description:**\
Shared Blackboard reference associated with this behaviour tree reference

**Returns:**\
`Blackboard` — The blackboard used by this behavior tree.

{% hint style="warning" %}
Always return shared reference of blackboard. If you need blackboard instance see [BehaviourRunner](/ai-tree/api/runtime/behaviourrunner.md)
{% endhint %}

**Example:**

```csharp
Blackboard blackboard = behaviourTree.GetBlackboard();
```

***

### **GetUpdateMode()**

**Description:**\
Retrieves the current update mode for the behavior tree.

**Returns:**\
`UpdateMode` — The current update mode (e.g., **Update**, **FixedUpdate**, **LateUpdate**, **Custom**).

**Example:**

```csharp
UpdateMode mode = behaviourTree.GetUpdateMode();
```

***

### **SetUpdateMode(UpdateMode mode)**

**Description:**\
Sets the update mode for the behavior tree.

**Parameters:**\
`UpdateMode mode` — The desired update mode.

**Example:**

```csharp
behaviourTree.SetUpdateMode(UpdateMode.Update);
```

***

### **GetTickRate()**

**Description:**\
Retrieves the tick rate, which defines how often the behavior tree updates.

**Returns:**\
`int` — The number of ticks per second.

**Example:**

```csharp
int tickRate = behaviourTree.GetTickRate();
```

***

### **SetTickRate(int count)**

**Description:**\
Sets the tick rate for how often the behavior tree should update.

**Parameters:**\
`int count` — The number of ticks per second.

**Example:**

```csharp
behaviourTree.SetTickRate(5);
```

***

### **IsRunning()**

**Description:**\
Checks whether the behavior tree is currently running.

**Returns:**\
`bool` — `true` if the tree is running, `false` otherwise.

**Example:**

```csharp
bool isRunning = behaviourTree.IsRunning();
```

***

### **GetRootNode()**

**Description:**\
Retrieves the root node of the behavior tree.

**Returns:**\
`Node` — The root node of the behavior tree.

**Example:**

```csharp
Node rootNode = behaviourTree.GetRootNode();
```

***

### **GetNodes()**

**Description:**\
Returns the list of all nodes in the behavior tree.

**Returns:**\
`List<Node>` — The list of all nodes.

**Example:**

```csharp
List<Node> nodes = behaviourTree.GetNodes();
```

***

### **SetNodes(List\<Node> value)**

**Description:**\
Sets the list of nodes in the behavior tree.

**Parameters:**\
`List<Node> value` — The list of nodes to assign.

**Example:**

```csharp
behaviourTree.SetNodes(new List<Node> { node1, node2 });
```

***

### **GetCurrentNode()**

**Description:**\
Returns the currently executing node.

**Returns:**\
`Node` — The node currently being executed.

**Example:**

```csharp
Node currentNode = behaviourTree.GetCurrentNode();
```

***

## Static

### **TraversePath(Node node, Node searchingNode)**

**Description:**\
Traverses the path from a specified node to a target node.

**Parameters:**\
`Node node` — The starting node.\
`Node searchingNode` — The target node to find.

**Returns:**\
`LinkedList<Node>` — The path of nodes leading to the target.

**Example:**

```csharp
LinkedList<Node> path = BehaviourTree.TraversePath(startNode, targetNode);
```

***

### **Traverse(Node node, Action\<Node> visiter)**

{% hint style="danger" %}
Not recommended. This method is obsolete.

Use Node.Traverse() implementation.
{% endhint %}

**Description:**\
Traverses all nodes from the target node with a visitor callback. **Note:** This method is obsolete; use `Node.Traverse()` instead.

**Parameters:**\
`Node node` — The starting node.\
`Action<Node> visiter` — The callback to apply to each node.

**Example:**

```csharp
BehaviourTree.Traverse(rootNode, node => Debug.Log(node.Name));
```

***

### **GetChildren(Node node)**

**Description:**\
Returns all child nodes of a given node.

**Parameters:**\
`Node node` — The parent node.

**Returns:**\
`List<Node>` — The list of child nodes.

**Example:**

```csharp
List<Node> children = BehaviourTree.GetChildren(parentNode);
```

***

### **IterateChildren(Node node)**

**Description:**\
Iterates through all child nodes of a given node.

**Parameters:**\
`Node node` — The parent node.

**Returns:**\
`IEnumerable<Node>` — An enumerable collection of child nodes.

**Example:**

```csharp
foreach (var child in BehaviourTree.IterateChildren(parentNode))
{
    Debug.Log(child.Name);
}
```

***

### **IsSubNode(Node node, Node checkNode)**

**Description:**\
Checks if a node is a sub-node of another node.

**Parameters:**\
`Node node` — The parent node.\
`Node checkNode` — The potential sub-node.

**Returns:**\
`bool` — `true` if `checkNode` is a sub-node of `node`, `false` otherwise.

**Example:**

```csharp
bool isSubNode = BehaviourTree.IsSubNode(parentNode, childNode);
```

### **Create(string name)**

{% hint style="warning" %}
**Editor only!**

If you call this method in build code block, wrap it in `UNITY_EDITOR` define.
{% endhint %}

**Description:**\
Creates a new behavior tree asset in the project.

**Parameters:**\
`string name` — The name of the new behavior tree asset.

**Example:**

```csharp
#if UNITY_EDITOR
    BehaviourTree newTree = BehaviourTree.Create("New AI Tree");
#endif
```

***

## Protected Virtual Methods

Use the following methods only for overriding when inheritance, it is not recommended not to call these methods manually.

### **OnEnable()**

**Description:**\
Called when the behavior tree is loaded.

**Example:**

```csharp
protected override void OnEnable()
{
    base.OnEnable();
    Debug.Log("Behaviour Tree Loaded");
}
```

***

### **Reset()**

**Description:**\
Called when the user presses the Reset button in the Inspector, or when the component is first added.

**Example:**

```csharp
protected override void Reset()
{
    base.Reset();
    Debug.Log("Behaviour Tree Reset");
}
```
