GM/OnEntityCreated

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Added example to show that vehicles can still access base entity functions)
m (Update released)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Hook
 
{{Hook
|Description=Called right after the Entity has been made visible to Lua.
+
|Description=Called when the entity is created.
  
'''NOTE:''' In the current version of gmod, vehicles passed to this function will not be considered valid until they're fully spawned.<br>
+
{{Note|Some entities on initial map spawn are passed through this hook, and then removed in the same frame. This is used by the engine to precache things like models and sounds, so always check their validity with {{GlobalFunction|IsValid}}.}}
As a temporary workaround, see the example below. IsValid, GetClass, and any other methods that do not have to access the internal vehicle object will still work, however.
+
 
 +
{{Warning|Removing the created entity during this event can lead to unexpected problems. Use {{LibraryFunction|timer|Simple}}( 0, .... ) to safely remove the entity.}}
 
|Realm=Shared
 
|Realm=Shared
 
|Predicted=No
 
|Predicted=No
|Name=OnEntityCreated
 
|Parent=GM
 
 
}}
 
}}
 
{{Arg
 
{{Arg
Line 20: Line 19:
 
ent:EmitSound( "vo/npc/male01/no02.wav" )
 
ent:EmitSound( "vo/npc/male01/no02.wav" )
 
end
 
end
end
 
}}
 
{{Example
 
|Description=When a vehicle is created, wait until the entity is valid and change its color to blue. Without a timer the ent will not validate and you may encounter errors.
 
|Code=function GM:OnEntityCreated( ent )
 
    timer.Simple( .01, function()
 
        if IsValid( ent ) and ent:IsVehicle() then
 
            ent:SetColor( Color( 0, 0, 255 ) )
 
        end
 
    end)
 
 
end
 
end
 
}}
 
}}
Line 42: Line 31:
 
local EntList = {}
 
local EntList = {}
  
hook.Add( "OnEntityCreated", "GS - Create Soft Entity List", function( ent )
+
hook.Add( "OnEntityCreated", "SoftEntList", function( ent )
if ( not ( ent:IsValid() and TrackedEnts[ ent:GetClass() ] ) ) then return end
+
if ( not ent:IsValid() or not TrackedEnts[ ent:GetClass() ] ) then return end
 
 
 
EntList[ ent:EntIndex() ] = ent
 
EntList[ ent:EntIndex() ] = ent
 
end )
 
end )
 
}}
 
}}

Latest revision as of 01:59, 2 May 2019

 GM:OnEntityCreated( )

Contents

Description

Called when the entity is created.

NOTE

Some entities on initial map spawn are passed through this hook, and then removed in the same frame. This is used by the engine to precache things like models and sounds, so always check their validity with IsValid.

WARNING

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

Arguments

Entity entity

The entity

Examples

Example

When a prop spawns it yells.

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


Examples

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 ( not ent:IsValid() or not TrackedEnts[ ent:GetClass() ] ) then return end
	
	EntList[ ent:EntIndex() ] = ent
end )


Personal tools
Navigation