S&box Wiki

Commands

Commands

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 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 [ServerCmd], [AdminCmd] or [ClientCmd].

ServerCmd

A ServerCmd 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.

[ServerCmd( "test_servercmd" )] public static void TestServerCmd( string parameter ) { Log.Info( $"Caller is {ConsoleSystem.Caller} - {parameter}" ); }

AdminCmd

AdminCmd can be used in place of ServerCmd - as the name suggests it ensures only admins can run this command.

ClientCmd

ClientCmd runs on the client that runs it, this can be executed on the client by the server.

[ClientCmd( "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 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 ).

If you call a method marked with ServerCmd on the client then it will automatically call it as a console command.

[ServerCmd( "example" )] public static void Example( string parameter ) { } // Calling this clientside Example( "hello" ); // Gets replaced with ConsoleSystem.Run( "example hello" ); // Which runs Example serverside.
You can not call engine console commands with ConsoleSystem.Run or Client.SendCommandToClient - you can only call commands added by the game itself.

Special Pages


Wikis

?

Render Time: 622ms

DB GetPage 74
Generate Html 188
SaveChanges (1) 151
Render Body 0
Render Sidebar 16