LUA:Auto Spawning

From GMod Wiki

Revision as of 15:31, 10 February 2011 by garry :D (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Lua: Auto Entity Spawning
Page white text.png Description:Spawn entitys automaticly on server start.
link=User:KillerLUA Original Author:KillerLUA
Calendar.png Created:March 12, 2010

Contents


Introduction

A simple way of spawning three crane frames

Code

First, we will use init post entity to spawn the entity's the on the server start.

All of this code goes in your addons server autorun script, or a gamemodes init.lua file

We'll add the hook like this:

 
function SpawnProps()
SpawnProp(Vector( 0, 0, 0 ), "models/mymodel.mdl")
end
hook.Add("InitPostEntity","SpawnTheProps",timer.Simple(1,SpawnProps))
 

Here, we call a function to spawn a prop with the model "models/mymodel.mdl"

Now, we need to add that function:

 
function SpawnProp(position, model)
	local ent1 = ents.Create("prop_physics") 
	local ang = Vector(0,0,1):Angle();
	ang.pitch = ang.pitch + 90;
	print("Prop spawned with model: " .. model)
	ang:RotateAroundAxis(ang:Up(), math.random(0,360))
	ent1:SetAngles(ang)
	ent1:SetModel(model)
	local pos = position
	pos.z = pos.z - ent1:OBBMaxs().z
	ent1:SetPos( pos )
	ent1:Spawn()
end
 

Now, here's a full look of the code:

 
function SpawnProps()
SpawnProp(Vector( 0, 0, 0 ), "models/Cranes/crane_frame.mdl")
SpawnProp(Vector( -1985.34, -275867.3, 85.2 ), "models/Cranes/crane_frame.mdl")
end
hook.Add("InitPostEntity","SpawnTheProps",timer.Simple(1,SpawnProps))
 
function SpawnProp(position, model)
	local ent1 = ents.Create("prop_physics") 
	local ang = Vector(0,0,1):Angle();
	ang.pitch = ang.pitch + 90;
	print("Prop spawned with model: " .. model)
	ang:RotateAroundAxis(ang:Up(), math.random(0,360))
	ent1:SetAngles(ang)
	ent1:SetModel(model)
	local pos = position
	pos.z = pos.z - ent1:OBBMaxs().z
	ent1:SetPos( pos )
	ent1:Spawn()
end
 

To get the position of the where you want the prop to spawn, noclip slightly under ground. With your head above the ground and type getpos into console.

You should get three numbers,

Those are the three values you use in the Vector above.

Yarin Kaul Icon ArrowSquare32.png Back to KillerLUA Tutorials


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox