Console Variables

How to read & write console variables (ConVars).

Overview

Console variables (ConVars) are the primary way to configure game settings and plugin behavior. With the new class support, you can work with ConVars in two ways:

  • Class-based approach - Using the ConVar wrapper class for cleaner, object-oriented code
  • Function-based approach - Traditional C-style functions with explicit handles

Finding a ConVar

Use the ConVar.Find static method to get a ConVar instance:

var cheatsCvar = ConVar.Find("sv_cheats");
if (cheatsCvar != null && cheatsCvar.Valid()) {
    // Use the ConVar instance
}

Manipulating Primitive Values

Reading and setting values is more intuitive with methods:

var cheatsCvar = ConVar.Find("sv_cheats");

// Read value using typed methods
bool value = cheatsCvar.GetBool();

// Set value using typed methods
cheatsCvar.SetBool(true);

// You can also use the generic Set method
cheatsCvar.Set(true);

Manipulating Objects

String values are handled through typed methods:

var stringCvar = ConVar.Find("sv_skyname");

// Get string value
string skyName = stringCvar.GetString();
PrintToServer($"sv_skyname = {skyName}");

// Set string value
stringCvar.SetString("foobar");

Creating ConVars

Create ConVars using the class constructor with automatic type detection:

// Create different types of ConVars
var stringCvar = new ConVar("my_plugin_name", "DefaultName", "Player name", ConVarFlags.None);
var intCvar = new ConVar("my_plugin_maxplayers", 32, "Max players", ConVarFlags.None);
var floatCvar = new ConVar("my_plugin_speed", 1.5f, "Speed multiplier", ConVarFlags.None);
var boolCvar = new ConVar("my_plugin_enabled", true, "Enable plugin", ConVarFlags.None);

// Use the created ConVars
if (boolCvar.GetBool()) {
    int maxPlayers = intCvar.GetInt32();
    PrintToServer($"Plugin enabled with {maxPlayers} max players\n");
}

Complete Example

using Plugify;
using static s2sdk.s2sdk;

public unsafe class ConVarDemo : Plugin
{
    private ConVar speedCvar;
    private ConVar gravityCvar;

    public void OnPluginStart()
    {
        // Create custom ConVars
        speedCvar = new ConVar("demo_speed", 1.0f, "Player speed multiplier", ConVarFlags.None);
        gravityCvar = new ConVar("demo_gravity", 1.0f, "Gravity multiplier", ConVarFlags.None);

        // Hook ConVar changes
        speedCvar.HookChange(OnSpeedChanged);

        // Find existing game ConVars
        var cheatsCvar = ConVar.Find("sv_cheats");
        if (cheatsCvar != null && cheatsCvar.Valid())
        {
            bool cheatsEnabled = cheatsCvar.GetBool();
            PrintToServer($"Cheats enabled: {cheatsEnabled}\n");
        }
    }

    public void OnPluginEnd()
    {
        // Unhook changes
        if (speedCvar != null && speedCvar.Valid())
        {
            speedCvar.UnhookChange(OnSpeedChanged);
        }
    }

    private static void OnSpeedChanged(ulong handle, string oldValue, double newValue)
    {
        PrintToServer($"Speed changed from {oldValue} to {newValue}\n");
    }
}