Post tutorial Report content RSS feed Adding a new weapon

Add your own weapon to your Half-Life 2 mod.

Posted by Sallycin on Jan 2nd, 2007
Intermediate Server Side Coding.

I'm going to walk you through creating your first weapon. To do this I'm going to show you how to make a clone of the pistol weapon which you can later customize.

I'm going to be using Microsoft Visual Studio .NET 2003 to compile, but if you are using Microsoft Visual Studio .NET 2005 read this tutorial.

[page=Opening and Copying]
Open up "Game_HL2-2003.sln" (or Game_HL2-2005.sln) in your Mod/src directory. Navigate to "weapon_pistol.cpp" in server_hl2/Source Files.

User Posted Image

Open up "weapon_pistol.cpp" then Select All (Ctrl-A) and copy (Ctrl-C) the code. Now right click on the "Source Files" folder icon to bring up a drop down list. Select Add > Add New Item...

This window will be brought up:

User Posted Image

In the name field type "weapon_*desired name*" In this case I used "weapon_pwner." Click Open and a file called weapon_pwner.cpp will be created and opened.

[page=Pasting and Editing]

Paste all the code from "weapon_pistol.cpp" (Ctrl-V) in the new file. Now is the long tedious part ;) Open up the find box (Ctrl-F). Now, type in "pistol." Go through the file and replace all instances of "Pistol" with "Pwner." Be sure to use the same capitilization schemes, Pistol would be Pwner, PISTOL would be PWNER, pistol would be pwner.

Watch out, some of the instances of Pistol in the file are links to sounds and animations. DO NOT REPLACE THESE.

Before we compile there is one more thing to do. Open: Client > Source Files > HL2 DLL > c_weapon_stubs_hl2.cpp. Find the line:
STUB_WEAPON_CLASS( weapon_pistol, WeaponPistol,C_BaseHLCombatWeapon );
Copy this and paste it under itself and change it to:
STUB_WEAPON_CLASS( weapon_pwner, WeaponPwner,C_BaseHLCombatWeapon );

We're almost done now.Before we compile we have to configure the compile. Go to Build > Configuration manager... and change the drop down menu selection from "Debug HL2" to "Release HL2."

User Posted Image

Navigate to Build > Build Solution and click it. You will most likely come up with some errors at fisrt. These are probably mistakes you made in "weapon_pwner.cpp" Double check to see that you didn't replace the animation and sound names, then compile it gain.

NOTE: If this is your first compile it will take a long time. This will not happen in the future now that you've compiled it once.

[page=Last Thing]
One last thing before you can see your new weapon. Navigate too your mod scripots folder. In my case it was located in: C:\Program Files\Steam\SteamApps\SourceMods\*the mod*\scripts). Copy the file "weapon_pistol.txt" and paste it in the same folder, but rename it "weapon_pwner.txt" Go into the file and change the bucket_position to 2 (this is the position on the drop down menu of weapons). Also, change the "printname" from #HL2_Pistol to #HL2_Pwner.

Now, go to C:\Program Files\Steam\SteamApps\SourceMods\*the mod*\resources and open "*your mod*_english.txt" Under:

"HL2_Pistol" "9MM PISTOL"
"HL2_Pwner" "Pwner Gun"

(or desired display name in-game).

[page=All Done]
Go to your mod folder and run "run_mod.bat" Now in the console screen open up a map (type map *your map name*). Bring up the console in the level and type "give weapon_pwner" and you will have your first weapon :)

User Posted Image

Post comment Comments
VGTeam Feb 4 2007 says:

OMG, i would not have known how to get my custom weapon working with out you telling where to register the new weapon, c_weapon_stubs.cpp you just saved me about 2 hrs of looking. thank you, super tutorial!!! keep making more of these!!

+6 votes     reply to comment
danm36 Feb 26 2008 says:

Great tut, though if you want to edit damage, you need to do a bit more. You have to find the code file that edit the code for the weapon settings you can edit in skill.cfg (Hope you got that). It's one code file where you may find

sk_plr_dmg_pistol etc

and such (I think). Just copy the pistol settings to the next line and edit ALL of them so it says pwner instead of pistol.



Now just go into skill.cfg (Usually in YOURMOD/cfg/)

and add

sk_plr_dmg_pwner "6"
sk_npc_dmg_pwner "6"
sk_max_pwner "150"


(Please can you refind the file to edit the common weapon properties as I forgot what it's called :(. Try searching for 'pistol')

+8 votes     reply to comment
btlmaster101 Apr 8 2008 says:

Thanks, very helpful. How do you change the weapon selection icon for your weapon though, I think it's to do with the HalfLife font but how do I access it?

And for some reason adding my weapon has changed the number font on the hud (which I changed previously), how do I fix this as I don't see what I've done to affect fonts.

+1 vote     reply to comment
Studiopaw May 5 2008 says:

1 thing...
Were do i get Microsoft Visual Studio???????
would be apreaseated

0 votes     reply to comment
Sockman111 May 1 2015 replied:

just search it on google. It's made by microsoft, so it's probably on their website.

+1 vote     reply to comment
CammieDee666 May 19 2008 says:

U Have To Buy It
Its Dear But Theres A Express [Free] Edition But I DOnt Know If It Works With This

+1 vote     reply to comment
danm36 Jun 17 2008 replied:

Express version for Visual Studio works with HL2 and Orange Box, I am using it right now!

+7 votes     reply to comment
redx475 Feb 27 2010 replied:

The version that costs money is really only for big studios. There is no reason to buy it, since it doesn't have any features you would use to make Source engine mods anyway. Plus, it costs a LOT of money and is a HUGE program.

+1 vote     reply to comment
danm36 Jun 18 2008 says:

It fails now wit something about DTClass. Help

+3 votes     reply to comment
Jimpen Jul 7 2008 says:

i cant find the last file C:\Program Files\Steam\SteamApps\SourceMods\*the mod*\resources
i only see resource and the _english.txt isnt there

+1 vote     reply to comment
lebofly Oct 16 2009 replied:

Have you ran the mod before?

+1 vote     reply to comment
Waterdotz Aug 22 2010 replied:

Im getting the same thing, I ran the mod but I still don't have the txt file.

+1 vote     reply to comment
Mynameislol Sep 17 2008 buried:


This tutorial sucks.

-34 votes     reply to comment
Mynameislol Oct 1 2008 says:

Great tutorial. "Client missing DT class CWeaponPISTOLBARN" Just ******* great.

+1 vote     reply to comment
jaumzors Oct 14 2008 says:

Yay i finally did it, worked just fine after the requirements for visual studio 2008

+2 votes     reply to comment
Aert Nov 11 2008 says:

I need HELP! I don't know whats the good changes in the weapon_(name).cpp .


+1 vote     reply to comment
hogan_skoll Dec 2 2008 says:

SOMEONE plz help me with this, i build the bitch then when i run the mod I get "Client missing DT class CWeaponWEAPONNAME" (not the actual name). Is it because im trying to make a mp mod? - someone help me out.

Btw I have virtually nil understanding/skill in c++ etc.

+2 votes     reply to comment
pigophone Apr 24 2009 says:

in the orange box sdk there is no file named c_weapon_stubs_hl2.cpp

is this vital? please help

+1 vote     reply to comment
pigophone Apr 24 2009 says:

doesnt mind you should change the weapon stub file name from c_weapon_stubs_hl2.cpp to c_weapon__stubs_hl2.cpp

2 underscores

+1 vote     reply to comment
crazyb2000 Jun 11 2009 says:

Dude I tried with a crowbar and loads of errors I can't seem to do it. I don't know which ones to to be crowbar and which ones to be axe. what are the things yuor supposed to keep cause then I get errors of .obj files. help plz and help the other guys.

+2 votes     reply to comment
freredarme Jul 8 2009 says:

1>weapon_gauss.obj : error LNK2001: symbole externe non résolu "class ConVar sk_plr_dmg_gauss" (?sk_plr_dmg_gauss@@3VConVar@@A)
1>weapon_gauss.obj : error LNK2001: symbole externe non résolu "class ConVar sk_plr_max_dmg_gauss" (?sk_plr_max_dmg_gauss@@3VConVar@@A)

For my gauss gun, i don'( understand, what is the problem !!

+1 vote     reply to comment
nosfer4tu Oct 26 2009 says:

ARGH i made mod at orange box engine and i dont have src folder inside my mod folder O_O whats wrong? please reply , i really want to learn about this :(

+1 vote     reply to comment
MatthewBPbudsModding Jul 24 2014 replied:

You probably found out by now, but the SRC folder is in the the folder where the mod is originated. (Meaning the first directory you had to type in.).

+1 vote     reply to comment
Foxtrop Nov 30 2009 says:

to anyone who has this problem:
Client missing DT class CWeaponWEAPONNAME
you should edit c_weapon__stubs_hl2.cpp
like it says in the tut:

"Before we compile there is one more thing to do. Open: Client > Source Files > HL2 DLL > c_weapon_stubs_hl2.cpp. Find the line:
STUB_WEAPON_CLASS( weapon_pistol, WeaponPistol,C_BaseHLCombatWeapon );
Copy this and paste it under itself and change it to:
STUB_WEAPON_CLASS( weapon_pwner, WeaponPwner,C_BaseHLCombatWeapon );"

check the "if" wich determines if the games is multiplayer or singleplayer

+1 vote     reply to comment
Knightcrim Dec 14 2009 says:

Is there any tool I can use other than Microsoft Visual Studio. When ever I look up Microsoft Visual Studio I get Basic and C++ and stuff. Are those what I need to do this or can I use something else?

+1 vote     reply to comment
Knightcrim Dec 15 2009 says:

Okay I fixed my other problem but I was wondering where exactly Game_HL2-2003.sln is?

0 votes     reply to comment
Hyzo Jan 1 2010 replied:

What if there is an #include "weapon_crowbar.h" should I change that one too and then create the header file?

(I didn't mean to reply to knightcrim..)

+1 vote     reply to comment
PeterHat Jan 2 2010 says:

how do you do this in the first half-life?(not source)

+1 vote     reply to comment
Siccity Jan 13 2010 says:

Still some way to go before npc's :D

+1 vote     reply to comment
nleo6 Oct 14 2010 says:

i cant find c_weapon_stubs_hl2.cpp ! can you help me please?

+1 vote     reply to comment
JrComissarValvatorez Oct 16 2010 says:

What version of Microsoft Visual Studio 2010 express works best for this?

+1 vote     reply to comment
thecooldude498 Apr 7 2011 says:

i need help with a awesome mod and i need people to help me i will credit the people who help me with it is a multiplayer game that will have the tf2 buildings and weapons and have the portal gun i will need people to make cp maps like for example the tf2 maps and to get the things like the dominating sound from hl2dm and the tf2 lady audio thing

0 votes     reply to comment
thecooldude498 Apr 7 2011 says:

oh and i forgot to say the mods name it's going to be called HTFL
or Half-TeamFortress-Life

0 votes     reply to comment
thecooldude498 Apr 7 2011 says:

i all ready got it started and running

0 votes     reply to comment
Exosoldier22 Apr 30 2011 says:

So how do we replace the model of this weapon without replacing the pistol model?

+1 vote     reply to comment
thecooldude498 May 6 2011 says:

hey i don't understand where to compile it to

+1 vote     reply to comment
MastG Jul 17 2011 says:

Extremely useful but needs some revisions.

+1 vote     reply to comment
Nullen Jan 22 2012 says:

Can this be done with NPCs?

+1 vote     reply to comment
lukeme99 Mar 18 2012 says:

I feel stupid, I accidentally replaced 2 smg1's with SMG2's so it gave me an error! ME STUPID

+2 votes     reply to comment
collector1 May 14 2012 says:

Can someone updae this to where you can use the latest Software?

+1 vote     reply to comment
LyokoDesura Apr 29 2014 says:

Would it be possible to use this technique to modify already-existing weapons (instead of creating new ones)? I'm asking because I was going to change the SPAS-12's secondary fire to toggle between pump-action & semiautomatic mode.

+1 vote     reply to comment
Guest Jul 26 2014 says:

This comment is currently awaiting admin approval, join now to view.

cpttavish Jan 16 2015 says:

countless errors after compile. when I try to run the mod I get this:

Set motd from file 'cfg/motd.txt'
Set motd_text from file 'cfg/motd_text.txt'
Dead end link: K2
'blackout.cfg' not present; not executing.
DataTable warning: No matching RecvTable for SendTable 'DT_WeaponHandgun'.
Client missing DT class CWeaponHandgun
Host_EndGame: CL_ParseClassInfo_EndClasses: CreateDecoders failed.

Don't know what to do... Please help anyone! :c

+1 vote     reply to comment
epicstufff Jun 16 2015 says:

Does this tutorial work for Half-Life 1?

+1 vote     reply to comment
FrainBreezed Jul 8 2015 replied:

nopenopenopenopenope nopenopenopenopenope nopenopenopenopenope nopenopenopenopenope

+1 vote     reply to comment
_gooby_ Aug 29 2015 says:

Can I do this if I have windows 7?

+1 vote     reply to comment
Post a comment

You are not logged in, your comment will be anonymous unless you join the community. Or sign in with your social account:

Related Games
Half-Life 2
Half-Life 2 First Person Shooter
Related Engines
Source Commercial Released 2004
Related Groups
Valve Developer & Publisher with 6 members