WEAPON
This is a list of all methods only available for weapons. It is also possible to call Entity functions on weapons.
A list of available methods has been expanded in your navigation bar.
Inherits methods from Entity.
Methods
boolean Weapon:AllowsAutoSwitchFrom()
Returns whether the weapon allows to being switched from when a better ( Weapon:GetWeight ) weapon is being picked up.
boolean Weapon:AllowsAutoSwitchTo()
Returns whether the weapon allows to being switched to when a better ( Weapon:GetWeight ) weapon is being picked up.
Calls a SWEP function on client. Does nothing on client.
This uses the usermessage internally, because of that, the combined length of the arguments of this function may not exceed 254 bytes/characters or the function will cease to function!
Returns how much primary ammo is in the magazine.
This is not shared between clients and will instead return the maximum primary clip size.
Returns how much secondary ammo is in the magazine.
This is not shared between clients and will instead return the maximum secondary clip size.
Forces the weapon to reload while playing given animation.
This will stop the Weapon:Think function from getting called while the weapon is reloading!
number Weapon:GetActivity()
Returns the sequence enumeration number that the weapon is playing.
This can return inconsistent results between the server and client. Issue Tracker: 2543
number Weapon:GetDeploySpeed()
Returns the weapon deploy speed, as set by Weapon:SetDeploySpeed. If not previously set, the value will be polled from the sv_defaultdeployspeed ConVar.
string Weapon:GetHoldType()
Returns the hold type of the weapon.
number Weapon:GetMaxClip1()
Returns maximum primary clip size
number Weapon:GetMaxClip2()
Returns maximum secondary clip size
number Weapon:GetNextPrimaryFire()
Gets the next time the weapon can primary fire. ( Can call WEAPON:PrimaryAttack )
number Weapon:GetNextSecondaryFire()
Gets the next time the weapon can secondary fire. ( Can call WEAPON:SecondaryAttack )
number Weapon:GetPrimaryAmmoType()
Gets the primary ammo type of the given weapon.
string Weapon:GetPrintName()
Returns the non-internal name of the weapon, that should be for displaying.
If that returns an untranslated message (#HL2_XX), use language. GetPhrase to see the "nice" name. If SWEP. PrintName is not set in the Weapon or the Weapon Base then "<MISSING SWEP PRINT NAME>" will be returned.
number Weapon:GetSecondaryAmmoType()
Gets the ammo type of the given weapons secondary fire.
number Weapon:GetSlotPos()
Returns slot position of the weapon
string Weapon:GetWeaponViewModel()
Returns the view model of the weapon.
string Weapon:GetWeaponWorldModel()
Returns the world model of the weapon.
Returns the "weight" of the weapon, which is used when deciding which Weapon is better by the engine.
Returns whether the weapon has ammo left or not. It will return false when there's no ammo left in the magazine and when there's no reserve ammo left.
This will return true for weapons like crowbar, gravity gun, etc.
boolean Weapon:IsCarriedByLocalPlayer()
Returns whenever the weapon is carried by the local player.
boolean Weapon:IsScripted()
Checks if the weapon is a SWEP or a built-in weapon.
boolean Weapon:IsWeaponVisible()
Returns whether the weapon is visible. The term visibility is not exactly what gets checked here, first it checks if the owner is a player, then checks if the active view model has EF_NODRAW flag NOT set.
number Weapon:LastShootTime()
Returns the time since this weapon last fired a bullet with Entity:FireBullets in seconds. It is not networked.
Weapon:SendWeaponAnim( number act )
Forces weapon to play activity/animation.
Weapon:SetActivity( number act )
Sets the activity the weapon is playing.
See also Weapon:GetActivity.
Lets you change the number of bullets in the given weapons primary clip.
Lets you change the number of bullets in the given weapons secondary clip.
Weapon:SetDeploySpeed( number speed )
Sets the weapon deploy speed. This value needs to match on client and server.
Weapon:SetHoldType( string name )
Sets the hold type of the weapon. This function also calls WEAPON:SetWeaponHoldType and properly networks it to all clients.
This only works on scripted weapons. Using this function on weapons held by bots will not network holdtype changes to clients if the world model is set to an empty string (SWEP. WorldModel = "").
Weapon:SetLastShootTime( number time = CurTime() )
Sets the time since this weapon last fired in seconds. Used in conjunction with Weapon:LastShootTime
Weapon:SetNextPrimaryFire( number time )
Sets when the weapon can fire again. Time should be based on CurTime.
The standard HL2 Pistol (weapon_pistol) bypasses this function due to an internal implementation. This will fire extra bullets if the time is set to less than CurTime. Issue Tracker: 3786
Weapon:SetNextSecondaryFire( number time )
Sets when the weapon can alt-fire again. Time should be based on CurTime.
Inherits methods from Entity.
Events
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.
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.
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.
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.
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.
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.
The player has picked up the weapon and has taken the ammo from it.
The weapon will be removed immediately 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.
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.
Vector WEAPON:GetTracerOrigin()
Allows you to override where the tracer effect comes from. ( Visual bullets )
This hook allows you to adjust view model position and angles.
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
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
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.
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.
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.
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.
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 weapon 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 (for your convenience), however it will not be recognized as a predicted hook via Player:GetCurrentCommand, and will run more often in this case.
This hook will be called before Player movement is processed on the client, and after on the server.
This hook only runs while the weapon is in players hands. It does not run while it is carried by an NPC. 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.
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.