User:Daeval/Weapon Basics
From Valve Developer Community
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
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;
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 };
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
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.
