S&box Wiki

Revision Difference

Bots#545855

<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. **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 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 { [AdminCmd( "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 Attack1 button. builder.SetButton( InputButton.Attack1, 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.