WEAPON:PrimaryAttack

From Garry's Mod
Jump to: navigation, search
 WEAPON:PrimaryAttack( )

Description

Called when primary attack button ( +attack ) is pressed.

Since this is a predicted hook, as mentioned on the bottom of the page, it has some additional behaviour to it. This is the same with all predicted SWEP hooks.

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.

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 IsFirstTimePredicted.

Examples

Example

This is how it is defined in weapon_base

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


Personal tools
Navigation