.qc (dot qc) - the group for quake c coders of all denominations. If you make quake one mods and write code in quake c, join our group! I'll try to answer any quake c questions - please post your inquiry in the forums...

Post tutorial Report article RSS Feed Quake c - create and use custom server variables

Quake one custom server variables - how to code them in quake-c (under darkplaces engines) and how to code them in any quake engine source.

Posted by numbersix on Aug 27th, 2011
Intermediate Server Side Coding.

Some adherents of Painkeep ( Moddb.com ), Chaos Archon ( Moddb.com ) and generations w-d-q (during its very short run) may have noticed I use a lot of custom server vars.

For instance - sv_botmatch. Open the console on one of the mods set that to 1 and get 3 frikbots and 3 pk*bots when you load the next map. Or set it to the number of bots you want.

There are 2 ways to get a console variable.

Darkplaces (and derivatives):

In your quake-c code, say in world.qc:

void () worldspawn =

// create the cvar, with default value 0

registercvar("sv_botmatch", "0"); // load bots without admin

// this sample code would go in other functions
// read cvar

float bcnt;

bcnt = cvar("sv_botmatch");

// bot start code

// zero value when done
cvar_set("sv_botmatch", "0");

// cvars are read as float, and set as a string
// but you can only store numerical data
// to store a float variable:
// ftos() is a float to string conversion - uses string temp space

cvar_set("sv_botmatch", ftos(bcnt));

// This MAY NOT work on all qcc compilers! on the original qcc you had to:

string tmp1;

tmp1 = ftos(bcnt);
cvar_set("sv_botmatch", tmp1);

// when using strings it is very important to know how the compiler you use handles string data


There is one caveat - if you set these vars in a .cfg file (such as autoexec.cfg) the first map you load will not be able to read them. Because they are not created until the first worldspawn() call is executed!

Quake engines:

To have permanent variables in an engine, get the source code (this is from glquake source) and open pr_edict.qc

Somewhere near the top with the rest of these defines put:

// create cvar, set initial value and state whether it gets saved in config.cfg

cvar_t sv_botmatch = {"sv_botmatch", "0", true };

// near the bottom with these defines - register the cvar

Cvar_RegisterVariable (&sv_botmatch);

Accessing the cvars in quake-c is exactly the same as the darkplaces sample code above.

Now you can create custom server variables for your mods and games. With a bit of imagination there are all sorts of possibilities.

Post comment Comments
LordHavoc Feb 24 2013, 8:55pm says:

It's better to put this in default.cfg:
set sv_botmatch 0 "enable bot match"

This creates a cvar if it does not exist, sets the value and the description text of the cvar so the user can see a comment when they tab complete it, additionally all cvars are saved to default values at the end of default.cfg so the user will see this value as default in the cvar description.

You also might want to use this in the qc:
float autocvar_sv_botmatch;

An autocvar variable will automatically mirror (for reading) the value of the corresponding cvar at all times (immediately updates if you use cvar_set), so you can monitor the value with no additional code, note that it is read-only mirroring (cvar_set must still be used to change it).

+2 votes     reply to comment
numbersix Author
numbersix Sep 22 2013, 11:10am replied:


I recently discovered autocvar_* after switching to fteqcc...because my main mod broke frikqcc - ouch.

I like the default.cfg concept, I'm going to swap out to that.

+1 vote   reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Aug 18, 2011
Open to all members
Send Message
Join this group
Group Watch
Track this group
Report Abuse
Report article
Related Games
Quake Single & Multiplayer First Person Shooter
Related Engines
DarkPlaces engine
DarkPlaces engine GPL Released Mar 2000
Quake Engine
Quake Engine GPL Released Jun 1996
Related Groups
Dark Places engine team
Dark Places engine team Developer with 15 members
id Software
id Software Developer & Publisher with 2 members
qc Hardware & Tech group with 29 members