Revision Difference
Bots#548050
<cat>Code.Player</cat>
<title>Bots</title>
# What are bots?
Bots are fake clients that can be added to a game, and take a normal player slot. Their input and behaviour can be coded like most other things in C#.
# Default Bot
By default, the console command `bot_add` will add a bot that mimics another player's input. This command has some additional parameters you can use:
`bot_add <clientIndex> <yawOffset> <forceCrouch>`
**clientIndex** *(def: 1)* Which client to mimic. If set to 0, it will not mimic anything and remain idle.
**yawOffset** *(def: 180)* The bots yaw rotation relative to the target client.
**forceCrouch** *(def: 0)* Whether or not the bot is forced to crouch.
⤶
⤶
#Custom Bots⤶
⤶
<note>Dedicated servers will fail to spawn default bots with this message:⤶
`SV: Cheat command 'bot_add' ignored. Set sv_cheats to 1 enable cheats.` *(sic!)*</note> ⤶
⤶
#Custom Bots⤶
To create a custom bot you need to first extend the `Bot` class, which has two important methods: `Tick` and `BuildInput`.
## Example
It's worth noting that **the methods used below are only called server-side**, unlike ordinary clients whose input is built client-side and sent to the server.
```csharp
public class MyCustomBot : Bot
{
[ConCmd.Admin( "bot_custom", Help = "Spawn my custom bot." )]
internal static void SpawnCustomBot()
{
Host.AssertServer();
// Create an instance of your custom bot.
_ = new MyCustomBot();
}
public override void BuildInput( InputBuilder builder )
{
// Here we can choose / modify the bot's input each tick.
// We'll make them constantly attack by holding down the PrimaryAttack button.
builder.SetButton( InputButton.PrimaryAttack, true );
}
public override void Tick()
{
// Here we can do something with the bot each tick.
// Here we'll print our bot's name every tick.
Log.Info( Client.Name );
}
}
```
## Pawns
Each bot has a `Client` property referring to its underlying fake client. Every bot can have its own [Pawn](https://wiki.facepunch.com/sbox/Pawn) and otherwise do anything any other ordinary client could do.