> For the complete documentation index, see [llms.txt](https://thebitcave.gitbook.io/ai-brain-extensions-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://thebitcave.gitbook.io/ai-brain-extensions-docs/aibrain-graph/topdown-engine-getting-started-tutorial.md).

# TopDown Engine - Getting Started Tutorial

In this tutorial you'll learn the basic steps of the AIBrain graph for TopDown Engine.

**Note**: To get you started using the AI Brain Graph, you should first follow the [Install instructions](/ai-brain-extensions-docs/install-instructions.md) and check that everything is working.

### The Tutorial Scene

First of all, look for the *MinimalAI3D\_Tutorial* scene (and open it), that will serve as a starting point for this tutorial. Basically, it is the TopDown *MinimalAI3D* scene with all enemies removed (with the exception of *PatrolAndMoveAI*).

1. Open the *AIs* group to check the content: you will find the *PatrolAndMoveAI* prefab along with two other gameObjects (stripped out of all AI components): *PatrolAndMoveAI\_Generated* and *PatrolAndMoveAI\_Pluggable.*

### Creating the AI Brain Graph

To start working with the graph, you'll need to create an AI Brain Graph:

1. In the Project panel, right-click the mouse button and select *Create > The Bit Cave > AI Brain Graph*
2. Rename the newly created asset *PatrolAndMoveBrain* (or anything you deem appropriate)
3. Double-click on the asset to open the Editor

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6UOCGu7Dp486lkrL_%2F-Lx6WH-RkC2ctnoo0UFS%2Ftopdown_tutorial_001.png?alt=media\&token=7d809e91-9684-4ecd-8e7d-ba35ec4518a5)

### The PatrolAndMoveAI Brain Structure

Select the *PatrolAndMoveAI* prefab in scene and look at the *AI Brain*, *AI Actions* and *AI Decisions* components: we want to replicate the same structure with the AI Brain Graph:

* The *AI Brain* has two states: *Patrolling* and *MoveTowardsTarget*
* While in *Patrolling* the character will perform an *AIActionMovePatrol3D* and an *AIActionAimWeaponAtMovement* action
* While in *MoveTowardsTarget* the character will perform an *AIActionMoveTowardsTarget3D* and an *AIActionAIMWeaponATMovement* action
* The chracters will exit the *Patrolling* state if the target is within range (*AIDecisionDetectTargetRadius3D*)
* The character will not exit the M*oveTowardsTarget* state after a while

To create the AI Brain Graph we will need these nodes:

* Two states
* Two transistions
* Three actions
* One decision

#### Creating the States

First of all, let's create the two states:

1. Right click on the AI Brain graph canvas and choose *AI > Brain State*: this will create a state
2. Right click on the node header and choose *Rename*. Rename the state *Patrolling* and select *Apply*
3. Repeat the first two steps, creating another node called *MoveTowardsTarget*
4. On the *Patrolling* node, click the *Set as starting state* button: this will set the *Patrolling* node as the entry state in the character brain

{% hint style="warning" %}
Each state should always have a unique name
{% endhint %}

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6krhzmGGXK9IlK23j%2F-Lx6qLjpsELStX7dnQOL%2Ftopdown_tutorial_002.png?alt=media\&token=d21c6448-5867-42bb-8d9a-fa6cb5842ff3)

#### Adding Transitions

The *Patrolling* state has a single transition, so we are going to create it:

1. Right click on the Graph canvas and select *AI > Transition*: this will add a transition node
2. Click on the *transitions* output in *Patrolling* state and drag it to the *Input* element of the transition node

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6tVme8pBjdUaQg13x%2F-Lx6vwGNJxVjuw0mTber%2Ftopdown_tutorial_003.png?alt=media\&token=832534ea-8d5d-4764-b5fb-7b2e5a9f4be0)

{% hint style="info" %}
There's no decision for the *MoveTowardsTarget* state, so we won't need a transition.
{% endhint %}

#### Adding AI Decisions

The original character has a single decision, connected with the *Patrolling* state, so let's add it to the graph.

1. Right click on the graph canvas and select *AI > Decision > 3D > Detect Target Radius*
2. Set the *Radius* to 8
3. Set the *Target Layer Mask* to *Player*
4. Set the Obstacle *Mask* to *Obstacles*, *ObstaclesDoors*, *NoPathfinding*
5. Connect the *Output* element to the *Decision* input of the patrolling state *AI Transition* node

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6tVme8pBjdUaQg13x%2F-Lx6wBQyVDielZdSObrm%2Ftopdown_tutorial_004.png?alt=media\&token=c88a6885-0b80-4b0a-9d86-12fa7ec5cf17)

#### Adding AI Actions

The swordsman has three actions, with one of them in common with both states: let's add them to the graph.

For the *Patrolling* state:

1. Right click on the graph canvas and select *AI > Action > 3D > Move Patrol*
   1. Set the Obstacle *Mask* to *Obstacles*, *ObstaclesDoors*, *NoPathfinding*
   2. Connect the *Output* element to the *Actions* input of the *Patroling* state node
2. Right click on the graph canvas and select *AI > Action > Aim Weapon At Movement*
   1. Connect the *Output* element to the *Actions* input of the *Patrolling* state node

For the *MoveTowardsTarget* state:

1. Right click on the graph canvas and select *AI > Action > 3D > Move Towards Target*
   1. Connect the *Output* element to the *Actions* input of the *MoveTowardsTarget* state node
2. You don't need to create a second *Aim Weapon At Movement*
   1. Connect the *Output* element to the *Actions* input of the *MoveTowardsTarget* state node

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6tVme8pBjdUaQg13x%2F-Lx6wSypqeDns9lNpp8k%2Ftopdown_tutorial_005.png?alt=media\&token=6962a308-6c3e-430c-9630-b9178caa4cca)

#### Connecting Transitions

The last step to complete the brain graph is to connect the transitions *True/False* states. In this case we only need the *True* one:

1. Connect the *True State* of the patrolling *AI Transition* to the *States in* input of the *MoveTowardsTarget* state

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6tVme8pBjdUaQg13x%2F-Lx6wa45xX9bQ5qc-EGg%2Ftopdown_tutorial_006.png?alt=media\&token=c2c12e0f-6c7b-42fd-a51e-de0b346881da)

### Generating the Character AIBrain System

We are now ready to add the brain graph to a gameObject and generate all the TopDown Engine AI structure: we have two options for this.

#### AI Brain Generator

The *AI Brain Generator* component adds a static brain structure to your character, just like you are used to do in TopDown Engine:

1. Enable the *PatrolAndMoveAI\_Generated* gameObject
2. Click the *Add Component* button and select *The Bit Cave > AI > AI Brain Generator*
3. Drag the *PatrolAndMoveBrain* we created above in the *AI Brain Graph* field
4. Click the *Generate* button and you will get the full working TopDown AI system for the *PatrolAndMoveAI* (try comparing it with the original prefab)

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6wbK3_jTSSdG2Yz_F%2F-Lx6xoF29FXavy-vop_B%2Ftopdown_tutorial_007.png?alt=media\&token=2b0db6f5-dee3-4cdc-abba-24f49f1bb3c4)

{% hint style="info" %}
Once you are happy with your brain, you can safely remove the AI Brain Generator component as it is not used during gameplay
{% endhint %}

#### AI Brain Pluggable

The *AI Brain Pluggable* generates all Corgi AI system at runtime from a list of brain graphs:

1. Enable the *PatrolAndMoveAI\_Pluggable* gameObject
2. Click the *Add Component* button and select *The Bit Cave > AI > AI Brain Pluggable*
3. Drag the *PatrolAndMoveBrain* in the *AI Brain Graphs* field: this is an array of elements, so you can add more than one (one will be chosen randomly at runtime)
4. Editor *Play* button and you should see the TopDown Engine AI Brain structure generated

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-Lx6wbK3_jTSSdG2Yz_F%2F-Lx6yYkbH1GCjAkkaxyh%2Ftopdown_tutorial_008.png?alt=media\&token=5bd8703c-db3e-42b9-9d95-68e27b5608c4)

{% hint style="warning" %}
The AI Brain Pluggable is an extension of the regular TopDown Engine AI Brain, so you'll have to keep it in your gameObject
{% endhint %}
