S&box Wiki

Revision Difference


<cat>Code.Misc</cat> <cat>Code.Network</cat> <title>Commands</title> 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. 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.Serverwill 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. ```csharp [ConCmd.Server( "test_servercmd" )] public static void TestServerCmd( string parameter ) { 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 {Local.Client.Name}" ); } ``` ## 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 only supported parameter types are: `float, double, uint, int, bool, string` # 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 )](Sandbox.Client.SendCommandToClient). If you call a method marked with ConCmd.Server on the client then it will automatically call it as a console command. ```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. ``` <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>