Now Onto Part 2: The Code.
Now that we've hexed the weapon were going to code it up into a whole new weapon with working shotgun.
Now what I like to do is go into lua/weapons/counter_strike in the gmod 9 directory and pick a current lua and edit that. Seeing as how it was originally a M4 replacement we're going to copy the M4. So copy that file and rename it to hk416_masterkey or something similiar.
Alright so now were stuck with this.
-- Called when the weapon is created.
function onInit( )
_SWEPSetSound( MyIndex, "single_shot", "Weapon_m4a1.Single" );
end
-- Called every frame
function onThink( )
end
-- When the player presses left mouse button
function onPrimaryAttack( )
end
-- When the player presses right mouse button
function onSecondaryAttack( )
end
function Deploy( )
end
function Holster( )
end
-- When player presses reload. Returning false means DONT RELOAD. Although this will hitch on the client.
function onReload( )
return true;
end
-- These are only accessed once when setting up the weapon
function getWeaponSwapHands()
return true;
end
function getWeaponFOV()
return 70;
end
function getWeaponSlot()
return 3;
end
function getWeaponSlotPos()
return 5;
end
function getFiresUnderwater()
return false;
end
function getReloadsSingly()
return false;
end
-- Primary Attack
function getDamage()
return 18;
end
function getPrimaryShotDelay()
return 0.09;
end
function getPrimaryIsAutomatic()
return true;
end
function getBulletSpread()
return vector3( 0.01, 0.01, 0.01 );
end
function getViewKick()
return vector3( 0.5, 0.0, 0.0);
end
function getViewKickRandom()
return vector3( 0.5, 0.5, 0.2 );
end
function getNumShotsPrimary()
return 1;
end
function getPrimaryAmmoType()
return "pistol";
end
-- Secondary attack
function getDamageSecondary()
return 150;
end
function getSecondaryShotDelay()
return 0.001;
end
function getSecondaryIsAutomatic()
return true;
end
function getBulletSpreadSecondary()
return vector3( 0.001, 0.001, 0.001 );
end
function getViewKickSecondary()
return vector3( 0.5, 0.0, 0.0);
end
function getViewKickRandomSecondary()
return vector3( 0.5, 0.5, 0.2 );
end
function getNumShotsSecondary()
return 15;
end
function getSecondaryAmmoType()
return "buckshot";
end
function getViewModel( )
return "models/weapons/v_rif_m4a1.mdl";
end
function getWorldModel( )
return "models/weapons/w_rif_m4a1.mdl";
end
function getClassName()
return "weapon_scripted";
end
function getHUDMaterial( )
return "gmod/SWEP/default";
end
function getMaxClipPrimary() -- return -1 if it doesn't use clips
return 30;
end
function getMaxClipSecondary() -- return -1 if it doesn't use clips
return 5;
end
function getDefClipPrimary() -- ammo in gun by default
return 128;
end
function getDefClipSecondary()
return 1;
end
function getAnimPrefix() -- How the player holds the weapon: pistol, smg, ar2, shotgun, rpg, phys, crossbow, melee, slam, grenade
return "smg";
end
function getPrintName()
return "M4A1";
end
-- 0 = Don't override, shoot bullets, make sound and flash
-- 1 = Don't shoot bullets but do make flash/sounds
-- 2 = Only play animations
-- 3 = Don't do anything
function getPrimaryScriptOverride()
return 0;
end
function getSecondaryScriptOverride()
return 3;
end
function getDeathIcon( )
return "d_m4a1";
end
Alright now we have a lot to change. First off we begin with sounds. We're going to need a new sound for our shotgun that's attached to the rifle. So we're going to go to FPSBanana yet again for a sound. For the tutorial will be using this one though:
https://www.fpsbanana.com/sounds/1674 Now take this sound and rename it. Then put it into sound/weapons and make a new folder called HK416 or something similar and place the sound in there.
Now head down to the part of code where it says
-- When the player presses right mouse button
function onSecondaryAttack( )
end
Now add this line in between the end and the function on secondary attack.
_EntEmitSound(Owner,"weapons/hk416/enter sound name here.wav");
(Please note change enter sound name here" with whatever you named the shotgun sound to.)
Now we should have something like this
function onSecondaryAttack( )
_EntEmitSound(Owner,"weapons/hk416/enter sound name here.wav");
end
We're done with the sound so lets mess with the rest of the code.
Move down to these lines.
function getWeaponSlot()
return 3;
end
function getWeaponSlotPos()
return 5;
end
Now these define where are weapon is in our inventory. You could leave this the same but then you won't be able to use the m4a1 so we're going to change 5 to 12. This way we can use both the m4a1 and our 416.
So now we should have this:
function getWeaponSlot()
return 3;
end
function getWeaponSlotPos()
return 12;
end
Now let's move down a little more to this line:
function getSecondaryShotDelay()
return 0.001;
end
Now we want to change this so the player can't just click every two seconds and fire the swep. So open up the m3 lua file.(In the same folder we found the m4a1 lua.)
Now go down to this line in the m3 lua.
function getPrimaryShotDelay()
return 0.9;
end
As you can see the delay for the shotgun is 0.9. So we are going to transfer this over to our hk416 and change this
function getSecondaryShotDelay()
return 0.001;
end
to this
function getSecondaryShotDelay()
return 0.9;
end
Now it will have a proper delay time.
Now just below is this
function getSecondaryIsAutomatic()
return true;
end
This is a simple change. We're just going to change true to false.
If you go down another couple of lines you'll find this bit of code:
function getBulletSpreadSecondary()
return vector3( 0.001, 0.001, 0.001 );
end
We are going to change this to so it acts more like a shotgun.
Change it to this:
function getBulletSpreadSecondary()
return vector3( 0.1, 0.1, 0.1 );
end
Now we have our shotgun effect completed.
Now we're going to change the model and the class so head down to these lines:
function getViewModel( )
return "models/weapons/v_rif_m4a1.mdl";
end
function getWorldModel( )
return "models/weapons/w_rif_m4a1.mdl";
end
function getClassName()
return "weapon_scripted";
end
And change it to this
function getViewModel( )
return "models/weapons/v_rif_hk16.mdl";
end
function getWorldModel( )
return "models/weapons/w_rif_hk16.mdl";
end
function getClassName()
return "weapon_hk416";
end
This will make sure we have the new model. The class part also makes sure we can use both the m4a1 and the 416 together. If it was left the same you would just be able to pick one up and use the other for ammo.
Now head down to this line:
function getPrintName()
return "M4A1";
end
This tells us the name of the weapon and the name that will show up in our inventory. Change it to this
function getPrintName()
return "HK416_Masterkey";
end
Now we're almost done. Head down to the last lines and find this:
function getSecondaryScriptOverride()
return 3;
end
Change the 3 to 0. This will make sure our shotgun fires.
Now save your file. Go to your lua/weapons/ folder and create a new folder called "My Weapons" Put your lua in there. Now you're done. Start up gmod and mess around with your new swep.