terminalCode Example

ConVar and ConCmd — console variables and commands with flags and game settings

calendar_today May 5, 2026 schedule ~1 min read person patrickjr verified 50

ConVar and ConCmd — Console Variables and Commands

[ConVar] and [ConCmd] are s&box attributes for exposing variables and commands to the developer console.

Console Commands

CSHARP
// Basic command
[ConCmd( "kill_all_npcs" )]
static void KillAllNpcs()
{
    foreach ( var npc in Game.ActiveScene.GetAll<NpcHealth>() )
        npc.Die();
}

// Command with arguments
[ConCmd( "set_gravity" )]
static void SetGravity( float value )
{
    // Backend converts string args to the declared type
    Scene.PhysicsWorld.Gravity = Vector3.Down * value;
}

// Server-only command — always runs on the host
[ConCmd( "kick_player", ConVarFlags.Server )]
static void KickPlayer( Connection caller, string targetName )
{
    Log.Info( $"{caller.DisplayName} tried to kick {targetName}" );
}

Console Variables

CSHARP
// Basic bool toggle
[ConVar]
public static bool debug_bullets { get; set; } = false;

// Saved to disk — persists between sessions
[ConVar( "bullet_count", ConVarFlags.Saved )]
public static int BulletCount { get; set; } = 6;

// Replicated — host controls it, value synced to all clients
[ConVar( "friendly_fire", ConVarFlags.Replicated )]
public static bool FriendlyFire { get; set; } = false;

// UserInfo — sent to host in Connection.UserInfo
[ConVar( "view_mode", ConVarFlags.UserInfo )]
public static string ViewMode { get; set; } = "firstperson";

// Hidden — not shown in autocomplete or find
[ConVar( "secret_mode", ConVarFlags.Hidden )]
public static int SecretMode { get; set; } = 0;

Game Settings

ConVarFlags.GameSetting exposes the variable in the game creation screen:
CSHARP
// Shows as a slider in the lobby creation UI
[ConVar( "player_speed", ConVarFlags.GameSetting ), Range( 50f, 1024f, 1f )]
public static float PlayerSpeed { get; set; } = 250f;

Reading ConVars in Code

CSHARP
// Access directly as a static property
if ( FriendlyFire )
    ApplyDamage( target, info );

// Or read by name at runtime
var val = ConsoleSystem.GetValue( "friendly_fire" );

Combining Flags

CSHARP
[ConVar( "max_players", ConVarFlags.Replicated | ConVarFlags.Saved | ConVarFlags.GameSetting )]
public static int MaxPlayers { get; set; } = 16;
Was this helpful?