> For the complete documentation index, see [llms.txt](https://giantgrey.gitbook.io/databrain/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://giantgrey.gitbook.io/databrain/add-ons/logic/finite-state-machine/examples.md).

# Examples

The Logic add-on comes with two dedicated FSM examples.

## FSM AI - Demo

{% hint style="warning" %}
This demo scene requires some setup to make sure it works properly.
{% endhint %}

### Setup

1. Make sure you have two layers in your project. (Player & Obstacles)
2. Assign the Player layer to the player game object in the scene.
3. Assign the Obstacles layer to the Obstacles group and its children.
4. Open the Data Library of the demo scene and select the Agent Data. Assign the Player layer to the target layer and the Obstacles to the obstacles field.

### Agent AI Component

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FxhVsPwMXZZP9246ICO2N%2Fstatemachine_example_agentAIComponent.png?alt=media&amp;token=d6bdb4f3-b479-48df-b14b-58eaa59c0b21" alt=""><figcaption></figcaption></figure></div>

The Agent AI Component is a component which is assigned on the enemy game object. It contains all relevant data and scene references we need for the FSM to work. This way we only need one scene component reference for the graph. All example AI nodes (Follow Waypoints, Follow Target, Vision) rely on the AgentAIComponent. The AgentAIComponent script is assigned on the Scene Components of the Logic Controller.

### Agent Data

<figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2Ft2YrcTSMQQt1H759slIm%2Fstatemachine_agentData.png?alt=media&amp;token=13c3cb1e-544c-4e9f-9b70-84ab5a582ee7" alt=""><figcaption></figcaption></figure>

The AgentData is a simple DataObject which contains all Agent relevant data, such as walk speed, view distance and so forth. The AgentData is assigned on the AgentAIComponent script.

### FSM

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FHznv9AYwn6Q5h34ctWCr%2Fstatemachine_fsmai_exp.png?alt=media&amp;token=8a57038d-77a9-4680-9887-25a61c3a15cd" alt=""><figcaption></figcaption></figure></div>

#### FSM - Patrol

The FSM in this demo is very straight forward. As you can see we have three states (**Patrol**, **Follow** and **Wait**).\
When a FSM Controller gets started, it will always start with the first(top) state. \
On the **Patrol** state two FSM actions are being executed, the **Follow Waypoints** and **Vision** actions.\
Each actions has appropriate outputs. In the **Patrol** state we only wait for a target (Player) to be detected. If this happens the **Switch State** node is being called which changes the current state to the **Follow** state.

#### FSM - Follow

The follow state executes two FSM actions, **Follow Target** and **Vision**. The **Follow Target** action simply follows the target which is assigned on the **AgentAIComponent** in the scene. When target has been reached, it will call the target reached output. In this case we have a simple **Debug.Log** node. But logically for production we would rather switch to an attack state. On the **Vision** action we only wait for if the target is out of sight (Target Lost). If so, we then switch to a **Wait** state. Which then switches back to the **Patrol** state.

#### FSM - Wait

The **Wait** state does not have any FSM action nodes. Instead we're using an **Action Split** node which splits the FSM actions into three calls - **OnEnter**, **OnUpdate** and **OnExit**. As we want to execute the wait only once, we connect our nodes to the **OnEnter** output. First we stop the agent with the **StopAgent** node. Then we wait for a few seconds, before switching the current state from **Wait** back to **Patrol**.

## FSM Wait - Demo

<figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2Fw2TtFb1jDCyFwGMoxxYQ%2Fstatemachine_fsm_wait.png?alt=media&amp;token=4575c2b1-80a7-45b5-af5b-e0654ce24f6b" alt=""><figcaption></figcaption></figure>

The second demo is a very simply state machine which waits for an input for a certain time. It nicely shows the use of FSM action nodes in combination with conventional Logic nodes.
