WEAPON/Initialize

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
m (I think it should be a note)
m (Better note)
 
Line 2: Line 2:
 
|Description=Called when the weapon entity is created.
 
|Description=Called when the weapon entity is created.
  
{{Note|{{ClassFunction|Entity|GetOwner}} will be always NULL at this point, because the weapon is not equpped by a player or NPC yet. Use {{HookFunction|WEAPON|Equip}} if you need the owner.}}
+
{{Note|{{ClassFunction|Entity|GetOwner}} will return NULL at this point because the weapon is not equpped by a player or NPC yet. Use {{HookFunction|WEAPON|Equip}} or {{HookFunction|WEAPON|Deploy}} if you need the owner to be valid.}}
  
 
{{Bug|Issue=2732|This is sometimes not called clientside. You can work around this by setting a variable in Initialize and check if it exists in {{HookFunction|WEAPON|Think}}. See the example below.}}
 
{{Bug|Issue=2732|This is sometimes not called clientside. You can work around this by setting a variable in Initialize and check if it exists in {{HookFunction|WEAPON|Think}}. See the example below.}}

Latest revision as of 17:15, 30 November 2019

 WEAPON:Initialize( )

Contents

Description

Called when the weapon entity is created.

NOTE

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.

BUG

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: #2732

BUG

This is not called serverside after a quicksave.

Issue Tracker: #3015

Examples

Example

Sets the weapon hold type to SWEP.HoldType.

function SWEP:Initialize()

	self:SetHoldType( self.HoldType )

end


Examples

Example

Fixes the function not being called clientside.

function SWEP:Initialize()
	self.m_bInitialized = true

	-- Other code
end

function SWEP:Think()
	if (not self.m_bInitialized) then
		self:Initialize()
	end

	-- Other code
end


Personal tools
Navigation