User:Daeval/Weapon Basics

From Valve Developer Community

Jump to: navigation, search

This article will explore the way weapons are constructed in the Template (scratch) SDK code.

This will live in my userspace for now. I'll move it to the open wiki when I feel it has something meaningful to add.


To do: Formatting for general tidiness.


Contents


Weapon Components

We'll dissect the Pistol to get a feel for how weapons are built in the SDK. There are three primary components; source code, definitions, a data file, and a localized string.


Source Code

The source code for the Pistol can be found in:

\src\game\shared\sdk\weapon_pistol.cpp
Note:When making your own weapons, make sure the source .cpp file is included in both the Client and Server projects in Visual Studio. Valve's use of the shared directory here is an important hint.


To do: Break down the pistol code here.


Weapon Definition

In addition to the source code, each weapon has a few variable definitions stored elsewhere for global use.

The weapon definitions for all weapons are split across these two files:

\src\game\shared\sdk\sdk_shareddefs.h
\src\game\shared\sdk\sdk_shareddefs.cpp


sdk_shareddefs.h

In sdk_shareddefs.h, we find this enum:

// \src\game\shared\sdk\sdk_shareddefs.h
 
typedef enum
{
	WEAPON_NONE = 0,
 
	SDK_WEAPON_NONE = WEAPON_NONE,
	SDK_WEAPON_MP5,
	SDK_WEAPON_SHOTGUN,
	SDK_WEAPON_GRENADE,
	SDK_WEAPON_PISTOL,	SDK_WEAPON_CROWBAR,
 
	WEAPON_MAX,		// number of weapons weapon index
} SDKWeaponID;


Note:When making your own weapons, you will need to add another line to this enum with an appropriate constant name, such as MYMOD_WEAPON_MYGUN. This constant will also be used in the source code, as mentioned above, so make sure you are consistent.
Warning:When modifying this enum, make sure the weapons are listed in the same order here as they are in sdk_shareddefs.cpp's array below or you will have indexing problems!


sdk_shareddefs.cpp

The second half of the weapon definition is in sdk_shareddefs.cpp, where the weapons are given names as strings, or aliases:

// \src\game\shared\sdk\sdk_shareddefs.cpp
 
static const char * s_WeaponAliasInfo[] = 
{
	"none",		// WEAPON_NONE
	"mp5",		// SDK_WEAPON_MP5
	"shotgun",	// SDK_WEAPON_SHOTGUN
	"grenade",	// SDK_WEAPON_GRENADE
	"pistol",	// SDK_WEAPON_PISTOL	"crowbar",	// SDK_WEAPON_CROWBAR
	NULL,		// WEAPON_NONE
};


Note:When making your own weapons, you will also need to add another line to this array using an appropriate string, such as "mygun", or perhaps "mymod_mygun" to keep things organized.
Note:These are not something the player will normally see in-game, unless he's cheating. Those names will come later, in the localization string.
Warning:When modifying this array, make sure the weapons are listed in the same order here as they are in sdk_shareddefs.h's enum above or you will have indexing problems!


How Weapon Definitions Are Used

The constant definitions in sdk_shareddefs.h can be used to refer to the weapons elsewhere within the code. For example, this code in sdk_gamerules.cpp loops through the enum and checks for specific weapons by "name." Notice that the loop is also using WEAPON_NONE and WEAPON_MAX as bounds for the index.

// \src\game\shared\sdk\sdk_gamerules.cpp
 
for (int i=WEAPON_NONE+1;i<WEAPON_MAX;i++)
{
	//Tony; ignore grenades, shotgun and the crowbar, 
        //grenades and shotgun are handled seperately because of their damage type not being DMG_BULLET.
	if (i == SDK_WEAPON_GRENADE || i == SDK_WEAPON_CROWBAR || i == SDK_WEAPON_SHOTGUN)
		continue;
 
	def.AddAmmoType( WeaponIDToAlias(i), DMG_BULLET, TRACER_LINE_AND_WHIZ, 0, 0, 200/*max carry*/, 1, 0 );
}


The aliases in sdk_shareddefs.cpp's array are used primarily in scripts, such as this one from the player classes included in the template SDK. They are a friendly way to refer to the weapons, which can then be resolved to the enum constant by the code.

// \Steam\steamapps\SourceMods\mod_dir\scripts\playerclass_blue_class1.txt
 
	// Weapon Info
	"primaryweapon"		"pistol"
	"secondaryweapon"	"none"
	"meleeweapon"		"crowbar"


Datafile

Each weapon also has a Datafile, under the SourceMods directory, that describes some of the basic weapon variables. The developer console will warn you if this file is missing. For the pistol, this is:

\Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
Note:These properties are not in the order they appear in the SDK's .txt. I moved them around a bit to better explain them. The order should not matter in your own Datafiles.

To do:  Datafile stuff.

Functional Properties

// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
 
// Weapon characteristics:
"Damage"			"54"		// damage per bullet
"Bullets"			"1"		// bullets per shot
"CycleTime"			"0.225"		// time between shots
// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
 
"bucket"			"1"
"bucket_position"		"0"
 
"clip_size"			"8"
"NumClips"			"3"
 
"primary_ammo"			"pistol"
"secondary_ammo"		"None"
 
"weight"			"25"
"item_flags"			"0"
// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
 
"printname"			"#SDK_Weapon_Pistol"


Animation Properties

// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
 
"BuiltRightHanded"		"0"
"viewmodel"			"models/weapons/v_pist_deagle.mdl"
"playermodel"			"models/weapons/w_pist_deagle.mdl"
"PlayerAnimationExtension"	"mp5"
// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
ModelBounds
{
	Viewmodel
	{
		Mins	"-10 -4 -13"
		Maxs	"21 9 -1"
	}
	World
	{
		Mins	"-10 -7 -6"
		Maxs	"22 8 9"
	}
}


Sound Properties

// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
 
// Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds)
SoundData
{
	//"reload"			"Default.Reload"
	//"empty"			"Default.ClipEmpty_Rifle"
	"single_shot"			"Weapon_DEagle.Single"
}

Texture Properties

// \Steam\steamapps\SourceMods\mod_dir\scripts\weapon_pistol.txt
// Weapon Sprite data is loaded by the Client DLL.
TextureData
{
	"weapon"
	{
			"font"		"WeaponIcons"
			"character"	"a"
	}
	"weapon_s"
	{	
			"font"		"WeaponIconsSelected"
			"character"	"a"
	}
	"ammo"
	{
			"font"		"WeaponIcons"
			"character"	"r"
	}
	"crosshair"
	{
			"file"		"sprites/crosshairs"
			"x"		"0"
			"y"		"48"
			"width"		"24"
			"height"	"24"
	}
	"autoaim"
	{
			"file"		"sprites/crosshairs"
			"x"			"0"
			"y"			"48"
			"width"		"24"
			"height"	"24"
	}
}




Localized String

To do:  Localization stuff.

Personal tools