Skip to content

cvar

The cvar namespace allows querying and modifying Source engine console variables (ConVars).


ConVars can be accessed directly as properties on the cvar table or via cvar:find:

-- Direct property lookup
local sv_cheats = cvar.sv_cheats
-- Method lookup
local max_ticks = cvar:find("sv_maxusrcmdprocessticks")

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).

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.

local sv_cheats = cvar.sv_cheats
local original_val = sv_cheats:int()
-- Enable sv_cheats
sv_cheats:int(1)
client.add_callback("unload", function()
-- Restore original value on script unload
sv_cheats:int(original_val)
end)