Gordon Freeman is back! Half-Life 2 picks up some time after the original Half-Life left off, with Gordon Freeman working for the G-Man. Along with scientist Eli Vance and his daughter Alyx, your mission is to... well, that would be telling.

Report this article Adding a new weapon

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

Posted by Sallycin on Jan 2nd, 2007 digg this super bookmark
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

Comments
VGTeam
VGTeam Feb 4 2007, 3: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!!

0 votes     reply to comment
danm36
danm36 Feb 26 2008, 11:25am 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')

+3 votes     reply to comment
btlmaster101
btlmaster101 Apr 8 2008, 6: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, 6:51pm says:

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

+1 vote     reply to comment
CammieDee666
CammieDee666 May 19 2008, 4:08pm says:

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

+2 votes     reply to comment
danm36
danm36 Jun 17 2008, 1:27pm replied:

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

+2 votes     reply to comment
danm36
danm36 Jun 18 2008, 1:14pm says:

It fails now wit something about DTClass. Help

+2 votes     reply to comment
Jimpen
Jimpen Jul 7 2008, 9: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
Mynameislol
Mynameislol Sep 17 2008, 12:59pm says:

This tutorial sucks.

0 votes     reply to comment
Mynameislol
Mynameislol Oct 1 2008, 10:54am says:

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

+2 votes     reply to comment
Post a Comment

Only registered members can share their thoughts. So come on! Join the Mod DB community today (totally free) and do things you never thought possible.

Icon
Half-Life 2
Platforms
PC, X360, PS3, XBOX
Developer & Publisher
Valve
Engine
Source
Contact
Send Message
Official Page
Valvesoftware.com
Release Date
Released Nov 10, 2004
Game Watch
Track this game
Bookmark
Digg Super bookmark
Tutorial
Browse
Tutorials
Views
3,702 (2 today)
Report Abuse
Report this article
Bookmark
Digg Super bookmark
Related Games
Half-Life 2
Half-Life 2
Single & Multiplayer First Person Shooter