VERC: How To Setup A Cvar Last edited 20 years ago2004-02-15 20:27:00 UTC

You are viewing an older revision of this wiki page. The current revision may be more detailed and up-to-date. Click here to see the current revision of this page.

This article was recovered from an archive and needs to be reviewed

  1. The formatting may be incorrect as it was automatically converted to WikiCode from HTML, it needs to be revised and reformatted
  2. Some information may be out of date as it was written before Half-Life was available on Steam
  3. After the article is re-formatted and updated for Steam HL, remove this notice
  4. Please do not remove the archive notice from the bottom of the article.
  5. Some archive articles are no longer useful, or they duplicate information from other tutorials and entity guides. In this case, delete the page after merging any relevant information into other pages. Contact an admin to delete a page.
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 creators 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.

Part A
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:
"true_cvar_name"
This is the exact name of the Cvar, this is used in the code to identify what Cvar you are calling/using.
"command_for_cvar"
This is what gets called in the console to change the Cvar value.
"current_value"
The default value of the Cvar

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):

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
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 with in 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:

1) Int/Float

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

2) String

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

3) 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
Part B
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.

1) Int/Float

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

2) String

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

3) 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. So once you have game.cpp open, scroll up to the top. You should notice something like this:

cvar_t displaysoundlist = { " displaysoundlist ", "0" };
if (testcvar.value)
//If cvar is greater then 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.