VERC: How To Setup A Cvar Last edited 2 weeks ago2018-06-27 09:12:29 UTC by Penguinboy Penguinboy

Cvars are registered variables, which gives the ability to mod creators to allow the public to change current settings to almost anything. However, the mod creator needs to create each cvar and where to place them. For this article, I will explain how to create/use Cvars on the server and client DLL, along with the flags and examples on how to use the Cvars. So let's begin.

Creating Cvars - Server Side Workspace

Cvars that effect everyone, since its on the server DLL.

Step A

Before we create a Cvar, we need to open game.cpp. This is the file which has a list of all the server's Cvars. I'll give you a brief explanation as to how the Cvar is set up, before you go on creating a new Cvar. From looking at the Cvar you would be a little confused, so here is a little break down:
cvar_t     true_cvar_name = { " command_for_cvar ", "current_value" };

Basic Info

So now you know what this function does, lets add in our new CVAR:
cvar_t     testcvar = { " test_cvar ", "0" };
// testcvar - Name Of Cvar || test_cvar - Command For Cvar || 0 - Value of 0
before or after
cvar_t     displaysoundlist = { " displaysoundlist ", "0" };

Step B

Before we go on to register the Cvars, we are going to look at type of flags you can use for each Cvar. Notice you need to add a new section when you add a flag.

No Flag

cvar_t     testcvar = { " test_cvar ", "0" };

A Flag

cvar_t     testcvar = { " test_cvar ", "0", Flag };
So before you add a flag, check on the list to make sure it's the flag you want to use(taken from common/cvardef.h): Now you can use more then one flag, but be careful on which flags you use. Here's an example of how to use one and more than one flag.

1 Flag

cvar_t     displaysoundlist = { " displaysoundlist ", "0", FCVAR_SERVER };
// Only the host can use this command

2 Flags

cvar_t     displaysoundlist = { " displaysoundlist ", "0", FCVAR_SERVER | FCVAR_UNLOGGED };
// Only the host can use this command and it's not logged.
Note: The Cvar can be changed by all players, by default. Now since this is the server DLL, the Cvar will effect everyone. So if wish to use the Cvar for the server only, make sure to use the Server-only flag (FCVAR_SERVER) on.

Step C

Now that we have created our Cvar, we have to register the cvar for the engine. So scroll on down till you find the following:
void GameDLLInit( void )
Now within the { } we are going to register our Cvar with:
CVAR_REGISTER (&testcvar);
Notice we are using the Cvar name and not the Cvar command.

Step D

Now that we registered our Cvar, we need to use it. But first, we need to externalize it.

Add this into game.h:
extern cvar_t     testcvar;
Then in the file you're going to include your Cvar, make sure to add this at the top:
#include "game.h"

Step E

Now we have everything set. Now we just need to know how to use the Cvars. Well here are a couple ways:

Integer

int number = CVAR_GET_FLOAT( "testcvar" );
ALERT(at_console," Int Number  : %d   n ", number );

Float

float limit = CVAR_GET_FLOAT( "testcvar" );
ALERT(at_console," Float Number  : %d   n ", limit );

String

ALERT(at_console," String Name  : %d   n ", CVAR_GET_STRING( "testcvar" ) );

If / Else

if (testcvar.value == X)
// If value is X , then do this

if (testcvar.value > X)
// If value of x is greater then the value of the cvar, then do something.

if (testcvar.value < X)
// If value of x is less then the value of the cvar, then do something.

if (testcvar.value)
// If cvar is greater then 0, then do this
Note: You can use "else", incase you want something else to happen, if the Cvar value is not what it needs to be. Example:
if (testcvar.value) {
    // Do Something
} else {
    // Something else
}

Creating Cvars - Client Side Workspace

Affects only 1 person, and that is the person who changes the Cvar.

Step A

Let's create a new Cvar for the client workspace.
Open hud.cpp and find the following:
void CHud :: Init( void )
Before we create the Cvar, there is something you need to know. Since the client Cvars are different, you don't have the ability to have the Cvar name and a command name, you just get 1 name. In other words, whatever your Cvar name is, that's the console command to change the value. Let's add the following (in CHud::Init):
CVAR_CREATE ("testcvar", "0",     0);
Notice the difference, between the client and server Cvar? In case you don't:
CVAR_CREATE ("name_of_cvar_and_command_name", "value",     flags);
Yes, the flags are the same as the server DLL, so in case you don't remember how to do the flags, scroll up to Part A, Step B.

Step B

Calling the Cvars are must the same as the server side workspace.

You use CVAR_GET_FLOAT to get a float value or a int value.
And you use CVAR_GET_STRING to get the string value.

Integer

int number = CVAR_GET_FLOAT( "testcvar" );
gEngfuncs.Con_Printf (" Int Number : %d \n ", number );

Float

float limit = CVAR_GET_FLOAT( "testcvar" );
gEngfuncs.Con_Printf (" Float Number : %d \n ", limit );

String

gEngfuncs.Con_Printf (" String Name : %d \n ", CVAR_GET_STRING( "testcvar" ) );

If / Else

if (testcvar.value == X)
// If value is X , then do this

if (testcvar.value > X)
// If value of x is greater then the value of the cvar, then do something.

if (testcvar.value < X)
// If value of x is less then the value of the cvar, then do something of all the servers Cvars.

if (testcvar.value)
// If cvar is not 0, then do this
Note: You can do "else", incase you want something else to happen, if the Cvar value is not what it needs to be. Example:
if (testcvar.value) {
    // Do Something
} else {
    // Something else
}
This article was originally published on the Valve Editing Resource Collective (VERC).
TWHL only archives articles from defunct websites. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.

Comments

You must log in to post a comment. You can login or register a new account.