Class System

From Valve Developer Community
Revision as of 18:04, 28 January 2024 by SirYodaJedi (talk | contribs)
Jump to navigation Jump to search
English (en)Русский (ru)Translate (Translate)
Info content.png
This page needs to be translated.
This page either contains information that is only partially or incorrectly translated, or there isn't a translation yet.
If this page cannot be translated for some reason, or is left untranslated for an extended period of time after this notice is posted, the page should be requested to be deleted.
Also, please make sure the article complies with the alternate languages guide.
The following content or section specifically needs to be translated:
Todo: restore from 2016 archived backup, then crosscheck for up-to-date content with the russian version.
Wikipedia - Letter.png
This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these template messages)
Underlinked - Logo.png
This article needs more Wikipedia icon links to other articles to help Wikipedia icon integrate it into the encyclopedia. Please help improve this article by adding links Wikipedia icon that are relevant to the context within the existing text.
January 2024

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.

Todo: 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."

Icon-Bug.pngBug: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.  [todo tested in ?]

Scripts

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"
Todo: Возможность добавить специальную способность для конкретного класса (например, дать двойной прыжок)

Система классов читает .txt файлы в папке "scripts/" по названию "классигрока_<красный/синий>_класс<номер>.txt".

Пример:

PlayerClassDatafile
{
	// Члены FilePLayerClassInfo_t
	"printname"		"#class_blue_class1"
	"playermodel"		"models/player/blue_player.mdl"
	"selectcmd"		"cls_blue_class1"

	// Члены CSDKPlayerClassInfo

	// Принадлежность класса к команде (для проверки)
	"team"			"BLUE"

	// Оружие класса
	"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"

	// Передвижение
	"RunSpeed"		"240"
	"SprintSpeed"		"360"
	"ProneSpeed"		"70"
}