# Behaviour Tree

**Behavior Trees** are created in a visual by adding and connecting a series of nodes which have some functionality attached to them to a Behavior Tree Graph. While a Behavior Tree executes logic, a separate asset called a [**Blackboard**](https://renownedgames.gitbook.io/ai-tree/basic/blackboard) is used to store information (called [**Blackboard Keys**](https://renownedgames.gitbook.io/ai-tree/basic/blackboard)) the Behavior Tree needs to know about in order to make informed decisions.

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2F2r4Box1KskZaGi8s1fsA%2FImage.png?alt=media&#x26;token=3e0beed3-2afd-4464-8bf6-88a0854cb361" alt=""><figcaption><p>An example of a Behavior Tree where an AI character can patrol.</p></figcaption></figure>

A typical workflow would be to create a Blackboard, add some Blackboard Keys, then create a Behavior Tree that uses the Blackboard asset (pictured below, a Blackboard is assigned to a Behavior Tree).

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2FQ0zq3fZZLNo15oz205z6%2FImage.png?alt=media&#x26;token=09dc0509-bfad-4a14-a1ff-43a993c18492" alt=""><figcaption><p>Assigned Blackboard</p></figcaption></figure>

Behavior Trees in AI Tree execute their logic from left-to-right, and from top-to-bottom. The numerical order of operation can be viewed in the upper-right corner of nodes placed in the graph. In the image below, a sample of a branch placed in the left-most portion of a Behavior Tree graph has some nodes that instructs an AI to generate a random position and go to it.

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2Fxduc40lJWsbX3usLC8Ih%2FImage.png?alt=media&#x26;token=7e9399f4-2e61-48d8-a05f-f2c2448739e1" alt=""><figcaption><p>Numbering of node execution.</p></figcaption></figure>

{% hint style="warning" %}
If you forgot to connect one of the nodes, it will show you this warning:
{% endhint %}

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2FmJQrp9YqVYrJI42T6a17%2FImage.png?alt=media&#x26;token=2849a9ce-0ee9-4939-bc65-7891f34b876e" alt=""><figcaption><p>Node isn't connected to one of the nodes.</p></figcaption></figure>

To link a node, you must pull the connection from the top or bottom of the node (indicated by a circle) to the center of another node.

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2F2zhelozsPaOm9bUrrU4R%2FImage.png?alt=media&#x26;token=cc79f187-31ba-4271-801e-bf126086a8a9" alt="" width="375"><figcaption><p>Link example</p></figcaption></figure>

{% hint style="info" %}
Connecting nodes in **Behavior Trees** only work by connecting an output to an input (you cannot connect from an input to an output).
{% endhint %}

We connect the nodes in this way:

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2F2r4Box1KskZaGi8s1fsA%2FImage.png?alt=media&#x26;token=3e0beed3-2afd-4464-8bf6-88a0854cb361" alt=""><figcaption><p>Links example</p></figcaption></figure>

{% hint style="danger" %}
In order for the tree to work in game mode, you need to add a component to the GameObject, in which you specify the desired tree.
{% endhint %}

To connect **Behavior Tree** to GameObject, we need to add a component to it Behavior Runner.

To do this, go to the inspector window of the necessary GameObject.

Clicks Add Component and searches Behavior Runner.

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2FmfkcRzLWLuVHL6q3rRZQ%2FImage.png?alt=media&#x26;token=80896f9f-9809-4ebc-902c-fc56cf20405a" alt="" width="563"><figcaption></figcaption></figure>

In the Behavior Runner field, connect our created **Behavior Tree.**

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2FqTWzZiUTdc9qkQy2dmRN%2FImage.png?alt=media&#x26;token=0e2d37a2-2e4f-4cec-bc7e-0949a2287938" alt="" width="563"><figcaption></figcaption></figure>
