Console Variables

How to read & write console variables (ConVars).

Finding a ConVar

Use the FindConVar static method to find a reference to an existing ConVar handle (or null).

var cheatsCvar = FindConVar("sv_cheats");

Manipulating Primitive Values

Reading the value of a ConVar will depend on the type; for basic value Cvars (like float, int, bool) you can use the GetConVar method to get a copy as the value.

var value = GetConVarBool(cheatsCvar); // false

Because this is passed by copy, you can't simply set this value to change the underlying value, e.g. You need to use the SetConVar method to change the value of a ConVar.

SetConVarBool(cheatsCvar, true, /*replicate*/true, /*replicate*/false);

// You can also use the simplified helper:
SetConVar(cheatsCvar, true);

Manipulating Objects

By default, the GetConVar method will return a string value for string Cvars. You can use the GetConVarString method to get a String object, which you can then manipulate as normal. To set the value back, you will need to use the SetConVarString method.

var stringCvar = FindConVar("sv_skyname");
var skyName = GetConVarString(stringCvar);
PrintToServer($"sv_skyname = {skyName}");

// You can then manipulate the string as normal.
skyName = "foobar";

SetConVarString(stringCvar, skyName);

Creating ConVars

You can create your own ConVars using the CreateConVar method. This is useful for creating custom settings for your plugin.

var cvar = CreateConVar("my_plugin_cvar", "default_value", "This is my custom ConVar", ConVarFlags.None);