Post tutorial Report RSS How to register a new CVAR for Half-Life

How to register a new CVAR for Half-Life (Server & Client CARs + Add to Settings) So I presume you know what CVARs are. No? Those are the commands you enter in the console in the game. And they can be used to do many things. Here I'm not going to show you how to make awesome things, I'm just going to show you how to register CVARs for you needs. As you know some commands are available for the clients and some are available for the server only. This is due to the way they are registered.

Posted by on - Intermediate Server Side Coding

How to register a new CVAR for Half-Life (Server & Client CARs + Add to Settings)

So I presume you know what CVARs are. No? Those are the commands you enter in the console in the game. And they can be used to do many things.

Here I'm not going to show you how to make awesome things, I'm just going to show you how to register CVARs for you needs.

As you know some commands are available for the clients and some are available for the server only. This is due to the way they are registered.

For example you would like to allow your client to select their hud color, but you don't want him to be able to select the gamerules for the server, or to outright change the map for the server right?

cvar create server

So let's see how the magic of registering CVARs is done:

1. Registering a Server (MP_) CVAR

For this example I will be registering a CVAR called "extraviolence" or "mp_extraviolence".

I plan to use it to make the game even more bloody than it is... Still it's going to be a good example on how to do a cvar anyway.

Open "game.h" and search for "// multiplayer server rules". Somewhere there under that line write your cvar.

extern cvar_t extraviolence;

Open "game.cpp" and search for "// multiplayer server rules", if you get more than one it should be the first one, look at the line for my CVAR and just figure it out:

cvar_t extraviolence= {"mp_extraviolence","3", FCVAR_SERVER };

A little explanation here, our cvar will be called "extraviolence", however from the console I would like to call it using a command "mp_extraviolence".

The default value is set to [3], and this is a Server CVAR, so only if you are hosting the server you will be able to do the command.

Now, again in "game.cpp" look for "// Register cvars here:" or "CVAR_REGISTER" and place this line here:

CVAR_REGISTER (&extraviolence);

That's it. We just registered a new server CVAR.

2. Let us create a client (CL_) CVAR now

How about no. I will just explain you the parameters, and this job will be for you to do.

The idea behind a client CVAR is that it's coded in the client dll, and every client can use it freely, with no control from the server itself.

Example of that would be changing the color of the HUD (assuming you don't have it hard coded.)

So how to do it... well the steps are the same for the registering, however the difference is that you're going to put it inside .cpp file located in "cl_dll" workspace.

The second difference would be the argument you're going to use. If you scroll above you will see that the initial argument we used for the Server CVAR was "FCVAR_SERVER".

This made the CVAR server side... and we need a client side one.

Well now you will have to use "FCVAR_CLIENTDLL".

\common\cvardef.hYou can find all the CVAR definitions in "\common\cvardef.h" file.

Assuming you have not defined any new (if you have, please let me know how...) Here's how the definitions look like:

#define FCVAR_ARCHIVE (1<<0) // set to cause it to be saved to vars.rc

#define FCVAR_USERINFO (1<<1) // changes the client's info string

#define FCVAR_SERVER (1<<2) // notifies players when changed

#define FCVAR_EXTDLL (1<<3) // defined by external DLL

#define FCVAR_CLIENTDLL (1<<4) // defined by the client dll

#define FCVAR_PROTECTED (1<<5) // 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

#define FCVAR_SPONLY (1<<6) // This cvar cannot be changed by clients connected to a multiplayer server.

#define FCVAR_PRINTABLEONLY (1<<7) // This cvar's string cannot contain unprintable characters ( e.g., used for player name etc ).

#define FCVAR_UNLOGGED (1<<8) // If this is a FCVAR_SERVER, don't log changes to the log file / console if we are creating a log

3. Adding the CVAR to Game Create

As added bonus here, I will show you how to add the cvar in question in the Game Create Server screen.So go to your mod folder (I assume you have ran the mod at least once, if you haven't go start it, the exit).

I'm asking for the mod to be run at least once since there is a file that will be created when you run the mod, and if you haven't done that that file might be missing...

The file in question is located under the mod directory and it's called "settings.scr". For some reason Windows thinks this is a screensaver file, but I beg to differ.

Go ahead and open it with notpad++ and take a good lock at it. Inside you will see your additional options (already predefined by Valve or by another coder like you )

So doing the analogy with the rest, let's add our server CVAR we created. Take a note that in my mod I have 4 different violence settings.

0 - No gore (just like the German version of the game, old time ago on WON)

1- Standard Half-Life 1 level of violence

2- Increased Gore level

3- Bloodbath

So here's how the code for my settings look like in the editor:

"mp_extraviolence"

{
"Violence Settings"
{
LIST
"No Gore" "0"
"Standard" "1"
"Increased" "2"
"Bloodbath" "3"
}
{ "3.000000" }
}

cvar create server


So, as you can see the syntax, on top we have the CVAR name as it should appear in the console, afterwards I have the display name how it should appear in the Game Menu.

cvar create server


Then I have a LIST of the states, that includes naming the value corresponding to the CVAR. Afterwards I have a value, that's my default value.

We'll I think that's everything for this tutorial.

Now go and use your awesome codding supper powers (I hope you have them) and create a context for your new server and client CVARs.

Have fun. Kids.

If you don't like something - Mod it!

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: