Adding New Weapons

Tutorial collection, comprehensive listings on main site.

Adding New Weapons

Postby Tutorial on Wed Nov 22, 2006 10:52 pm

category
Miscellaneous

description
A guide on what it takes to add your own weapons to HL2.

keywords
adding, add, new, weapons, weapon, models, model, code, coding.

Adding a weapon into Half-Life 2 can be a complicated task, some people assume you just create a model and then the rest takes care of itself; this is not the case. Adding 1 weapon requires you to make and/or edit many files.
This guide will NOT teach you how to model a weapon; it is simply an overview of the process showing which files are affected, giving you an understanding of how to begin adding your own new weapons.

The models
Weapons consist of 2 models, each with their own prefix. A model named v_???.mdl is a view model this is the model that is seen from the players perspective, this normally consists of not only the weapon model but also the player hands.
Next is the w_???,mdl; this is know as the world model these models are used for weapons laying on the ground and also for weapons that you can see in other characters/players hands.
Lets look at the pistol for example:

-- Pic of v_pistol --
Image
-- Pic of w_pistol --
Image

As you can see the world model is considerably uglier, this is for optimisation reasons. You will rarely notice the quality of a world model in-game, as it is not in the players view to the same extent as the view model.

Once you have made the relevant models, they go into the folder:
/nameofmodfolder/models/weapons/

The Textures
The textures/skins for the weapons also need to be placed in the correct folder so once the textures are done, they need to be placed in:
/nameofmodfolder/materials/models/weapons/

weapon_???.txt
Each weapon has its own script file which can be edited in any .txt program, the weapon script file has many different purposes, which include:- model file names, bucket info, clip size, ammo type, weight, sounds, icons, crosshairs.
This is an example of the weapon_pistol.txt file:

Code: Select all
// Pistol

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

   "clip_size"         "18"
   "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"
      }
   }
}


so create your weapon_nameofweapon.txt file in the same format as shown and save it in the following folder:

/nameofmodfolder/scripts/

Name of Weapon for HUD
Backtracking ever so slightly to the weapon script file just mentioned, look at the line:
"printname" "#HL2_Pistol"

This is setting up the name that will be shown on the HUD during the weapon selection menu. To change this you need to go to the folder:
/nameofmodfolder/resource/
and open up the hl2_english.txt (or indeed, it may be called something different depending on if you’ve made your own)

In this file scroll down and you can see this line:
"HL2_Pistol" "9MM PISTOL"
This is calling “HL2_Pistol” “9mm Pistol”, so to add your own you need to reference the name of your weapon in both files (hl2_english and weapon_???.txt)

Here is an example:
In weapon_???.txt:
"printname" "#HL2_Zapper"
so in hl2_english.txt:
"HL2_Zapper" "Tazer Gun "

To clarify; by adding the above to your hl2_english.txt, the weapon icon on the HUD will now say "Tazer Gun". If you were to not make this change then it would say "#HL2_Zapper"

Making your weapon show in Hammer
To get your weapon to show in hammer you need to add the entry to your .fgd file, which should be saved in sourcesdk/bin/

Open it up in notepad or similar, and find where the other (HL2) weapons are referenced. Add yours with them, here is an example:
@PointClass base(Weapon) studio("models/weapons/w_tazer.mdl") = weapon_tazer : "Tazer" []

Code your Weapon
When it comes to coding your weapon there are a few files that need to be created/ modified. They are as follows:

/game_shared/hl2mp/weapon_???.cpp
/dlls/hl2_dll/weapon_???.cpp
and lastly add the new weapon into
-cl_dll/chl2_hud/c_weapon__stubs_hl2.cpp

also, if you want your weapon to be given with the impulse 101 cheat, add it in this file:
/dlls/player.cpp

NOTE: some of the directory structures may be different, following SDK updates etc. this was written after the large August 2006 update

There you have it; you should now have an understanding of how to get a new weapon in-game. Be aware that there is a lot of work to be done that I have not mentioned in detail, such as the code or explaining the weapon_???,txt file fully. Most people should be able to figure out the rest.

Good luck and I hope to see some new weapons being made soon.

-Mr Greenfish
- Don't send PM's to this user -
Tutorial
Not A Real User
 
Joined: Sun Mar 06, 2005 11:00 pm

Postby RotatorSplint on Thu Mar 22, 2007 6:53 pm

Hey, I just finnished my new pistol model and have it all working in-game. My problem is that putting weapon_pistol.txt in the Half-Life 2/hl2/scripts folder is not overriding the presence of the weapon_pistol.txt in the .GCF. My clip size remains the same. Additionally, game_sounds_weapons.txt isn't being overridden either (I want to get rid of that shitty HL2 pistol firing sound, so I made the necesssary changes and saved that one in the scripts folder too).

Any help?
For fast-acting relief, try slowing down.
User avatar
RotatorSplint
Regular
Regular
 
Joined: Tue Jan 23, 2007 9:43 pm
Location: East Coast, US

Postby mr greenfish on Thu Mar 22, 2007 9:39 pm

i'm not sure if it's possible to do this to vanilla halflife2... but with a mod it would definitely work
Image Available now
User avatar
mr greenfish
1337 p0st3r
1337 p0st3r
 
Joined: Sat Dec 04, 2004 12:05 pm

Postby RotatorSplint on Thu Mar 22, 2007 10:04 pm

Oh, okay.

I figured that if you could replace textures, sounds, and models, perhaps you could replace scripts.

Guess not :P . I can always make the 'fire' and 'reload' sounds reference a sound that makes no sound (ie, a blank recording), and then add in my sounds via keyframe.

Thanks!
For fast-acting relief, try slowing down.
User avatar
RotatorSplint
Regular
Regular
 
Joined: Tue Jan 23, 2007 9:43 pm
Location: East Coast, US

Postby Head on Thu Mar 22, 2007 10:17 pm

or just replace the hl2 pistol sounds :P
Image
User avatar
Head
1337 p0st3r
1337 p0st3r
 
Joined: Fri Apr 29, 2005 3:21 pm
Location: Sweden ;(

Postby hyper3 on Sun Jul 15, 2007 12:30 am

Man ive tried to do this for a while now and it wont work. Followed multiple tutorials and it still wont work. Someone help, though this was easy, i know i have all the proper stuff Visual 2k5, SDK, etc. :evil:

But when i still go in game set sv_cheats 1, and try to give myself the weapon it acts of if its never been made. :O
hyper3
Member
Member
 
Joined: Sat Jul 14, 2007 7:16 pm

Re: Adding New Weapons

Postby Yoda12999 on Sat Aug 30, 2008 1:23 am

what does bucket mean?
User avatar
Yoda12999
Just Joined
Just Joined
 
Joined: Sat Aug 30, 2008 1:20 am

Re: Adding New Weapons

Postby mr greenfish on Sat Aug 30, 2008 11:19 am

"bucket" is the category/position on the weapon select menu. e.g. crowbar and gravgun are in bucket 0, pistol etc. in bucket 1 and so on

"bucket position" is the vertical location within that category. bucket position 0 is at the top and then they work their way downwards

hope that helps
User avatar
mr greenfish
1337 p0st3r
1337 p0st3r
 
Joined: Sat Dec 04, 2004 12:05 pm

Re: Adding New Weapons

Postby Ottawagunner on Mon Dec 22, 2008 9:26 pm

wheres the spot for RPM or bullet damage?
User avatar
Ottawagunner
Regular
Regular
 
Joined: Sat May 26, 2007 7:45 pm
Location: The big one with all the maple syrup

Re: Adding New Weapons

Postby anderson on Thu Nov 25, 2010 6:46 pm

Thanks for this weapon guide, now I can add my favorite weapon here. :)
anderson
Just Joined
Just Joined
 
Joined: Thu Nov 25, 2010 6:44 pm

Return to Tutorials

Who is online

Users browsing this forum: No registered users

cron