# Bots

Bots (also known as "simplayers") are fake players that help creators representatively test game performance, rendering and networking by simulating high player counts.

## **Adding bots to a world** <a href="#part-1-spawning-bots" id="part-1-spawning-bots"></a>

You can add bots directly to a launched world by navigating to the "Operations" tab on your project Dashboard.

<figure><img src="/files/uSo2jjGnE4ubZDmRClQM" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/hL1CvKwxTY114Zj7grty" alt=""><figcaption></figcaption></figure>

After a few minutes, you should see bots start to appear in your world

<figure><img src="/files/PgcSDqAMADakWY56DGsQ" alt=""><figcaption></figcaption></figure>

## **Adding bots locally** <a href="#part-1-spawning-bots" id="part-1-spawning-bots"></a>

In the Play dropdown, set the `Number of Players` to `2` (or more) players.

<figure><img src="/files/sYNMd5M8FA6AFPLIKZnB" alt=""><figcaption></figcaption></figure>

Open `Editor Preferences`, search for `Editor Client Connection Types`

First, add `Player` connection type, then a `Bot` connection type. If you added more than 2 players in the previous step, add more `Bot` connection types. If you want multiple bots per bot client, you can specify this in the `NumOfBots` field.

<figure><img src="/files/u6scpZeqU7jxbzub59Aa" alt=""><figcaption></figcaption></figure>

Pressing `Play` will now spawn an additional window for each bot connection

<figure><img src="/files/HJnQrKDn3PeThkb4t3Fq" alt=""><figcaption></figcaption></figure>

## **Bot behaviors** <a href="#part-2-performing-behaviours" id="part-2-performing-behaviours"></a>

### **Overview**

Bot behaviours describe what actions the bot takes after spawning.

In the `Outliner` panel, ensure your level has a `BotBehaviorStore` (if not, add one).

The bot behavior store controls what behavior is run on the bots, based on what command you give them.

<figure><img src="/files/rWRe4kc6GixHiOunwIa7" alt=""><figcaption><p>In this example, the <code>emote</code> command will run the <code>BT_BotEmote</code> behavior tree, and so on.</p></figcaption></figure>

In a PiE session, press `` ` `` to open the console.

Type `Morpheus.Bots.List` to list available bot behaviours in the Output Log.

<figure><img src="/files/QtTAlcPd2O6hEBPidbz1" alt=""><figcaption></figcaption></figure>

Enter `Morpheus.Bots.Run emote` (or another entry), and all running bots should change their behaviour accordingly.

<figure><img src="/files/86ZNAws1oTP4Pdj0XTEj" alt=""><figcaption></figcaption></figure>

### **Setting a "default behavior"**

The "default bot behavior" is controlled via live config: `Bots.BotDefaultBehaviour` (in the `Game` config). Whatever value this is set to, bots will start up running that behavior.

(If this value is set to be empty, then the bots will not run any behavior until you explicitly tell them to)

### **Adding new behaviors**

Bot behaviors are implemented using Unreal behaviour trees. To implement behavior trees, you can reference the existing behaviors or consult the Unreal docs:

* [![](https://docs.unrealengine.com/4.27/Include/Images/site_icon.png)Behavior Tree Overview](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/ArtificialIntelligence/BehaviorTrees/BehaviorTreesOverview/)
* [![](https://docs.unrealengine.com/4.27/Include/Images/site_icon.png)Behavior Tree Quick Start Guide](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/ArtificialIntelligence/BehaviorTrees/BehaviorTreeQuickStart/)
* [![](https://docs.unrealengine.com/4.27/Include/Images/site_icon.png)Behavior Tree User Guide](https://docs.unrealengine.com/4.27/en-US/InteractiveExperiences/ArtificialIntelligence/BehaviorTrees/BehaviorTreeUserGuide/)

Once created, add your behavior tree to the list of behaviors on the bot behaviour store in your level (giving it a unique name).

<figure><img src="/files/W9xfDoDFViHw9k2Eb09d" alt=""><figcaption></figcaption></figure>

In a PiE session, type `Morpheus.Bots.List` and you should see your new behavior name.

Type `Morpheus.Bots.Run <behaviour_name>` and all bots should start to execute your behaviour tree

#### Teardown behaviors

Since behaviors can be stopped at any point during their behavior tree (e.g. if you call `Morpheus.Bots.Run [Some other behavior]` whilst one is running, we can get into bad states if any logic was set for the duration of the test that should not be present when running other behaviors.

Our solution to this was adding `TearDownBehaviors` to the behavior store. This behavior is run once you switch away from a given behavior, so can be used to clean up any state the behavior used.

* If you have a behavior `[behavior_name]` in your behavior store's `Behaviors` map, that requires teardown logic, add an entry to the `TearDownBehaviors` map, with a matching `[behavior_name]`
* Do your cleanup logic in that teardown behavior tree. Once you are done, call `BTT_MarkTearDownComplete` to inform the bot manager system that the teardown of the previous behavior has finished.
* The bot will then move on to running the next behavior.

{% hint style="info" %}
NOTE: Any teardown behaviors must include the `BTT_MarkTearDownComplete` at the end of their behavior trees. This marks that the teardown is complete, and so we can move on to the next behavior. Otherwise, the bots will be stuck in their "tearing down" state forever.
{% endhint %}

<figure><img src="/files/3DatXnn0fp3aXuNWpPKX" alt=""><figcaption><p>The <code>roles</code> teardown behavior (<code>BT_BotSwitchRolesTeardown</code>) is run when you stop running the <code>roles</code> behavior, and makes sure that the bots return to the default role, rather than being stuck in whatever role the running behavior had previously set them to.</p></figcaption></figure>

## Using State Tree

{% hint style="info" %}
NOTE: The StateTree plugins have been enabled for MSquared as of release v35.
{% endhint %}

[![](https://docs.unrealengine.com/4.27/Include/Images/site_icon.png) StateTree Unreal Documentation](https://dev.epicgames.com/documentation/en-us/unreal-engine/state-tree-in-unreal-engine)

Unreal's StateTree system can also be used to run bot behaviors, in a different setup to our existing setup. If you want to use this, you can, but we would recommend you disabling the existing bots system (turn off the default behavior in [#setting-a-default-behavior](#setting-a-default-behavior "mention") to make sure the two systems are not conflicting). Otherwise, this system should largely work in MSquared in the same way as native Unreal.

Some things to take care with:

* Any replication will still need to be handled through Morpheus Actors
* If the state tree is set to start automatically, the logic may run before the bots have set up correctly. The bots are given their appropriate bot controller via the morpheus actor, and this may not be set up when the character starts running its behavior tree. This can be handled by waiting for the appropriate controller, e.g. by using the bootflow\\

  <figure><img src="/files/JdWPxBuvKcOKhBTFllsw" alt=""><figcaption></figcaption></figure>
* Make sure that the state tree is only run on bot clients!

## Calling server RPCs

In test deployments, multiple bots can run on the same cloud instance, each pretending to be a player with their own client connection. That means that multiple client connections share an Unreal client.

This introduces a complication: when client code invokes an [RPC](/creation/unreal-development/getting-started/networking/morpheus-rpcs.md) on the server, it's not always obvious which of these client connections the RPC should go via.

For example, suppose you have a [singleton](/creation/unreal-development/features-and-tutorials/singletons.md) `AMorpheusActor` with a server RPC, let's say `KillCallingPlayer`. The RPC's handler function will rely on `ServerGetRpcCallerConnection` to securely identify which player to kill. By default, the RPC will be sent via the client connection which created the singleton. However, when multiple bots share an Unreal client, they also share the client's singletons, so by default it's probably the wrong bot who'll get killed.

To get round this problem, you can use the `AMorpheusActor` function `PushRpcSenderConnection` to specify which client connection to use, before calling your RPC. Make sure to either set its `PopAfterNextRpc` flag or else clean up manually by calling `PopRpcSenderConnection` after calling the RPC. For example:

<figure><img src="/files/n0OntUyJr4BD7YkpaNm4" alt=""><figcaption><p>PushRpcServerConnection on client, ServerGetRpcCallerConnection on server</p></figcaption></figure>

N.B. The default client connection used for an RPC is the one which was used to created the `AMorpheusActor` which the RPC belongs to. That means that you don't need to bother with this step if the RPC belongs to a `PlayerCharacter` actor - each bot's own character gets created by its own client connection.


---

# 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://docs.msquared.io/creation/unreal-development/features-and-tutorials/bots.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.
