> 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/corgi-engine-getting-started-tutorial.md).

# Corgi Engine - Getting Started Tutorial

In this tutorial you'll learn the basic steps of the AIBrain graph for Corgi 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 *AIBrain\_Tutorial* scene (and open it), that will serve as a starting point for this tutorial. Basically, it is the Corgi *RetroAI* scene with all enemies removed (with the exception of *RetroSwordsman*).

1. Open the *Enemies* group to check the content: you will find the *RetroSwordsman* prefab and two other gameObjects (stripped out of all AI components): *RetroSwordsmanGenerated* and *RetroSwordsmanPluggable.*

### 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 *RetroSwordman AIBrain* (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-LwoGvqvfj9-CnIbRxdm%2F-LwoHTPUm4lAZ_St1ZTD%2Fcorgi_starting_tutorial_001.png?alt=media\&token=712011d7-d289-486a-a992-6080a9f4bb04)

### The RetroSwordsman AI Brain Structure

Select the *RetroSwordsman* 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: *Patrol* and *Shooting*
* While in *Patrol* the swordsman will perform an *AIActionPatrol* action
* While in *Shooting* the swordsman will perform an *AIActionShoot* action
* The swordsman will exit the *Patrol* state if the target is within range (*AIDecisionDetectTargetRadius*)
* The swordsman will exit the *Shooting* state after a while (*AIDecisionTimeInState*)

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

* Two states
* Two transistions
* Two actions
* Two decisions

#### 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 *Patrol* and select *Apply*
3. Repeat the first two steps, creating another node called *Shooting*
4. On the *Patrol* node, click the *Set as starting state* button: this will set the *Patrol* 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-LwoGvqvfj9-CnIbRxdm%2F-LwoH_JCiJH8TyPaiDZX%2Fcorgi_starting_tutorial_002.png?alt=media\&token=ca3f4060-58ac-4f06-a08c-7a138c816f1c)

#### Adding Transitions

Each state has a single transition, so we are going to create two and connect them with *Patrol* and *Shooting*:

1. Right click on the Graph canvas and select *AI > Transition*: this will add a transition node
2. Click on the *transitions* output in *Patrol* state and drag it to the *Input* element of the transition node
3. Repeat the first two steps for the *Shooting* state

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-LwoGvqvfj9-CnIbRxdm%2F-LwoHeHxcUrJy1vxYpEA%2Fcorgi_starting_tutorial_003.png?alt=media\&token=0ec6e136-333f-4601-8eae-dc14fdf17328)

#### Adding AI Decisions

The original swordsman has two decisions, each one connected with a state, so let's add them to the graph.

For the *Patrol* state:

1. Right click on the graph canvas and select *AI > Decision > Detect Target Radius*
2. Set the *Radius* to 4
3. Set the *Target Layer* to *Player*
4. Connect the *Output* element to the *Decision* input of the patrol state *AI Transition* node

For the *Shooting* state:

1. Right click on the graph canvas and select *AI > Decision > Time In State*
2. Let the fields to their default values
3. Connect the *Output* element to the *Decision* input of the shooting state *AI Transition* node

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-LwoGvqvfj9-CnIbRxdm%2F-LwoHiIQXf08OLWsWVXq%2Fcorgi_staring_tutorial_004.png?alt=media\&token=35fdcce2-188d-42ea-843d-bfb3fa72d296)

#### Adding AI Actions

The swordsman has two actions, each one connected with a state: let's add them to the graph.

For the *Patrol* state:

1. Right click on the graph canvas and select *AI > Action > Patrol*
2. Check the *Avoid Falling* value
3. Set the *Hole Detection Offset* x value to 1
4. Connect the *Output* element to the *Actions* input of the *Patrol* state node

For the *Shooting* state:

1. Right click on the graph canvas and select *AI > Action > Shoot*
2. Check the *Aim At Target* value
3. Connect the *Output* element to the *Actions* input of the *Shooting* state node

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-LwoGvqvfj9-CnIbRxdm%2F-LwoHnOCHnsvkJ1noTZ8%2Fcorgi_starting_tutorial_005.png?alt=media\&token=316611af-6ddc-45f0-88d3-4dfabc6d25a3)

#### 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* ones:

1. Connect the *True State* of the patrol *AI Transition* to the *States in* input of the *Shooting* state
2. Connect the *True State* of the shooting *AI Transition* to the *States in* input of the *Patrol* state

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-LwoGvqvfj9-CnIbRxdm%2F-LwoHrh-P46k09d1rsTN%2Fcorgi_staring_tutorial_006.png?alt=media\&token=0600b184-2e8b-4bb9-a912-bddde6897fd2)

### Generating the Character AIBrain System

We are now ready to add the brain graph to a gameObject and generate all the Corgi 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 Corgi:

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

<div align="center"><img src="https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-LwoGvqvfj9-CnIbRxdm%2F-LwoHw_ukns-ngMfEKRI%2Fcorgi_staring_tutorial_007.png?alt=media&amp;token=f5ccae0c-9f23-4827-899c-c57760d12afa" alt=""></div>

{% 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 *RetroSwordsmanPluggable* gameObject
2. Click the *Add Component* button and select *The Bit Cave > AI > AI Brain Pluggable*
3. Drag the *RetroSwordsman AIBrain* 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. Hit the Editor *Play* button and you should see the Corgi AI Brain structure generated

![](https://1480578063-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LwY4qXRelZyML_CLwPb%2F-LwoGvqvfj9-CnIbRxdm%2F-LwoI3Uzp8BX-bnW7N53%2Fcorgi_staring_tutorial_008.png?alt=media\&token=ccff089b-bb0c-416f-92ed-088e4236a39f)

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