ENTITY:SpawnFunction

From Garry's Mod
Jump to: navigation, search
 ENTITY:SpawnFunction( )

Contents

Description

This is the spawn function. It's called when a client calls the entity to be spawned. If you want to make your SENT spawnable you need this function to properly create the entity.

Arguments

Player ply

The player that is spawning this SENT

Arguments

table tr

A TraceResult structure from player eyes to their aim position

Arguments

string ClassName

The classname of your entity

Examples

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


Examples

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


Personal tools
Navigation