Garry's Mod Wiki

cvars.AddChangeCallback

  cvars.AddChangeCallback( string name, function callback, string identifier = nil )

Description

Adds a callback to be called when the named convar changes.

This does not callback convars in the menu state.

Issue Tracker: 1440
This does not callback convars on the client with FCVAR_GAMEDLL and convars on the server without FCVAR_GAMEDLL.

Issue Tracker: 3503
This does not callback convars on the client with FCVAR_REPLICATED.

Issue Tracker: 3740

Arguments

1 string name
The name of the convar to add the change callback to.
2 function callback
The function to be called when the convar changes.
Function argument(s):
1 string convar - The name of the convar.
2 string oldValue - The old value of the convar.
3 string newValue - The new value of the convar.
3 string identifier = nil
If set, you will be able to remove the callback using cvars.RemoveChangeCallback.

The identifier is not required to be globally unique, as it's paired with the actual name of the convar.

Example

Called when convar changes.

cvars.AddChangeCallback("convar name", function(convar_name, value_old, value_new) print(convar_name, value_old, value_new) end)
Output: "convar name" 2 5