Source SDK

Source SDK

54 ratings
How to add custom weapon to Source SDK 2013 Singleplayer Mod
By Fox
This tutorial will teach you how to add rifles and pistols to Source 2013 Singleplayer Base Modification.
   
Award
Favorite
Favorited
Unfavorite
Creating weapon_(weaponname).cpp
Creating a .cpp file
Go to your "<mod source files directory>/src/game/server/hl2" and create file called "weapon_<your weapon name>.cpp" like this:

Adding your file to solution
Open your "everything.sln". In the solution explorer find "Server (HL2)" then "Source Files" and "HL2 DLL"
Right click on "HL2 DLL" -> "Add" -> "Existing File" and chose "weapon_<your weapon name>.cpp"
Editing weapon_<your weapon name>.cpp - pistols
Template Code
Copy and paste this[pastebin.com] code to your "weapon_<your weapon name>.cpp".

Change all "CWeaponGlock18" to "CWeaponYourweaponname" for example"
CWeaponFiveseven

Change all "GLOCK18" to "YOURWEAPONNAME", example:
#define GLOCK18_FASTEST_REFIRE_TIME
to
#define FIVESEVEN_FASTEST_REFIRE_TIME

Change all "glock18" to "yourweaponname".
Editing weapon_<your weapon name>.cpp - machine gun
Template Code
Copy and paste this[pastebin.com] code to your "weapon_<your weapon name>.cpp".

Change all "CWeaponSMG2" to "CWeaponYourweaponname" for example"
CWeaponM4a1

Change all "SMG2" to "YOURWEAPONNAME", example:

Change all "smg2" to "yourweaponname".

Modyfying code
Change Bullet Shoting Frequence Speed
Line 45:
float GetFireRate(void) { return 0.075f; } // Bullet Shooting Speed
Smaller Value - More Shoots per time
Adding your weapon to game
Implementing your weapon to c_weapon__stubs_hl2.cpp
Open "c_weapon__stubs_hl2.cpp" and find
STUB_WEAPON_CLASS( weapon_pistol, WeaponPistol, C_BaseHLCombatWeapon );

Copy this line and paste beneath, change "weapon_pistol" to "weapon_yourweaponname" and "WeaponPistol" to "WeaponYourWeaponname", example:

Now compile it!
weapon_<your weapon name>.txt
Creating "weapon_<your weapon name>.txt"
Go to your mod's folder (inside sourcemods) and find scripts folder. Create .txt file called weapon_<your weapon name>.txt", open it and paste following code to it:
// Glock18 WeaponData { // Weapon data is loaded by both the Game and Client DLLs. "printname" "#HL2_Glock18" "viewmodel" "models/weapons/v_pist_glock18.mdl" "playermodel" "models/weapons/w_pist_glock18.mdl" "anim_prefix" "glock18" "bucket" "2" "bucket_position" "0" "bucket_360" "0" "bucket_position_360" "0" "clip_size" "18" "primary_ammo" "Pistol" "secondary_ammo" "None" "weight" "2" "rumble" "1" "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_Glock.Single" "single_shot_npc" "Weapon_Glock.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" } "weapon_small" { "font" "WeaponIconsSmall" "character" "d" } "ammo" { "font" "WeaponIconsSmall" "character" "p" } "crosshair" { "font" "Crosshairs" "character" "Q" } "autoaim" { "file" "sprites/crosshairs" "x" "0" "y" "48" "width" "24" "height" "24" } } }
Change all "Glock18" to "your weapon name", change models to your weapon's models. Do the same to sounds.
Accesing your weapon in game
Open console and type:
"give weapon_<weapon name>"

Thats all :3
72 Comments
Competitive Racist 7 Oct, 2023 @ 12:10pm 
Works nicely, until I attempt to actually use the weapon. It does not fire a Bullet, no impact decal is made on any wall, and no blood is left from a player. (Yes the weapon_script.txt is set up right, and includes damage). The firing animation of my model, as well as the sounds play, and it consumes ammo, but won't actually fire and hit anything.
snowy 6 Aug, 2023 @ 2:52pm 
hey, you probably wont answer this but what program did u use? i tried latest vsc release but it didnt work
Bobsolete 27 Jun, 2023 @ 6:23pm 
My code in c_weapon__stubs_hl2.cpp looks nearly identical to yours, however, for some reason I keep getting the error:
] give weapon_testweapon
Attempted to create unknown entity type weapon_testweapon!
NULL Ent in GiveNamedItem!
Is there anything else that could be causing this issue?
Garry :з 21 Jun, 2023 @ 8:49am 
what do you mean exactly? (sorry to be annoying but i don't understand)
Fox  [author] 21 Jun, 2023 @ 7:44am 
@General Shepard
It's been a while since I've coded a source engine mod. Check `Weapon_Switch` and `SetAmmoCount` functions in npc code.
Garry :з 21 Jun, 2023 @ 6:35am 
i know you probably don't respond to comments anymore, but is there a way to make NPCS fire the new weapon?
QwesterGuy 8 Mar, 2023 @ 10:16am 
Hey there! Can you tell me how can i edit damage of the weapon pls? Thank you!
MazdoTheWolf 15 Jan, 2023 @ 1:12pm 
Working :steamthumbsup:
anonimowy 2503 16 Dec, 2022 @ 11:31am 
What to put in place of SoundData
Fox  [author] 12 Dec, 2022 @ 2:00pm 
@Troll212420
Once in a while