Garry's Mod Wiki

Revision Difference

ENTITY:Initialize#515552

<function name="Initialize" parent="ENTITY" type="hook">⤶ <ishook>yes</ishook>⤶ <description>⤶ Called when the entity is created. This is called when you <page>Entity:Spawn</page> the custom entity.⤶ ⤶ This is called **after** <page>ENTITY:SetupDataTables</page> and <page>GM:OnEntityCreated</page>.⤶ ⤶ <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 <page>ENTITY:Think</page>. See the example below.</bug>⤶ </description>⤶ <realm>Shared</realm>⤶ <predicted>No</predicted>⤶ </function>⤶ ⤶ <example>⤶ <description>Example Initialize function</description>⤶ <code>⤶ function ENT:Initialize()⤶ -- Sets what model to use⤶ self:SetModel( "models/props/cs_assault/money.mdl" )⤶ ⤶ -- Sets what color to use⤶ self:SetColor( Color( 200, 255, 200 ) )⤶ ⤶ -- Physics stuff⤶ self:SetMoveType( MOVETYPE_VPHYSICS )⤶ self:SetSolid( SOLID_VPHYSICS )⤶ ⤶ -- Init physics only on server, so it doesn't mess up physgun beam⤶ if ( SERVER ) then self:PhysicsInit( SOLID_VPHYSICS ) end⤶ ⤶ -- Make prop to fall on spawn⤶ local phys = self:GetPhysicsObject()⤶ if ( IsValid( phys ) ) then phys:Wake() end⤶ end⤶ </code>⤶ ⤶ </example>⤶ ⤶ ⤶ <example>⤶ <description>Fixes the function not being called clientside.</description>⤶ <code>⤶ 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⤶ </code>⤶ ⤶ </example>