# Using Master/Slave AIBrains

The **Master/Slave** system implements a set of methods to control AIBrains (set as *Slaves*) from other AIBrains (set as *Masters*) or other parts of the application through the Corgi/TopDown [Event System](https://corgi-engine-docs.moremountains.com/events.html).

### Using the System

The two main actors involved in this system are:

* **Brain Master**: not necessarily connected with a brain, it is used to send State change command events through dedicated channels
* **Brain Slave**: this element listens for State change command events and forces the AIBrain  behavior

{% hint style="info" %}
A Master can also be a Slave and listen to events (even those dispatched by itself).
{% endhint %}

#### &#x20;BrainMaster Ability

The **BrainMaster** ability component lets you send state change commands to slave brains through a dedicated channel: usually this is achieved through an *AIActionChangeAIBrainStateCommand* but you can access the *SendCommand* method:

```csharp
SendCommand(StateCommandChannel channel, string newStateName, Transform target = null)
```

* *channel* is used to filter who will receive and execute the command
* *newStateName* is the state the AIBrain should transition in
* *target* is the AIBrain target (if any)

{% hint style="info" %}
This ability doesn't extend the regular Corgi/TopDown abilities, so it doesn't need a Character component: this means you can use anywhere you need (i.e.: with a some trigger or any game logic).
{% endhint %}

#### The Brain Slave Ability

The **BrainSlave** ability component is used to receive state change commands through a dedicated channel.

{% hint style="info" %}
The slave can listen to one or more channels.
{% endhint %}

Whenever an event is received, the slave will try to change the AIBrain to the new state.

As an alternative, the state change can be also forced through the TransitionToState() method:

```csharp
TransitionToState(string newStateName, Transform target = null)
```

* *newStateName* is the state the AIBrain should transition in
* *target* is the AIBrain target (if any)

#### Channels

To use the Master/Slave communication system, you will have to create a **Channel**, that will be added to the slave channel list:

1. Select Create > The Bit Cave > MasterSlave > State Command Channel
2. Rename the newly created asset
3. Add it to your slave *Channels* list
4. Add the same channel to the master *AIActionChangeAIBrainStateCommand* component


---

# 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://thebitcave.gitbook.io/ai-brain-extensions-docs/master-slave-brains/untitled.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.
