How would I make a script to spawn npc over time at a co ordinate set on the map?
An example of what I mean would be Zombies in Zmod, I want an autospawning script that will spawn an NPC every 20 seconds, can someone please help me out or direct me to somewhere that I can learn from?
All help is greatly appreciated, thanks.
timer.Create
[code]function ZombieSpawns()
local zombie = ents.Create("npc_zombie")
zombie:SetPos( Vector( -393.009399, 5451.424316, -10111.968750 ) )
zombie:Spawn()
timer.Simple( 10, function() ZombieSpawns() end )
end
function StartZombieSpawning()
ZombieSpawns()
end
hook.Add("InitPostEntity", "StartZombieSpawning", StartZombieSpawning)[/code]
[QUOTE=Mr Ibizza;44676922][code]function ZombieSpawns()
local zombie = ents.Create("npc_zombie")
zombie:SetPos( Vector( -393.009399, 5451.424316, -10111.968750 ) )
zombie:Spawn()
timer.Simple( 10, function() ZombieSpawns() end )
end
function StartZombieSpawning()
ZombieSpawns()
end
hook.Add("InitPostEntity", "StartZombieSpawning", StartZombieSpawning)[/code][/QUOTE]
Why are you hooking a function whose only purpose is to call another function? Also, seeing as [I]ZombieSpawns()[/I] takes no arguments, you don't need to wrap it within another function in the [i]timer.Simple[/i].
Could just do;
[code]function ZombieSpawns()
local zombie = ents.Create("npc_zombie")
zombie:SetPos( Vector( -393.009399, 5451.424316, -10111.968750 ) )
zombie:Spawn()
timer.Simple( 10, ZombieSpawns )
end
hook.Add("InitPostEntity", "StartZombieSpawning", ZombieSpawns )[/code]
Less functions, and a simpler [i]timer.Simple[/i].
I would also suggest setting a variable controlling the maximum amount of NPCs that you want to be spawned.
[editline]29th April 2014[/editline]
And no, use timer Create, that way he has more control over it.
In this case we don't really need to use [I]timer.Create[/I], all we need to do is re-call the function, we can do the checks inside said function, the only addition would be adding the checks inside the timer to save an additional function call if he does decide to add a max amount of NPCs in (The (n+1)th call where the max has already been reached, but it'll give a near-to-none performance boost anyway)
Something simple like this;
[code]local numZombies = 0
local maxZombies = 10
function ZombieSpawns()
if ( numZombies < maxZombies ) then
local zombie = ents.Create("npc_zombie")
zombie:SetPos( Vector( -393.009399, 5451.424316, -10111.968750 ) )
zombie:Spawn()
numZombies = numZombies + 1
timer.Simple( 10, ZombieSpawns )
end
end
hook.Add( "InitPostEntity", "StartZombieSpawning", ZombieSpawns )[/code]
Use a script to create npc_makers,
Setup the proper fields to set the NPC spawn type and limit with SetKey or w/e the function is called (I'll correct this post if I remember later when not on my phone)
Then activate it with ent:Fire.
I'll try to post an example, NPC makers are the easiest way to do what you want (since I'm assuming you don't just want infinate NPCs spawning ontop of one another, I'll also provide a pure lua implementation that doesn't use entities however since I'm just nice like that... And bored enough)
To add to what thelastpenguin said, [url=https://developer.valvesoftware.com/wiki/Npc_maker]this is the article on Valve's wiki[/url]. It has a lot of useful settings, like max active children, how often it spawns children, if it should spawn an npc when the player can see the spawner, and even allows it to have a finite amount of children before it becomes exhausted, and you can also control a fair amount through simple inputs and outputs.
Unless you need a pure Lua solution, this is probably the best route to take for simplicity.
Sorry, you need to Log In to post a reply to this thread.