This quick guide will show you what they are and how they behave.
Here's a great list of cvars: http://scripting.elxdraco.net/cvarlist/
What are they?
You can think of them as global (sort of) variables. You can retrieve their values, set new values, and also create new cvars. They differ from actual global variables, because you can change cvar values from the console.
You can store all sorts of stuff in your cvars. Typically, they are used to control plugins, turn them on/off, or to hold data temporarily. You can use the flags listed below to modify the behavior of the cvars.
If you will read the cvar value later on in your script, use pointers.
, or get_pcvar_string
Do not use get_cvar_flags
, or get_cvar_string
register_cvar ( const name, const string, flags = 0, Float:fvalue = 0.0 )
I haven't found any examples where fvalue
is used, so please post if you know what it's for.
How do they behave?
new cvar_MyCvar // this is a pointer
new cvar_OtherCvar // this is also a pointer
// this cvar has no flags assigned
cvar_MyCvar = register_cvar("amx_mycvar", "0")
// this cvar has two flags assigned
cvar_OtherCvar = register_cvar("amx_other", "1", FCVAR_SPONLY|FCVAR_UNLOGGED)
Order of Events:
1.Server is off hlds.exe is not running
3.plugin_init() cvars are created
5.amxx.cfg cvars assigned values from amxx.cfg
6.server.cfg cvars assigned values from server.cfg
7.Server restarts mapchange or 'restart' command
8.plugin_init() cvars are NOT re-created or overwritten; if they exist, only pointers are gathered
10.amxx.cfg cvars assigned values from amxx.cfg (server.cfg is not called next)
The functions plugin_init()
are called on every map-change, restart, and "fresh" start. ("Fresh" start means the server turned on after being off.) The amxx.cfg file is read on every map-change, restart and "fresh" start. The server.cfg file is only read on "fresh" starts.
When you do register_cvar("cvar_name", "cvar_value")
, you are attempting to create a new cvar with a new default value. If this cvar already exists, this function will not overwrite the original value. Instead, it will return a pointer to the existing cvar that you can later access with get_pcvar_flags
, or get_pcvar_string
To get a pointer for a cvar, use get_cvar_pointer("cvarname")
This is taken directly from amxconst.inc:
set to cause it to be saved to vars.rc
changes the client's info string
notifies players when changed
defined by external DLL
defined by the client dll
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
This cvar cannot be changed by clients connected to a multiplayer server.
This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ).
If this is a FCVAR_SERVER, don't log changes to the log file / console if we are creating a log
Unfortunately, I can't find any examples for the following flags: FCVAR_ARCHIVE
So, I'd like to ask anyone who knows about these flags to post.
Here's a simple script that counts how many times a server stops running. If you can understand how this script works, then you probably have a good grip on cvars.
This script should only be used as a demonstration to understand cvars. If you really want to count crashes accurately, I would recommend a different method. Let me know if you're interested.
#define PLUGIN "Crash Counter"
#define VERSION "1.0"
#define AUTHOR "stupok"
#define DIR_CRASHLOG "addons\amxmodx\logs\CrashLog"
#define FILE_CRASHMAP "addons\amxmodx\logs\CrashLog\crash_map.txt"
#define FILE_LOG "CrashLog.log" // will be found in addons\amxmodx\logs
register_plugin(PLUGIN, VERSION, AUTHOR)
cvar_Crashed = register_cvar("server_crashed", "1", FCVAR_SPONLY) // do not let players edit this cvar
if(!dir_exists(DIR_CRASHLOG)) mkdir(DIR_CRASHLOG) // make addons\amxmodx\logs\CrashLog
if(!file_exists(FILE_CRASHMAP)) write_file(FILE_CRASHMAP, "-- Do not edit --", -1) // make crash_map.txt
read_file(FILE_CRASHMAP, 2, szMapName, charsmax(szMapName), iLineLen) // read previous map from file
if(get_pcvar_num(cvar_Crashed)) // check if server just started, 1 = start, 0 = restart/mapchange
if(szMapName) // do we have a map from crash_map.txt?
log_to_file(FILE_LOG, "The server exited or crashed on '%s' before this recorded time.", szMapName)
set_pcvar_num(cvar_Crashed, 0) // this will be set to 1 again when the server has a "fresh" start (not restart)
get_mapname(szMapName, charsmax(szMapName)) // get the name of the current map
write_file(FILE_CRASHMAP, szMapName, 2) // write it to crash_map.txt