Garry's Mod Wiki

GM:OnEntityCreated

  GM:OnEntityCreated( Entity entity )

Description

Called as soon as the entity is created. Very little of the entity's properties will be initialized at this stage. (keyvalues, classname, flags, anything), especially on the serverside.

Removing the created entity during this event can lead to unexpected problems. Use SafeRemoveEntityDelayed( entity, 0 ) to safely remove the entity.

Arguments

1 Entity entity
The entity

Example

When a prop spawns it yells.

hook.Add( "OnEntityCreated", "PropScream", function( ent ) if ( ent:GetClass() == "prop_physics" ) then ent:EmitSound( "vo/npc/male01/no02.wav" ) end end )

Example

Adds all props and ragdolls into a list. More efficient alternative to looping over ents.GetAll.

local TrackedEnts = { [ "prop_physics" ] = true, [ "prop_ragdoll" ] = true } local EntList = {} hook.Add( "OnEntityCreated", "SoftEntList", function( ent ) if ( TrackedEnts[ ent:GetClass() ] ) then EntList[ ent:EntIndex() ] = ent end end )