Skip to main content

ConVar

Wraps a Source 2 console variable (cvar). Use Find to look up existing cvars or Create to register new ones.

public sealed class ConVar

Namespace: DeadworksManaged.Api
Source: ConVar.cs#L8

Notes​

Unlike typing a value in the console, the Set methods here also work on cheat-protected cvars while sv_cheats is off.

Properties​

ConVar.IsValid​

Signature

public bool IsValid { get; }

Source: ConVar.cs#L13

Methods​

ConVar.Find()​

Looks up an existing ConVar by name. Returns null if not found.

Signature

public static ConVar? Find(string name)

Source: ConVar.cs#L16

ConVar.Create()​

Creates a new ConVar registered with the engine. Returns null if creation fails.

Signature

public static ConVar? Create(string name, string defaultValue, string description = "", bool serverOnly = false)

Source: ConVar.cs#L25

ConVar.GetInt()​

Gets the cvar's value as an integer.

Signature

public int GetInt()

Source: ConVar.cs#L39

ConVar.GetFloat()​

Gets the cvar's value as a float.

Signature

public float GetFloat()

Source: ConVar.cs#L41

ConVar.GetBool()​

Gets the cvar's value as true or false. Any non-zero number counts as true.

Signature

public bool GetBool()

Source: ConVar.cs#L43

ConVar.GetString()​

Gets the cvar's value as a string.

Signature

public string GetString()

Source: ConVar.cs#L45

ConVar.SetInt()​

Sets the cvar's value as an integer.

Signature

public void SetInt(int value)

Source: ConVar.cs#L51

ConVar.SetFloat()​

Sets the cvar's value as a float.

Signature

public void SetFloat(float value)

Source: ConVar.cs#L53

ConVar.SetBool()​

Sets the cvar's value to true or false (1 or 0 on a number cvar).

Signature

public void SetBool(bool value)

Source: ConVar.cs#L55

ConVar.SetString()​

Sets the cvar from text. The text is converted to whatever type the cvar holds, so this works for number, bool and text cvars alike. Returns false if the text isn't a valid value for this cvar, like "abc" for a number cvar.

Signature

public bool SetString(string value)

Notes A value outside the cvar's allowed range is clamped to that range.

Source: ConVar.cs#L63

ConVar.SetValue()​

Sets the cvar from a value of any type, the same way SetString does: numbers are written out as text and bools as 1 or 0. Returns false if the value doesn't fit this cvar's type. Handy for applying a dictionary of settings in one loop.

Signature

public bool SetValue(object value)

Example

var settings = new Dictionary<string, object> { ["sv_cheats"] = true, ["sv_gravity"] = 600, ["hostname"] = "My server" };
foreach (var (name, value) in settings) {
if (ConVar.Find(name)?.SetValue(value) != true)
Console.WriteLine($"Couldn't set {name} to {value}");
}

Source: ConVar.cs#L84