Creating A Multiplayer Weapon
Creating A HL2 Multiplayer Weapon
You're wanting to create a whole new weapon for your new Half-Life 2 multiplayer game, right? This tutorial will enable you to able to just that and make a basic weapon for your multiplayer mod.
weapon_rifle.cpp
First of all, we'll start by using an existing weapon, and change it to how we want it.
- Open weapon_357.cpp (or any other weapon you want to copy) [Located at server/Source Files/hl2mp/weapons in Visual Studio]
- Hit CTRL A + CTRL C to copy the contents to the clipboard
- In your server's solution just where your 357 (or whatever you've used) is located, right click the Weapons folder then select Add > Add New Item.
- Select CPP as the file type and enter weapon_rifle.cpp as the filename. Please make sure you change the save location down the bottom to [mod dir]\src\game\shared\hl2mp as it is a common file between the server and client.
- In the weapon_rifle.cpp hit CTRL + V hit save (ctrl + s)
- Now for the tricky part. You need to replace every instant of CWeapon357 with CWeaponRifle. This can be acheived with a basic find/replace procedure.
- Do not replace references to sound unless you create new ones.
- You will need to set up the Client class name separate using the example below.
#ifdef CLIENT_DLL
//#define CWeapon357 C_Weapon357
// should be instead
#define CWeaponRifle C_WeaponRifle
#endif
- You will also need to update the Entity to Class Link as indicated below
LINK_ENTITY_TO_CLASS( weapon_rifle, CWeaponRifle );
- Also make sure the references to weapon_357 (or whatever) in IMPLEMENT_NETWORKCLASS_ALIASED, LINK_ENTITY_TO_CLASS and PRECACHE_WEAPON_REGISTER are changed to weapon_rifle.
All you need to do now is add the same file to the client side, open up the client/Source Files/hl2mp/weapons and right click on the folder and select add existing. Select the same file weapon_rifle.cpp that is in the game_share dir (from before) and click add. Compile and your done.
weapon_rifle.TXT
So now you're probably happy and dandy that theres no compile errors right? Well, there's still one last thing to do. Though, the hardest part has already been dealt with. So now we need to get the weapon so it works in-game. Open up weapon_357.txt, or the corrisponding weapon file you based your weapon off, the files are located in MyMod/Scripts/. Copy it all, and paste it into a new new text file and edit what you like. Here's an example of what you will end up with:
// Rifle - Wiki tutorial :D WeaponData { // Weapon data is loaded by both the Game and Client DLLs. "printname" "#HL2_Rifle" "viewmodel" "models/weapons/v_shotgun.mdl" "playermodel" "models/weapons/w_annabelle.mdl" "anim_prefix" "shotgun" "bucket" "2" "bucket_position" "1" "clip_size" "2" "primary_ammo" "357" "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 { "empty" "Weapon_Shotgun.Empty" "reload" "Weapon_Shotgun.Reload" "special1" "Weapon_Shotgun.Special1" "single_shot" "Weapon_Shotgun.Single" "double_shot" "Weapon_Shotgun.Double" // NPC WEAPON SOUNDS "reload_npc" "Weapon_Shotgun.NPC_Reload" "single_shot_npc" "Weapon_Shotgun.NPC_Single" } // Weapon Sprite data is loaded by the Client DLL. TextureData { "weapon" { "file" "sprites/w_icons2" "x" "0" "y" "128" "width" "128" "height" "64" } "weapon_s" { "file" "sprites/w_icons2b" "x" "0" "y" "128" "width" "128" "height" "64" } "ammo" { "file" "sprites/a_icons1" "x" "55" "y" "90" "width" "73" "height" "20" } "crosshair" { "file" "sprites/crosshairs" "x" "0" "y" "48" "width" "24" "height" "24" } "autoaim" { "file" "sprites/crosshairs" "x" "0" "y" "48" "width" "24" "height" "24" } } }
Other Useful Stuff
Do you want to spawn with your newly created weapon? Well all you need to do is open hl2mp_player.cpp in your server project and find:
void CHL2MP_Player::GiveDefaultItems( void )
If you cannot find the above, press Ctrl + G and enter the line number 198, this should take you right to it. All you will need to do now is add your weapon like the others:
i.e GiveNamedItem( "weapon_rifle" );