cvar
The cvar namespace allows querying and modifying Source engine console variables (ConVars).
Finding ConVars
Section titled โFinding ConVarsโConVars can be accessed directly as properties on the cvar table or via cvar:find:
-- Direct property lookuplocal sv_cheats = cvar.sv_cheats
-- Method lookuplocal max_ticks = cvar:find("sv_maxusrcmdprocessticks")convar Methods
Section titled โconvar MethodsโConVar setter methods return the previous value before modification, making restoration on unload straightforward:
local previous_val = cvar.sv_cheats:int(1) -- Sets to 1 and returns previous integer| Method | Parameters | Returns | Description |
|---|---|---|---|
cv:int |
value?: number |
number |
Reads (or sets if passed) the integer value. Returns old value on write. |
cv:float |
value?: number |
number |
Reads (or sets if passed) the float value. Returns old value on write. |
cv:string |
value?: string |
string |
Reads (or sets if passed) the string value. Returns old value on write. |
cv:get_name |
(none) | string |
Returns the ConVarโs registration name. |
cv:add_flag |
flag: number |
nil |
Adds engine flag bits (e.g. FCVAR_CHEAT = 1 << 14). |
cv:remove_flag |
flag: number |
nil |
Strips engine flag bits (e.g. removing FCVAR_CHEAT or FCVAR_HIDDEN). |
ConVar Properties
Section titled โConVar Propertiesโ| Property | Type | Description |
|---|---|---|
description |
string |
ConVar help description. |
flags |
number |
Bitmask of active FCVAR_* flags. |
default |
string |
Default value string. |
has_min |
boolean |
true if a minimum clamping limit exists. |
has_max |
boolean |
true if a maximum clamping limit exists. |
min |
number |
Minimum numeric value. |
max |
number |
Maximum numeric value. |
Safe Restoration Example
Section titled โSafe Restoration Exampleโlocal sv_cheats = cvar.sv_cheatslocal original_val = sv_cheats:int()
-- Enable sv_cheatssv_cheats:int(1)
client.add_callback("unload", function() -- Restore original value on script unload sv_cheats:int(original_val)end)