Pick up the crowbar of research scientist Gordon Freeman, who finds himself on an alien-infested Earth being picked to the bone, its resources depleted, its populace dwindling. Freeman is thrust into the unenviable role of rescuing the world from the wrong he unleashed back at Black Mesa. And a lot of people, people he cares about, are counting on him.

Report article 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.

[page=Introduction]
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 );

[page=Compile]
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"
type
"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
VGTeam Feb 4 2007, 4:05am 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!!

+5 votes     reply to comment
danm36
danm36 Feb 26 2008, 12:25pm 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.

e.g.

sk_plr_dmg_pwner

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"

Done!

(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
btlmaster101 Apr 8 2008, 7:03am 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
Studiopaw May 5 2008, 7:51pm says:

Thanks!111....
1 thing...
Were do i get Microsoft Visual Studio???????
would be apreaseated
thanks

0 votes     reply to comment
CammieDee666
CammieDee666 May 19 2008, 5:08pm 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
danm36 Jun 17 2008, 2:27pm replied:

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

+7 votes     reply to comment
redx475
redx475 Feb 27 2010, 7:04pm 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
danm36 Jun 18 2008, 2:14pm says:

It fails now wit something about DTClass. Help

+3 votes     reply to comment
Jimpen
Jimpen Jul 7 2008, 10:21am 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
lebofly Oct 16 2009, 5:43pm replied:

Have you ran the mod before?

+1 vote     reply to comment
Waterdotz
Waterdotz Aug 22 2010, 1:06am 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
Mynameislol Sep 17 2008, 1:59pm buried:

(buried)

This tutorial sucks.

-31 votes     reply to comment
Mynameislol
Mynameislol Oct 1 2008, 11:54am says:

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

+1 vote     reply to comment
jaumzors
jaumzors Oct 14 2008, 12:19am says:

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

+2 votes     reply to comment
Aert
Aert Nov 11 2008, 8:24am says:

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

HELP ME PLS...

+1 vote     reply to comment
hogan_skoll
hogan_skoll Dec 2 2008, 11:16pm 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
pigophone Apr 24 2009, 7:36pm 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
pigophone Apr 24 2009, 7:50pm 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
crazyb2000 Jun 11 2009, 5:11pm 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
freredarme Jul 8 2009, 11:16am 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
KOLIAakaIL2
KOLIAakaIL2 Aug 14 2009, 3:14pm says:

Recompiling Half-Life 2 Source Code with new features,like a new npc's,weapons is hard job.A tried to add monster_zombie,monster_apache,weapon_glock and other half-life:source enemies and weapons and it's failed.....

+1 vote     reply to comment
thecooldude498
thecooldude498 May 6 2011, 8:23pm replied:

yea i understand that imagine how hard it must has been to just make Fortress Forever

+1 vote     reply to comment
Nawwx
Nawwx Oct 26 2009, 3:05pm 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
Foxtrop
Foxtrop Nov 30 2009, 1:24am 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
Knightcrim Dec 14 2009, 8:17pm 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
Knightcrim Dec 15 2009, 3:15pm says:

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

0 votes     reply to comment
Hyzo
Hyzo Jan 1 2010, 4:50pm 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
PeterHat Jan 2 2010, 2:16pm says:

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

+1 vote     reply to comment
Siccity
Siccity Jan 13 2010, 4:45pm says:

Still some way to go before npc's :D

+1 vote     reply to comment
Macic
Macic Jan 18 2010, 3:47pm says:

Thank you for helping us help you help us all!

+2 votes     reply to comment
nleo6
nleo6 Oct 14 2010, 11:06am says:

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

+1 vote     reply to comment
JrComissarValvatorez
JrComissarValvatorez Oct 16 2010, 10:22am says:

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

+1 vote     reply to comment
thecooldude498
thecooldude498 Apr 7 2011, 6:34pm 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
thecooldude498 Apr 7 2011, 6:35pm 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
thecooldude498 Apr 7 2011, 6:36pm says:

i all ready got it started and running

0 votes     reply to comment
Exosoldier22
Exosoldier22 Apr 30 2011, 11:46pm says:

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

+1 vote     reply to comment
thecooldude498
thecooldude498 May 6 2011, 8:19pm says:

hey i don't understand where to compile it to

+1 vote     reply to comment
MastG
MastG Jul 17 2011, 11:58am says:

Extremely useful but needs some revisions.

+1 vote     reply to comment
Nullen
Nullen Jan 22 2012, 7:27pm says:

Can this be done with NPCs?

+1 vote     reply to comment
lukeme99
lukeme99 Mar 18 2012, 2:57pm 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
collector1 May 14 2012, 6:56pm says:

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

+1 vote     reply to comment
Post a Comment
click to sign in

You are not logged in, your comment will be anonymous unless you join the community today (totally free - or sign in with your social account on the right) which we encourage all contributors to do.

2000 characters limit; HTML formatting and smileys are not supported - text only

Icon
Half-Life 2
Platforms
Windows, PS3, X360, XBOX
Developer & Publisher
Valve
Engine
Source
Contact
Send Message
Official Page
Half-life2.com
Release Date
Released Nov 15, 2004
Game Watch
Track this game
Tutorial
Browse
Tutorials
Report Abuse
Report article
Related Games
Half-Life 2
Half-Life 2 Single & Multiplayer First Person Shooter