Class System

From Valve Developer Community
Jump to: navigation, search

In the March 17 2008 beta of the SDK, a flexible class system was added. This page will teach programmers how to work with this system.

To do: Explain how to implement into Half Life 2: Deathmatch

Starting off

Open up the SDK, and run the "Create a Mod" wizard. Select "Start a Multiplayer mod from a template." Don't worry about the "advanced users" warning, since Valve has made this extremely easy. Make sure you check "Enable classes" and most likely "Enable teams."

Bug: After starting a mod from a template, compiling the dlls and playing the mod, the mod hangs when you try to quit. The reason for this is unknown.

A fix for this bug is in the Source 2007 Template Fixes page.


Now, most of the configuration for the class itself is done with simple text files. The files that control this are located in $moddir\scripts, under the names playerclass_blue_class1.txt and such.

While most of it is relatively self-explanatory, certain things might be obscure, or not listed.

Variables (on the C++ side)

int m_iTeam; 			// 0 is unassigned, 1 is spectator, 2 is team 1 (BLUE), 3 is team 2 (RED), etc.
int m_iPrimaryWeapon;		// Index of the weapon, as defined in sdk_shareddefs.h (default is NONE, MP5, SHOTGUN, GRENADE, PISTOL, and CROWBAR
int m_iSecondaryWeapon;	// Same as primary, but for secondary, and allowed to be WEAPON_NONE
int m_iMeleeWeapon;		// Same as secondary, but for melee
int m_iNumGrensType1;		// Number of Type 1 Grenades
int m_iGrenType1;		// Same as secondary, but for Type 1 Grenades
int m_iNumGrensType2;		// Number of Type 2 Grenades
int m_iGrenType2;		// Same as secondary, but for Type 2 Grenades
char m_szLimitCvar[64];	// Name of the cvar that controls the class limit for this class
float m_flRunSpeed;		// Run (normal) speed, in units/second. SDK Default is 220, HL2 uses 190
float m_flSprintSpeed;		// Sprint speed, in units/second. Is usually 150% run speed. SDK default is 330, HL2 uses 327.5
float m_flProneSpeed;		// Prone speed, in units/second. SDK defaults to 50
int m_iArmor;			// Amount of extra armor. It will absorb 80% of the damage (to change that, go to player.cpp)

char m_szClassImage[SDK_PLAYERCLASS_IMAGE_LENGTH];	// According to comments, this is "HUD player status health images (when the player is hurt)"
char m_szClassImageBG[SDK_PLAYERCLASS_IMAGE_LENGTH];	// However, I can't seem to trace the code for it.

Variables (on the .txt side)

// Mandatory / Strongly Recommended keyvalues
"printname"		"#class_red_class1"			// the name of the class, usually as a localization string. Not used in sdk_playerclass_info_parse.cpp, but it's used in other places in .res files
"playermodel"		"models/player/red_player.mdl"		// the model of the class
"selectcmd"		"cls_red_class1"			// from what the author can tell, the name of the console command used to switch to this class
"team"			"RED"					// The team it goes with. Default code only accepts "BLUE" or "RED"
"primaryweapon"	"pistol"				// The name of the primary weapon. Cannot be "none"
"limitcvar"		"mp_limit_red_class1"			// The console variable that controls the maximum amount of this class allowed
"classimage"		"cls_red_class1_active"			// An image name, apparently for the HUD
"classimagebg"		"cls_red_class1_active_bg"		// Same

// Optional keyvalues
"secondaryweapon"	"none"					// Defaults to none
"meleeweapon"		"crowbar"				// Defaults to none
"grenadetype"		"grenade"				// Defaults to none
"numgrens"		"1"					// Defaults to 0
"grenadetype2"		"grenade"				// Defaults to none
"numgrens2"		"1"					// Defaults to 0
"armor"		"50"					// Defaults to 0
"RunSpeed"		"240"					// Defaults to SDK_DEFAULT_PLAYER_RUNSPEED, which defaults to 220
"SprintSpeed"		"360"					// Defaults to SDK_DEFAULT_PLAYER_SPRINTSPEED, which defaults to 330
"ProneSpeed"		"70"					// Defaults to SDK_DEFAULT_PLAYER_PRONESPEED, which defaults to 50

How it glues together

void unused			= "printname"
int m_iTeam; 			= "team"
int m_iPrimaryWeapon;		= "primaryweapon"
int m_iSecondaryWeapon;	= "secondaryweapon"
int m_iMeleeWeapon;		= "meleeweapon"
int m_iNumGrensType1;		= "numgrens"
int m_iGrenType1;		= "grenadetype"
int m_iNumGrensType2;		= "numgrens2"
int m_iGrenType2;		= "grenadetype2"
char m_szLimitCvar[64];	= "limitcvar"
float m_flRunSpeed;		= "RunSpeed"
float m_flSprintSpeed;		= "SprintSpeed"
float m_flProneSpeed;		= "ProneSpeed"
int m_iArmor;			= "armor"

char m_szClassImage[SDK_PLAYERCLASS_IMAGE_LENGTH];	= "classimage"
char m_szClassImageBG[SDK_PLAYERCLASS_IMAGE_LENGTH];	= "classimagebg"

To do: Cover adding a special ability to just one class (ex. give one class double-jumping)

The class system reads from txt files in the scripts/ directory named as "playerclass_<red/blue>_class<number>.txt".

Example Text File

	// FilePLayerClassInfo_t members
	"printname"			"#class_blue_class1"
	"playermodel"			"models/player/blue_player.mdl"
	"selectcmd"			"cls_blue_class1"

	// CSDKPlayerClassInfo members

	// What team has this class, for verification
	"team"				"BLUE"

	// Weapon Info
	"primaryweapon"		"pistol"
	"secondaryweapon"	"none"
	"meleeweapon"		"crowbar"

	"grenadetype"		"grenade"
	"numgrens"		"1"
	"limitcvar"		"mp_limit_blue_class1"

	"classimage"		"cls_blue_class1_active"
	"classimagebg"		"cls_blue_class1_active_bg"

	"armor"			"50"

	// Movement
	"RunSpeed"		"240"
	"SprintSpeed"		"360"
	"ProneSpeed"		"70"