Raised This Month: $7 Target: $400
 1% 

[INFO] Cvars


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
stupok
Veteran Member
Join Date: Feb 2006
Old 02-08-2009 , 14:41   [INFO] Cvars
Reply With Quote #1

CVARs

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.

Usage

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.

Use get_pcvar_flags, get_pcvar_float, get_pcvar_num, or get_pcvar_string.
Do not use get_cvar_flags, get_cvar_float, get_cvar_num, or get_cvar_string.

Code:
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.

Example Usage:

Code:
new cvar_MyCvar // this is a pointer new cvar_OtherCvar // this is also a pointer public plugin_init() {     // 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) } public MyFunction() {     if(get_pcvar_num(cvar_MyCvar))         return 1     return 0 }

How do they behave?

Code:
Order of Events:
1.Server is off		hlds.exe is not running
2.Server starts
3.plugin_init()		cvars are created
4.plugin_cfg()
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
9.plugin_cfg()
10.amxx.cfg		cvars assigned values from amxx.cfg (server.cfg is not called next)
The functions plugin_init() and plugin_cfg() 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, get_pcvar_float, get_pcvar_num, or get_pcvar_string.

To get a pointer for a cvar, use get_cvar_pointer("cvarname").

CVAR flags

This is taken directly from amxconst.inc:
FCVAR_ARCHIVE set to cause it to be saved to vars.rc
FCVAR_USERINFO changes the client's info string
FCVAR_SERVER notifies players when changed
FCVAR_EXTDLL defined by external DLL
FCVAR_CLIENTDLL defined by the client dll
FCVAR_PROTECTED 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
FCVAR_SPONLY This cvar cannot be changed by clients connected to a multiplayer server.
FCVAR_PRINTABLEONLY This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ).
FCVAR_UNLOGGED 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, FCVAR_USERINFO, FCVAR_EXTDLL, FCVAR_CLIENTDLL.

So, I'd like to ask anyone who knows about these flags to post.

Example

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.

Code:
#include <amxmodx> #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 new cvar_Crashed public plugin_init() {     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         new szMapName[32]     new iLineLen     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[0]) // 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 }
__________________
stupok is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 02-09-2009 , 09:04   Re: [INFO] Cvars
Reply With Quote #2

Nice guide!
i'm interested in this
Quote:
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.
i would wanna see code ;)
__________________
xPaw is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 02-09-2009 , 11:20   Re: [INFO] Cvars
Reply With Quote #3

To avoid creating many variables for cvars, you can do like this:
PHP Code:
enum _:cvars
{
    
CVAR_1,
    
CVAR_2,
    
CVAR_3,
    
CVAR_4
    
// ...
}

new 
g_Cvarscvars ];

// ...

public plugin_init( )
{
    
g_CvarsCVAR_1 ] = register_cvar"cvar_1""value" );
    
g_CvarsCVAR_2 ] = register_cvar"cvar_2""value" );
    
g_CvarsCVAR_3 ] = register_cvar"cvar_3""value" );
    
g_CvarsCVAR_4 ] = register_cvar"cvar_4""value" );

    
// ...
}

// ...
new value1 get_pcvar_numg_CvarsCVAR_1 ] );
// .. 
ND!
__________________

anakin_cstrike is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 02-09-2009 , 11:23   Re: [INFO] Cvars
Reply With Quote #4

That doesn't really help unless you cache the strings as a series of arrays, too. Then you can loop through each cvar.
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 02-11-2009 , 09:37   Re: [INFO] Cvars
Reply With Quote #5

PHP Code:
 
#define cvars 4
new cvar_names[cvars][] =
{
   
"name1",
   
"name2",
   
"name3",
   
"name4"
   
//...
};
new 
cvar_values[cvars] =
{
   
0,
   
5,
   
7,
   
8
   
//...
};
 
new 
g_Cvarscvars ];
 
// ...
 
public plugin_init( )
{
   for(new 
0cvarsi++)
         
g_Cvars[i] = register_cvar(cvar_names[i], num_to_str(cvar_values));// ...
}
 
// ...
new Cvars[cvars];
for(new 
0cvarsi++)
   
Cvars[i]= get_pcvar_numg_Cvars[i] );
// ... 
@stupok, nice...

Last edited by SnoW; 02-11-2009 at 09:40.
SnoW is offline
Send a message via MSN to SnoW
SpliN
Junior Member
Join Date: Jun 2020
Location: Egypt
Old 06-11-2020 , 10:22   Re: [INFO] Cvars
Reply With Quote #6

good work bro
SpliN is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:36.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode