# Finite State Machine

With version 1.1.0 Logic comes with state machine nodes which lets you easily create finite state machines. State machine nodes differ from conventional logic nodes as they can listen to **OnEnter**, **OnUpdate** and **OnExit** calls by the state machine controller.

<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&#x26;token=8a57038d-77a9-4680-9887-25a61c3a15cd" alt=""><figcaption><p>Simple state machine which waits for an input for a certain time.</p></figcaption></figure>

## Create a state machine

First we need to create a state machine controller node. This node runs the actual state.\
Right click on the node canvas and select: ***StateMachine / FSM Controller***

We can now add some state outputs in the node inspector. (for example Wait, Success, Fail)

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2F19ibRPrJMwgsROI9mM4B%2Fstatemachine_addStates.png?alt=media&#x26;token=62c76cf5-1bc8-40ab-aed6-b4966f981d65" alt=""><figcaption><p>Add new outputs (states) to the FSM controller node.</p></figcaption></figure></div>

{% hint style="info" %}
The "Outputs" UI which allows you to modify the outputs on a node can be added on any custom node by using the **\[NodeAddOutputsUI]**. Read more about it: [#nodeaddoutputsui](https://giantgrey.gitbook.io/databrain/add-ons/custom-nodes/node-attributes#nodeaddoutputsui "mention")
{% endhint %}

## Add state actions

To perform state actions we need to connect the output of a FSM Controller with an **Actions** node first. Then we can define as many action outputs as we like.

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2FaJj5mi07XyR7CyWYe2gg%2Fstatemachine_actions.png?alt=media&#x26;token=ed1507fc-e0bc-4302-a22e-26a6829b7bb2" alt=""><figcaption><p>WaitForInput is a state machine action node which is being executed by the actions node.</p></figcaption></figure></div>

A state machine action node differs from conventional Logic nodes as they have three state machine states: OnEnter, OnUpdate and OnExit. Here's an example code of an "empty" state machine action node:

```csharp
public class MyStateAction : StateMachineActionNode
{
    public override void OnEnter()
    {
        // On Enter
    }

    public override void OnExit()
    {
        // On Exit
    }

    public override void OnUpdate()
    {
        // On update is called by the state machine controller node on every tick
    }   
}
```

## Run conventional nodes

State machine nodes can be used along with the conventional logic nodes.\
To run conventional nodes you can use the action split node. As the name implies - the action split node splits the output of an actions node (OnEnter, OnUpdate, OnExit) and allows you to connect conventional nodes to each output.

* **OnEnter**: Called once when entering the state.
* **OnUpdate**: Called on every tick.
* **OnExit**: Called when exiting the state.

<div align="left"><figure><img src="https://2348672745-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIoqF5QAgQsqxrjmTIKpE%2Fuploads%2Febu5CXjKtML12UcXUr3p%2Fstatemachine_actionSplit.png?alt=media&#x26;token=b27936fd-92c0-4066-a47d-5fb9dfd8d504" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://giantgrey.gitbook.io/databrain/add-ons/logic/finite-state-machine.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
