S&box Wiki

Revision Difference

Commands#561162

<cat>Code.Network</cat>⤶ <cat>Code.Input</cat>⤶ <title>Commands</title> <warning>This page was written for the entity system and may be outdated</warning>⤶ Commands are easy to create and can be called from the console of the server or clients, they are mainly useful for administrative and debugging purposes. 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.⤶ Alternatively [RPCs](RPCs) can be used to transmit data from the server to clients with lesser restrictions then commands.⤶ ⤶ ⤶ # Creating commands Commands can be created on any static method by applying a method attribute `[ConCmd.Server]`, `[ConCmd.Admin]` or `[ConCmd.Client]`.⤶ ⤶ ## ConCmd.Server⤶ ⤶ A ConCmd.Server will run on the server and can be executed by the server itself or any client. Within the method ConsoleSystem.Caller returns the client running the command, it will be null if the server has run the command itself or through rcon.⤶ Commands can be created on any static method by applying the method attribute `[ConCmd]`⤶ ```csharp [ConCmd.Server( "test_servercmd" )] public static void TestServerCmd( string parameter ) [ConCmd( "test_command" )] public static void TestCommand( string input ) { Log.Info( $"Caller is {ConsoleSystem.Caller} - {parameter}" ); }⤶ ```⤶ ⤶ ## ConCmd.Admin⤶ ⤶ ConCmd.Admin can be used in place of ConCmd.Server - as the name suggests it ensures only admins can run this command.⤶ ⤶ ## ConCmd.Client⤶ ⤶ ConCmd.Client runs on the client that runs it, this can be executed on the client by the server.⤶ ⤶ ```csharp⤶ [ConCmd.Client( "test_clientcmd" )]⤶ public static void TestClientCmd()⤶ {⤶ Log.Info( $"Hello I am {Game.LocalClient.Name}" );⤶ 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 server or client's console by name.⤶ They can also be run with code: `ConsoleSystem.Run( string command, )` - this will run a console command on either the server or client.⤶ ⤶ Commands can also be called on a client from the server with [Client.SendCommandToClient( string command )](https://asset.party/api/Sandbox.IClient.SendCommandToClient(string)).⤶ ⤶ If you call a method marked with ConCmd.Server on the client then it will automatically call it as a console command.⤶ Obviously commands can be run directly from the console by name, but they can also be run via ConsoleSystem.Run⤶ ```csharp [ConCmd.Server( "example" )]⤶ public static void Example( string parameter ) { } ⤶ // Calling this clientside⤶ Example( "hello" ); ⤶ // Gets replaced with⤶ ConsoleSystem.Run( "example hello" );⤶ ⤶ // Which runs Example serverside.⤶ protected override void OnStart()⤶ { ConsoleSystem.Run( "test_command", "hi!" );⤶ } ``` <warning>You can not call engine console commands with ConsoleSystem.Run or Client.SendCommandToClient - you can only call commands added by the game itself.</warning> <warning>You can not call engine console commands with ConsoleSystem.Run - you can only call commands added by the game itself.</warning>