Revision Difference
WEAPON:PrimaryAttack#512220
<function name="PrimaryAttack" parent="WEAPON" type="hook">⤶
<ishook>yes</ishook>⤶
<description>⤶
Called when primary attack button ( +attack ) is pressed.⤶
⤶
Since this is a .⤶
⤶
When in singleplayer, this function is only called in the server realm. When in multiplayer, the hook will be called on both the server and the client in order to allow for [prediction](/gmod/Prediction).⤶
⤶
You can force the hook to always be called on client like this:⤶
⤶
```⤶
if ( game.SinglePlayer() ) then self:CallOnClient( "PrimaryAttack" ) end⤶
```⤶
⤶
⤶
Note that due to prediction, in multiplayer SWEP:PrimaryAttack is called multiple times per one "shot" with the gun. To work around that, use <page>Global.IsFirstTimePredicted</page>.⤶
</description>⤶
<realm>Shared</realm>⤶
<predicted>Yes</predicted>⤶
<hidepredictionwarning>Disable automatic prediction warning</hidepredictionwarning>⤶
</function>⤶
⤶
<example>⤶
<description>This is how it is defined in weapon_base</description>⤶
<code>⤶
function SWEP:PrimaryAttack()⤶
⤶
-- Make sure we can shoot first⤶
if ( !self:CanPrimaryAttack() ) then return end⤶
⤶
-- Play shoot sound⤶
self.Weapon:EmitSound("Weapon_AR2.Single")⤶
⤶
-- Shoot 1 bullet, 150 damage, 0.01 aimcone⤶
self:ShootBullet( 150, 1, 0.01 )⤶
⤶
-- Remove 1 bullet from our clip⤶
self:TakePrimaryAmmo( 1 )⤶
⤶
-- Punch the player's view⤶
self.Owner:ViewPunch( Angle( -1, 0, 0 ) )⤶
⤶
end⤶
</code>⤶
⤶
</example>