Garry's Mod Wiki

WEAPON

Default weapon methods, that are available for use in SWEPs. These hooks will not work on non-scripted weapons, such as the Half-Life 2 weapons.

You can find all available SWEP fields here: SWEP structure

Inherits methods from Weapon.

Events

boolean WEAPON:AcceptInput( string inputName, Entity activator, Entity called, string data )
Called when another entity fires an event to this entity.
Allows you to adjust the mouse sensitivity. This hook only works if you haven't overridden GM:AdjustMouseSensitivity.
number WEAPON:Ammo1()
Returns how much of primary ammo the player has.
number WEAPON:Ammo2()
Returns how much of secondary ammo the player has.
Vector, Angle, number WEAPON:CalcView( Player ply, Vector pos, Angle ang, number fov )
Allows you to adjust player view while this weapon in use. This hook is called from the default implementation of GM:CalcView which is here. Therefore, it will not be called if any other hook added to CalcView returns any value, or if the current gamemode overrides the default hook and does not call the SWEP function.
Vector, Angle WEAPON:CalcViewModelView( Entity ViewModel, Vector OldEyePos, Angle OldEyeAng, Vector EyePos, Angle EyeAng )
Allows overriding the position and angle of the viewmodel. This hook only works if you haven't overridden GM:CalcViewModelView.
Called when a Citizen NPC is looking around to a (better) weapon to pickup.
Helper function for checking for no ammo.
Helper function for checking for no ammo.
Allows you to use any numbers you want for the ammo display on the HUD. Can be useful for weapons that don't use standard ammo.
boolean WEAPON:Deploy()
Called when player has just switched to this weapon. Due to this hook being predicted, it is not called clientside in singleplayer at all, and in multiplayer it will not be called clientside if the weapon is switched with Player:SelectWeapon or the "use" console command, however it will be called clientside with the default weapon selection menu and when using CUserCmd:SelectWeapon
Called when the crosshair is about to get drawn, and allows you to override it. This function will not be called if SWEP. DrawCrosshair is set to false or if player is affected by Player:CrosshairDisable.
boolean WEAPON:DoImpactEffect( table tr, number damageType )
Called so the weapon can override the impact effects it makes.
WEAPON:DrawHUD()
This hook allows you to draw on screen while this weapon is in use. If you want to draw a custom crosshair, consider using WEAPON:DoDrawCrosshair instead.
This hook allows you to draw on screen while this weapon is in use. This hook is called before WEAPON:DrawHUD and is equivalent of GM:HUDPaintBackground.
WEAPON:DrawWeaponSelection( number x, number y, number width, number height, number alpha )
This hook draws the selection icon in the weapon selection menu.
WEAPON:DrawWorldModel( number flags )
Called when we are about to draw the world model.
Called when we are about to draw the translucent world model.
WEAPON:Equip( Entity NewOwner )
Called when a player or NPC has picked the weapon up.
WEAPON:EquipAmmo( Player ply )
The player has picked up the weapon and has taken the ammo from it. The weapon will be removed immidiately after this call.
boolean WEAPON:FireAnimationEvent( Vector pos, Angle ang, number event, string options, Entity source )
Called before firing animation events, such as muzzle flashes or shell ejections. This will only be called serverside for 3000-range events, and clientside for 5000-range and other events.
This hook allows you to freeze players screen. Player will still be able to move or shoot
This hook is for NPCs, you return what they should try to do with it.
number WEAPON:GetNPCBulletSpread( number proficiency )
Called when the weapon is used by NPCs to determine how accurate the bullets fired should be. The inaccuracy is simulated by changing the NPC:GetAimVector based on the value returned from this hook.
Called when the weapon is used by NPCs to tell the NPC how to use this weapon. Controls how long the NPC can or should shoot continuously.
Called when the weapon is used by NPCs to tell the NPC how to use this weapon. Controls amount of time the NPC can rest (not shoot) between bursts.
Allows you to override where the tracer effect comes from. ( Visual bullets )
Vector, Angle WEAPON:GetViewModelPosition( Vector EyePos, Angle EyeAng )
This hook allows you to adjust view model position and angles.
boolean WEAPON:Holster( Entity weapon )
Called when weapon tries to holster. This will only be called serverside when using Player:SelectWeapon as that function immediately switches the weapon out of prediction. This is called twice for every holster clientside, one in Prediction and one not. Issue Tracker: 2854Before WEAPON:OnRemove is called, this function is only called serverside. Issue Tracker: 3133
boolean WEAPON:HUDShouldDraw( string element )
This hook determines which parts of the HUD to draw.
WEAPON:Initialize()
Called when the weapon entity is created. Entity:GetOwner will return NULL at this point because the weapon is not equpped by a player or NPC yet. Use WEAPON:Equip or WEAPON:Deploy if you need the owner to be valid. This is not called serverside after a quicksave. Issue Tracker: 3015
boolean WEAPON:KeyValue( string key, string value )
Called when the engine sets a value for this scripted weapon. See GM:EntityKeyValue for a hook that works for all entities. See ENTITY:KeyValue for an hook that works for scripted entities.
WEAPON:OnDrop()
Called when weapon is dropped by Player:DropWeapon. See also WEAPON:OwnerChanged.
WEAPON:OnReloaded()
Called whenever the weapons Lua script is reloaded.
WEAPON:OnRemove()
Called when the swep is about to be removed.
WEAPON:OnRestore()
Called when the weapon entity is reloaded from a Source Engine save (not the Sandbox saves or dupes) or on a changelevel (for example Half-Life 2 campaign level transitions). For the duplicator callbacks, see ENTITY:OnDuplicated. See also saverestore for relevant functions.
WEAPON:OwnerChanged()
Called when weapon is dropped or picked up by a new player. This can be called clientside for all players on the server if the weapon has no owner and is picked up. See also WEAPON:OnDrop.
WEAPON:PostDrawViewModel( Entity vm, Weapon weapon, Player ply )
Called after the view model has been drawn while the weapon in use. This hook is called from the default implementation of GM:PostDrawViewModel, and as such, will not occur if it has been overridden. WEAPON:ViewModelDrawn is an alternative hook which is always called before GM:PostDrawViewModel.
WEAPON:PreDrawViewModel( Entity vm, Weapon weapon, Player ply )
Allows you to modify viewmodel while the weapon in use before it is drawn. This hook only works if you haven't overridden GM:PreDrawViewModel.
WEAPON:PrimaryAttack()
Called when primary attack button ( +attack ) is pressed. 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.
WEAPON:PrintWeaponInfo( number x, number y, number alpha )
A convenience function that draws the weapon info box, used in WEAPON:DrawWeaponSelection.
WEAPON:Reload()
Called when the reload key ( +reload ) is pressed.
WEAPON:RenderScreen()
Called every frame just before GM:RenderScene. Used by the Tool Gun to render view model screens (TOOL:DrawToolScreen). Materials rendered in this hook require $ignorez parameter to draw properly.
WEAPON:SecondaryAttack()
Called when secondary attack button ( +attack2 ) is pressed. For issues with this hook being called rapidly on the client side, see the global function IsFirstTimePredicted.
WEAPON:SetupDataTables()
Called when the SWEP should set up its Data Tables.
WEAPON:SetWeaponHoldType( string name )
Sets the hold type of the weapon. This must be called on both the server and the client to work properly. NOTE: You should avoid calling this function and call Weapon:SetHoldType now.
WEAPON:ShootBullet( number damage, number num_bullets, number aimcone, string ammo_type = "self.Primary.Ammo", number force = 1, number tracer = 5 )
A convenient function to shoot bullets.
WEAPON:ShootEffects()
A convenience function to create shoot effects.
Called to determine if the view model should be drawn or not.
Should this weapon be dropped when its owner dies? This only works if the player has Player:ShouldDropWeapon set to true.
WEAPON:TakePrimaryAmmo( number amount )
A convenience function to remove primary ammo from clip.
WEAPON:TakeSecondaryAmmo( number amount )
A convenience function to remove secondary ammo from clip.
WEAPON:Think()
Called when the swep thinks. This hook won't be called during the deploy animation and when using Weapon:DefaultReload. Works only in players hands. Doesn't work in NPCs hands. Despite being a predicted hook, this hook is called clientside in single player, however it will not be recognized as a predicted hook to Player:GetCurrentCommand. This hook will be called before Player movement is processed on the client, and after on the server. This will not be run during deploy animations after a serverside-only deploy. This usually happens after picking up and dropping an object with +use. Issue Tracker: 2855
Translate a player's Activity into a weapon's activity, depending on how you want the player to be holding the weapon. For example, ACT_MP_RUN becomes ACT_HL2MP_RUN_PISTOL.
number WEAPON:TranslateFOV( number fov )
Allows to change players field of view while player holds the weapon. This hook must be defined shared and return same value on both to properly affect Area Portals.
WEAPON:ViewModelDrawn( Entity ViewModel )
Called straight after the view model has been drawn. This is called before GM:PostDrawViewModel and WEAPON:PostDrawViewModel.