S&box Wiki

Revision Difference

Commands#563208

<cat>Code.Input</cat>⤶ <title>Commands</title>⤶ ⤶ ⤶ Commands are easy to create and can be called from the console, they are mainly useful for administrative and debugging purposes.⤶ ⤶ Alternatively [RPCs](RPCs) can be used to transmit data between the host and clients with less restrictions then commands.⤶ ⤶ ⤶ # Creating commands⤶ ⤶ Commands can be created on any static method by applying the method attribute `[ConCmd]`⤶ ⤶ ```csharp⤶ [ConCmd( "test_command" )]⤶ public static void TestCommand( string input )⤶ {⤶ Log.Info( input );⤶ }⤶ ```⤶ ⤶ ## Parameter Types⤶ ⤶ There is limited support for parameter types as they all need to be converted to a string and back - if you find this too limiting you should probably use [RPCs](RPCs) instead.⤶ ⤶ The following types are allowed: `float, double, int, unit, bool, string, long, ulong, Vector2, Vector3, Vector4, Angles, Color, RangedFloat, Rotation`.⤶ ⤶ Types can also be allowed if it satisfies one of the following conditions:⤶ * It is an enum.⤶ * Your type has an [implicit converter](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators).⤶ * Your type has a constructor with a single `string` parameter.⤶ ⤶ # Running commands⤶ ⤶ Obviously commands can be run directly from the console by name, but they can also be run via ConsoleSystem.Run⤶ ⤶ ```csharp⤶ protected override void OnStart()⤶ {⤶ ConsoleSystem.Run( "test_command", "hi!" );⤶ }⤶ ```⤶ ⤶ <warning>You can not call engine console commands with ConsoleSystem.Run - you can only call commands added by the game itself.</warning>⤶