Adding a Cvar Feature Type: Coding Tutorials     Added: 06/01  
  Author: Corven 

This will show you how to add whats known as a "cvar" a variable that can be used in many ways within the code.


Code to be Added or Changed

A cvar is a variable that we can reference in the code to do several things, you'll see them throughout any tutorial. Basically saying if this cvar is set do this, else do something different. But it must be declared before you can use them.

Here's some of the common types of cvar:
Integer An integer number value, a whole number but can also be a negative.
Floating Point or Value A number value that, unlike an integer, can include decimals.
String A string is simply a bunch of alpha-numeric characters.

Cvars can be added to any part of the code, meaning either the GAME, CGAME or UI parts. You may want to setup a cvar to control something on the serverside, or perhaps an effect on the clientside, or maybe even something on the menus.

For an example, we'll add a cvar to the GAME (serverside) part and we'll call it g_ourcvar


Open up g_local.h and look about two 3rds of the way down, you'll see a long list of cvars like these:

extern vmCvar_t g_dmflags;
extern vmCvar_t g_fraglimit;
extern vmCvar_t g_timelimit;
extern vmCvar_t g_capturelimit;
extern vmCvar_t g_friendlyFire;
extern vmCvar_t g_password;

Now we will just add our cvar onto the end:
extern vmCvar_t g_ourcvar;


Now we will setup our cvar in g_main.c so the serverside can use it, also we will set its default value and it's type. Not far from the top of g_main.c you'll see another list of cvars like these:

vmCvar_t g_dmflags;
vmCvar_t g_fraglimit;
vmCvar_t g_timelimit;
vmCvar_t g_capturelimit;
vmCvar_t g_friendlyFire;
vmCvar_t g_password;
vmCvar_t g_needpass;

Again we'll just add our new cvar onto the end of those:
vmCvar_t g_ourcvar;

Then scroll down until you get to the cvar table which will look like this:
static cvarTable_t gameCvarTable[] = {

Once again you guessed it, we'll add our cvar to the end of the table listing:
{ &g_ourcvar, "g_cvar", "0", CVAR_SERVERINFO | CVAR_LATCH, 0, qfalse },

What this last part does is setup the cvar, how it will be known, its initial values, its state, how it will be stored and wether it will be announced if changed.

Now your all ready to use your cvar somewhere in the code, for an example see the Starting Health and Armor tutorial, it will show you how to use a cvar to do different things depending on what value its set to.

Comments »

  Page 1

  Comments ( 0 )

  Features Index


Copyright 2001 MGON International AB - Privacy statement