Scripting Cvars (AMX Mod X)
CVARs, or Console VARiables, are an easy and effecient way to incorporate user defined variations within code. Internally, all CVAR's are stored as strings, but can be recovered in any format.
CVARs come in two flavors: server and client.
Contents
Server Side CVARs
Server side CVARs are stored within the HL1 engine, and can be altered through server console commands.
Usage
Server side CVARs are easy to create and manipulate.
Creation
register_cvar(const name[],const string[],flags = 0,Float:fvalue = 0.0);
Note: The last parameter is for backwards compatibility only, and serves no purpose in AMXx. It is recommended you register CVARs during plugin_init, so that other plugins may alter them at plugin_cfg
- Parameters:
- name[] : The name of the CVAR you want to register
- string[] : The default value of the CVAR
- flags : See CVARs in AMXx#flags.
Getting Value
/* Gets a cvar float. */ Float:get_cvar_float(const cvarname[]);
/* Gets a cvar integer value. */ get_cvar_num(const cvarname[]);
/* Reads a cvar value. */ get_cvar_string(const cvarname[],output[],iLen);
Setting Value
/* Sets a cvar to given value. */ set_cvar_string(const cvar[],const value[]);
/* Sets a cvar to given float. */ set_cvar_float(const cvar[],Float:value);
/* Sets a cvar with integer value. */ set_cvar_num(const cvarname[],value);
Flags
These flags can be used for the third parameter in the register_cvar native:
/* Flags for register_cvar() */
- define FCVAR_ARCHIVE 1 /* set to cause it to be saved to vars.rc */
- define FCVAR_USERINFO 2 /* changes the client's info string */
- define FCVAR_SERVER 4 /* notifies players when changed */
- define FCVAR_EXTDLL 8 /* defined by external DLL */
- define FCVAR_CLIENTDLL 16 /* defined by the client dll */
- define FCVAR_PROTECTED 32 /* It's a server cvar, but we don't send the data since it's a password, etc. Sends 1 if it's not bland/zero, 0 otherwise as value */
- define FCVAR_SPONLY 64 /* This cvar cannot be changed by clients connected to a multiplayer server. */
- define FCVAR_PRINTABLEONLY 128 /* This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ). */
- define FCVAR_UNLOGGED 256 /* If this is a FCVAR_SERVER, don't log changes to the log file / console if we are creating a log */
One may also alter these flags dynamically, using the flag natives: /* Returns a cvar flags. */ get_cvar_flags(const cvar[]);
/* Sets a cvar flags (not allowed for amx_version,
- fun_version and sv_cheats cvars). */
set_cvar_flags(const cvar[],flags);
/* Removes a cvar flags (not allowed for amx_version,
- fun_version and sv_cheats cvars). */
remove_cvar_flags(const cvar[],flags = -1);
These allow you to dynamically alter flags; dynamic protection, dynamic logging, etc etc.
Client Side CVARs
Client side CVARs, like server side CVARs, allow bits of code to be dynamic. However, since they are in the client, we cannot create such CVARs, only alter them.
Usage
Setting
Setting a client side CVAR is easy; execute a new value onto the client:
client_cmd(player_id,"cvar value")
Getting
Recently, VALVE added a client CVAR checking interface, which has been added to AMXx:
// Dispatches a client cvar query // id: Player id // cvar: cvar name // resultFunc: public handler function // paramLen + params: optional array parameter // resultFunc looks like: // public callbackCvarValue(id, const cvar[], const value[]) // or if you use the optional parameter: // public callbackCvarValue(id, const cvar[], const value[], const param[]) query_client_cvar(id, const cvar[], const resultFunc[], paramlen=0, const params[] = "");
This native may appear daunting, but it is actually quite simple; the first parameter is the player, the second the CVAR, the third the function called which will contain the client CVAR, and the last two are for allowing the passage of strings.
If you wish to get an array or strting back, simply put the length you want it, and fill params will a value. Then, in your return function, you will receive an array.
Locking
Unfortunately, the client can change CVAR values back, unless we lock them. Since alias's are executed before CVARs, we can create an alias of the same name of the CVAR, thus locking the CVAR:
CVAR: cvar_ex.
client_cmd(player_id,"cvar_ex 1")
client_cmd(player_id,";alias cvar_ex")
Now the CVAR cannot be changed by the player or the server; it is locked.