ENTITY/SpawnFunction

From Garry's Mod
(Difference between revisions)
Jump to: navigation, search
(Note self not being an entity)
 
Line 3: Line 3:
  
 
If you want to make your SENT spawnable you need this function to properly create the entity.
 
If you want to make your SENT spawnable you need this function to properly create the entity.
 +
 +
{{Warning|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 {{LibraryFunction|scripted_ents|GetStored}}("ent_name").}}
 
|Realm=Server
 
|Realm=Server
 
|Predicted=No
 
|Predicted=No

Latest revision as of 22:11, 7 October 2019

 ENTITY:SpawnFunction( )

Contents

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.

WARNING

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

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