menu_bookDocumentation
ConVar and ConCmd: Console Variables and Commands in s&box
ConVar and ConCmd: Console Variables and Commands
s&box uses [ConVar] and [ConCmd] attributes on static properties and methods to expose them to the console system.
ConVar (Console Variable)
CSHARP
// Basic ConVar
[ConVar( "my_speed" )]
public static float MySpeed { get; set; } = 300f;
// With flags and help text
[ConVar( "my_debug", ConVarFlags.Protected, Help = "Enable debug mode" )]
public static bool DebugMode { get; set; } = false;
// Saved to config file
[ConVar( "my_volume", ConVarFlags.Saved )]
public static float Volume { get; set; } = 1.0f;
// With min/max clamping
[ConVar( "my_fov", Min = 60f, Max = 120f )]
public static float FieldOfView { get; set; } = 90f;ConCmd (Console Command)
CSHARP
// Basic command
[ConCmd( "my_command" )]
public static void MyCommand( string arg1, int arg2 = 0 )
{
Log.Info( $"Called with {arg1}, {arg2}" );
}
// Admin-only command
[ConCmd( "kick_player", ConVarFlags.Admin )]
public static void KickPlayer( string name )
{
// Only runs if caller is host
}
// Access the caller connection
[ConCmd( "my_cmd" )]
public static void MyCmd( Connection caller, string arg )
{
// First parameter of type Connection receives the caller
Log.Info( $"{caller.DisplayName} called with {arg}" );
}ConVarFlags
| Flag | Effect |
|---|---|
| Protected | Cannot be changed by game code |
| Saved | Persisted to config file via CookieContainer |
| Replicated | Server value is replicated to clients |
| UserInfo | Client value is sent to server (accessible via Connection userinfo) |
| Cheat | Requires sv_cheats 1 |
| Admin | Only host can run |
| Hidden | Not visible in autocomplete |
| Server | Only runs on server |
Name Validation
ConVar/ConCmd names must contain only ASCII letters, digits, underscores, dots, or hyphens. Spaces, semicolons, and quotes are not allowed.
Was this helpful?