Adding Variables


This tutorial is for adding server side variables for now. I may add client side variables (and how to move them between the games) later.


Ok the first thing you should decide is where you want the variable. Do you want it to be available for all objects in the world, or just the players, or do you want it to be global (not attached to any object in particular, but available for every file and function)

Next, you should decide the type of variable you want to add. Look at this list (It's possible to create your own, but thats a different tutorial)

Finally you need a name for the variable. I'd suggest making this meaningful, rather than something like x. One good idea is to preface all your variables with a small code (like your initials) i.e. instead of calling something newpostion, I would call it sma_newposition. it make your additions easy to find, and prevents conflicting variable names.


Decided all that? Good.

I'll get to the positioning of the variable in a moment, first off I'll show you how to declare it

Fairly simple, you put the type of variable first, and then the name, followed by a semi-colon

Examples:

int sma_mytimer;
vec3_t sma_mypostion;
char[20] sma_myname;

Special case: Pointers
A pointer is a special kind of variable that points to another variable.Its used in q3 mostly for dealing with objects, as all the objects are actually held in a large array. G_spawn just links the pointer to an entry in the array. To define a variable as a pointer, place a star (*) before the variable name

Ok now we come to positioning the variable. If you want it available for every object, then you want to place it in the gentity_t structure.

Search for qboolean botDelayBegin in g_local.h (game directory).

Place the variable declaration between that and the };

i.e.

	gitem_t		*item;			// for bonus items

	qboolean	botDelayBegin;

	int sma_mytimer;   // new variable is here

};

Placing it just for the players is similar. search for int timeResidual; in g_local.h (game directory)

This puts you at the end of the gclient_t structure definiton. put the variable between that and the };

i.e.

	gentity_t	*hook;				// grapple hook if out

	// timeResidual is used to handle events that happen every second
	// like health / armor countdowns and regeneration
	int			timeResidual;


	int sma_mytimer;   // new variable is here

};

Got it? Good.


Ok, declaring it globaly is slightly different. You actualy have to declare it twice. one in a specific file, and once in g_local.h as an extern. (Extern is a key word that tells the compiler that the actual definition is in another file)

At the end of g_local.h add a declaration like the one below

extern int sma_mytimer;

The pick a file (g_main.c is a good one) and add the defintion just after all the #include lines at the top.


Using the variables

Bit different depending on the type of variable you've added.

All object variables
Objects are of type gentity_t. Theres normally a parameter in the function defintion of that type. I'm going to use ent as that parameter.
to use your variable you use ent->sma_mytimer (or whatever you called your variable)
Player variables
Players are of type gentity_t with another field called client. Theres normally a parameter in the function defintion of that type. I'm going to use ent as that parameter.
to use your variable you use ent->client->sma_mytimer (or whatever you called your variable)
One point, if you try to access a player only variable from a non player object, you'll crash the game
Global variables
Easy, just use the variable name whereever you want to.

Back to the tutorials
Mail me