Tutorials

Coding a new weapon



Since every single tutorial on coding has really long explanation things that nobody wants to read, and the fact they don't tell you how to get to the weapon coding in the first place, I'll explain it all.

First off, you'll need Microsoft Visual C++ 2005 Express, which can be found Here.

Then, once that's installed, you'll need to download the Platform SDK, and the DirectX SDK.

Platform SDK
DirectX SDK

Now that you have all of those installed (They should be in an easy place like Program Files and the such) you can start setting up VS2005.

Open up VS2005, and go to tools -> options -> projects and solutions -> VC++ Directories and in the top right of the window there should be a dropdown box, select Executable Files, and click on the New Line button and browse for the location C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Bin

news post image

Now click on the dropdown and select include, make two new lines and browse to these two locations
C:\Program Files\Microsoft DirectX SDK (February 2006)\Include
and
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Include

news post image

Now click on the dropdown and select library, make two new lines and browse to these two locations
C:\Program Files\Microsoft DirectX SDK (February 2006)\Lib\x86
and
C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib

news post image

Now that that's all done, you'll need to either download an edited corewin_express file from Here or edit it yourself. (That usually ends up badly)

Place the new corewin_express file into the "C:\Program Files\Microsoft Visual Studio 8\VC\VCProjectDefaults" folder.

Now that all this is done, open the hl2 2005 source solution file, which you would get from the SDK when you create a mod, and when the solution is open, go up to the top and change "Debug HL2" to "Release HL2"

news post image

Now go to "Build -> Build Solution"

You should get a clean no error build, don't worry about the warnings.

Now to get the new gun into your mod.

Now you should keep VS2005 open, and you should have this menu on the left.

news post image

Now, keeping VS2005 open, go to your folder that has all your code in it, for me it's "C:/Program Files/Steam/SteamApps/SourceMods/Code" While in your code folder, go to the "dlls/hl2_dll" folder, and copy the "weapon_pistol.cpp" then paste it, and name it "weapon_killer.cpp" now open your new .cpp file and replace all instances of CWeaponPistol, DTWeaponPistol and all the console commands with Killer. (So, CWeaponKiller, DTWeaponKiller, etc.)
If you cannot get that working, I did it for you, and here's a download.

Now, drag weapon_killer.cpp into VS2005, and drag that into the Solution Explorer, which is that little box on the left, and make it go into "Server_hl2 -> Source Files -> HL2_DLL" and drop it there.

Now keeping VS2005 open, go to your source code again, then go to "cl_dll/hl2_hud" and open "c_weapon__stubs_hl2.cpp"

Now while in that, you should see:

"STUB_WEAPON_CLASS( weapon_pistol, WeaponPistol, C_BaseHLCombatWeapon );"

Copy that, and paste it under itself, and change the newly pasted line of code to:

"STUB_WEAPON_CLASS( weapon_killer, WeaponKiller, C_BaseHLCombatWeapon );"

Save it, and save the whole Solution. Now, go to "Build -> Build Solution" It should compile with no errors, and now your coding it finished.

To cap it all off, go into your actual mod directory, and go into the "scripts" folder. Then copy weapon_pistol.txt and paste it, and change that to weapon_killer.txt.

Change the inside of weapon_killer.txt to say this:

// Killer

WeaponData
{
	// Weapon data is loaded by both the Game and Client DLLs.
	"printname"	"Killer"
	"viewmodel"		"models/weapons/v_pistol.mdl"
	"playermodel"		"models/weapons/w_pistol.mdl"
	"anim_prefix"		"pistol"
	"bucket"			"1"
	"bucket_position"	"2"

	"clip_size"			"20"
	"primary_ammo"		"Pistol"
	"secondary_ammo"	"None"

	"weight"			"2"
	"item_flags"		"0"

	// Sounds for the weapon. There is a max of 16 sounds per category (i.e. max 16 "single_shot" sounds)
	SoundData
	{

		"reload"		"Weapon_Pistol.Reload"
		"reload_npc"	"Weapon_Pistol.NPC_Reload"
		"empty"			"Weapon_Pistol.Empty"
		"single_shot"	"Weapon_Pistol.Single"
		"single_shot_npc"	"Weapon_Pistol.NPC_Single"
		"special1"		"Weapon_Pistol.Special1"
		"special2"		"Weapon_Pistol.Special2"
		"burst"			"Weapon_Pistol.Burst"
	}

	// Weapon Sprite data is loaded by the Client DLL.
	TextureData
	{
		"weapon"
		{
				"font"		"WeaponIcons"
				"character"	"d"
		}
		"weapon_s"
		{	
				"font"		"WeaponIconsSelected"
				"character"	"d"
		}
		"ammo"
		{
				"font"		"WeaponIcons"
				"character"	"p"
		}
		"crosshair"
		{
				"font"		"Crosshairs"
				"character"	"Q"
		}
		"autoaim"
		{
				"file"		"sprites/crosshairs"
				"x"			"0"
				"y"			"48"
				"width"		"24"
				"height"	"24"
		}
	}
}



Now run your mod and you should see that your new gun is in there. The only really annoying thing is that Valve does not include Episode 1 content into the solution, you have to do that yourself, so if you're using EP1 dll's prior to doing this tutorial, you will not have any EP1 content, that includes upgraded lighting, zombine, and hopwire grenades.

-Jman

View comments ( 15 )

Back to top