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: Structures/SWEP

Events

boolean WEAPON:AcceptInput( string inputName, Entity activator, Entity called, string data )
Called when another entity fires an event to this entity.
number WEAPON:AdjustMouseSensitivity()
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. SWEPs that define this function will not work with the taunt camera.
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.
boolean WEAPON:CanBePickedUpByNPCs()
Called when a Citizen NPC is looking around to a (better) weapon to pickup.
boolean WEAPON:CanPrimaryAttack()
Helper function for checking for no ammo.
boolean WEAPON:CanSecondaryAttack()
Helper function for checking for no ammo.
table WEAPON:CustomAmmoDisplay()
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
boolean WEAPON:DoDrawCrosshair( number x, number y )
Called when the crosshair is about to get drawn, and allows you to override it. This function will not be called if SWEP.DrawCrosshair set to false.This function will not be called if player was affected by Player:CrosshairDisable.The arguments passed to this function are not affected by SWEP.AccurateCrosshair.Issue Tracker: 2117
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.
WEAPON:DrawHUDBackground()
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.
WEAPON:DrawWorldModelTranslucent( number flags )
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.
boolean WEAPON:FreezeMovement()
This hook allows you to freeze players screen. Player will still be able to move or shoot
number WEAPON:GetCapabilities()
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.
number, number, number WEAPON:GetNPCBurstSettings()
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.
number, number WEAPON:GetNPCRestTimes()
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.
Vector WEAPON:GetTracerOrigin()
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 immidiately 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 sometimes not called clientside. You can work around this by setting a variable in Initialize and check if it exists in WEAPON:Think. See the example below.Issue Tracker: 2732This 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. 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 th..
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:SetDeploySpeed( number speed )
Sets the weapon deploy speed. This value needs to match on client and server.
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.
boolean WEAPON:ShouldDrawViewModel()
Called to determine if the view model should be drawn or not.
boolean WEAPON:ShouldDropOnDie()
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. 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 droppin..
number WEAPON:TranslateActivity( number act )
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.

Page Links


Special Pages


Wikis

?

Render Time: 91ms

DB GetPage 59
Generate Html 8
SaveChanges (1) 9
Render Body 0
Render Sidebar 11