Garry's Mod Wiki

ENTITY:SpawnFunction

  ENTITY:SpawnFunction( Player ply, table tr, string ClassName )

Description

This is the spawn function. It's called when a player spawns the entity from the spawnmenu.

If you want to make your SENT spawnable you need this function to properly create the entity.

Unlike other ENTITY functions, the "self" parameter of this function is not an entity but rather the table used to generate the SENT. This table is equivalent to scripted_ents.GetStored("ent_name").

Arguments

1 Player ply
The player that is spawning this SENT
2 table tr
A TraceResult structure from player eyes to their aim position
3 string ClassName
The classname of your entity

Example

This is how it is defined in sent_ball

function ENT:SpawnFunction( ply, tr, ClassName ) if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos + tr.HitNormal * 16 local ent = ents.Create( ClassName ) ent:SetPos( SpawnPos ) ent:SetBallSize( math.random( 16, 48 ) ) ent:Spawn() ent:Activate() return ent end

Example

This is how base_edit spawns (also rotates the entity to face the player, remove * 10 if it spawns in the air)

function ENT:SpawnFunction( ply, tr, ClassName ) if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos + tr.HitNormal * 10 local SpawnAng = ply:EyeAngles() SpawnAng.p = 0 SpawnAng.y = SpawnAng.y + 180 local ent = ents.Create( ClassName ) ent:SetPos( SpawnPos ) ent:SetAngles( SpawnAng ) ent:Spawn() ent:Activate() return ent end