# BehaviourRunner

The main component to run created behaviour tree assets. Use Behaviour Runner component to access associated behaviour tree and blackboard of related AI entity at the scene.

## Public Methods

### GetSharedBehaviourTree()

**Description:**\
Get shared reference of behaviour tree.

**Returns:**\
Shared reference of behaviour tree.

**Example:**

```csharp
BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
BehaviourTree behaviourTree = runner.GetSharedBehaviourTree();
```

***

### SetSharedBehaviourTree(BehaviourTree tree)

**Description:**\
Set new shared behaviour tree reference.

{% hint style="warning" %}
This action will stop the execution of the current behavior tree and terminate it. The work of the tree, its state and the values of the keys in the blackboard will be destroyed and overwritten by the new tree. **This action is irreversible!**
{% endhint %}

**Parameters:**\
Reference of behaviour tree.

**Example:**

<pre class="language-csharp"><code class="lang-csharp">BehaviourRunner runner = ai.GetComponent&#x3C;BehaviourRunner>();
<strong>BehaviourTree behaviourTree = runner.SetSharedBehaviourTree(newTree);
</strong></code></pre>

***

### GetBehaviourTree()

**Description:**\
Associated instance of behaviour tree.

**Returns:**\
Associated (local) instance of behaviour tree created by runner after loading.

**Example:**

```csharp
BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
BehaviourTree behaviourTree = runner.GetBehaviourTree();
Node rootNode behaviourTree.GetRootNode();
```

***

### GetSharedBlackboard()

**Description:**\
Get shared reference of blackboard

**Returns:**\
Shared reference of blackboard.

**Example:**

```csharp
BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
Blackboard blackboard = runner.GetSharedBlackboard();
```

***

### GetBlackboard()

**Description:**\
Associated instance of blackboard.

**Returns:**\
Associated (local) instance of blackboard created by runner after loading.

**Example:**

```csharp
BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
Blackboard  blackboard = runner.GetBlackboard();
if(blackboard.TryGetKey<float>("speed", out FloatKey key))
{
    key.SetValue(10);
}
```

***

## Protected Virtual Methods

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

### Awake()

**Description:**\
Called when an enabled script instance is being loaded.

**Example:**

```csharp
protected override void Awake()
{
    base.Awake();
    Debug.Log("Runner fully loaded");
}
```

***

### Start()

**Description:**\
Called on the frame when a script is enabled, just before any of the Update methods are called the first time.

**Example:**

```csharp
protected override void Start()
{
    base.Start();
    Debug.Log("Runner started the tree");
}
```

***

### Update()

**Description:**\
Called every frame, if the Behaviour is enabled.

{% hint style="info" %}
Keep in mind it's Unity pipeline update call, not of selected behaviour tree update mode.
{% endhint %}

**Example:**

```csharp
protected override void Update()
{
    base.Update();
    // Todo you code here...
}
```

***

### FixedUpdate()

**Description:**\
Called every fixed frame-rate frame. 0.02 seconds (50 calls per second) is the default time between calls, if the Behaviour is enabled.

{% hint style="info" %}
Keep in mind it's Unity pipeline fixed update call, not of selected behaviour tree update mode.
{% endhint %}

**Example:**

```csharp
protected override void FixedUpdate()
{
    base.FixedUpdate();
    // Todo you code here...
}
```

***

### LateUpdate()

**Description:**\
Called every frame, if the Behaviour is enabled after all Update calls.

{% hint style="info" %}
Keep in mind it's Unity pipeline late update call, not of selected behaviour tree update mode.
{% endhint %}

**Example:**

```csharp
protected override void LateUpdate()
{
    base.LateUpdate();
    // Todo you code here...
}
```

***

### OnDestroy()

**Description:**\
Called when an enabled script instance is being destroyed.

{% hint style="info" %}
At this point, the associated instance of the behavior tree and the blackboard is still present and can be safely invoked.
{% endhint %}

**Example:**

```csharp
protected override void OnDestroy()
{
    base.OnDestroy();
    Debug.Log("Runner stopped the tree");
}
```

***

### OnDrawGizmos()

**Description:**\
Implement if you want to draw gizmos that are also pickable and always drawn.

**Example:**

```csharp
protected override void OnDrawGizmos()
{
    base.OnDrawGizmos();
    // Todo your code here...
}
```

***

### OnDrawGizmosSelected()

**Description:**\
Implement to draw a gizmos if the object is selected.

**Example:**

```csharp
protected override void OnDrawGizmosSelected()
{
    base.OnDrawGizmosSelected();
    // Todo your code here...
}
```

***

## Events Callbacks

### OnCloneTreeCompleted

**Description:**\
Called when behaviour runner complete cloning of shared beahviour tree.

{% hint style="info" %}
This callback can be used for making some custom actions with tree before starting the tree of specified AI.&#x20;
{% endhint %}

**Example:**

```csharp
private void SomeMethod()
{
    // AI entity and runner component disabled (sleeping) at this point.
    BehaviourRunner runner = ai.GetComponent<BehaviourRunner>();
    runner.OnCloneTreeCompleted += OnCloneTreeCompleted;
}

private void OnCloneTreeCompleted(BehaviourTree tree)
{
    List<Node> nodes = tree.GetNodes();
    foreach(Node node in nodes)
    {
        // Just example...
    }
}
```
