How hard would it be to make auto spawning Npc's.
I would be adding "npc_zombie" to spawn automatically.
So far i know it needs a timer and i would need the set multiple spawn positions.
So i think i would start with a timer, How does this look
[CODE]
timer.Create( "zombiespawn", 10, 0, function() --Dont really know here ;C
npc.create ("npc_zombie")
end)
[/CODE]
I'm not sure if it works with the _spawnpoints at top, I think it does atleast.
To add more spawnpoints, go ingame and type getpos in console and copy the "setpos" and add it to spawnpoints
[LUA]
local _spawnpoints = {};
_spawnpoints[ 0 ] = Vector( 123, 321, 0 );
_spawnpoints[ 1 ] = Vector( 321, 123, 0 );
timer.Create( "zombieSpawn", 10, 0, function()
local _e = ents.Create( "npc_zombie" );
_e:SetPos( _spawnpoints[ math.random( 0, #_spawnpoints ) ] );
_e:Spawn();
end );[/LUA]
What file should i add this to?
[QUOTE=AIX-Who;42755437]What file should i add this to?[/QUOTE]
A serverside file.
[QUOTE=Busan1;42755357][lua]
_e:SetPos( _spawnpoints[ math.random( 0, #_spawnpoints ) ] );[/lua]
[/QUOTE]
Why do people keep doing this? Why not just _e:SetPos(table.Random(_spawnpoints)) ?
[QUOTE=LEETNOOB;42755830]Why do people keep doing this? Why not just _e:SetPos(table.Random(_spawnpoints)) ?[/QUOTE]
I don't know about other people, but back when I was developing in garrysmod Lua I would tend to avoid using functions that were added as extensions to the standard libraries to avoid confusion when I was using Lua outside of garrysmod.
[QUOTE=LEETNOOB;42755830]Why do people keep doing this? Why not just _e:SetPos(table.Random(_spawnpoints)) ?[/QUOTE]
I was unaware of this function - thank you.
Hm, i put it in a lua file and put it into server/autorun
No luck, And yes i added the correct Pos
[QUOTE=LEETNOOB;42755830]Why do people keep doing this? Why not just _e:SetPos(table.Random(_spawnpoints)) ?[/QUOTE]
I normally just make a loop or use math.random to pic the spawns. My method might end up being pretty long though.
a loop? not just long extremely inefficient.
I'm rather confused, Would a loop make it work, Zombies dont spawn at all
[lua]
local GM = GM; -- you use to have to do this outside of the gamemode folder but i don't know if you have to anymore.
if ( not GM ) then
return; -- just another safeguard.
end;
GM.NPCSpawnpoints = {};
function AddSpawnPoint( vec, class, chance)
GM.NPCSpawnpoints[vec] = {class, chance};
end;
hook.Add( "InitPostEntity", "NPCSpawner", function()
timer.Create( "NPCSpawner", 60, 0, function()
for position,data in pairs( GM.NPCSpawnpoints ) do
if ( math.random(1,data[2]) == 1 ) then
local npc = ents.Create(data[1]);
npc:SetPos(position);
npc:Spawn(); -- idk if you have to do this with npcs or not
end;
end;
end);
end);
AddSpawnPoint( Vector(0,0,0), "npc_zombie", 2 ) -- 50% chance for zombie to spawn here every 60 seconds.
[/lua]
I will test that on my server, Awaiting results.
[QUOTE=LEETNOOB;42755830]Why do people keep doing this? Why not just _e:SetPos(table.Random(_spawnpoints)) ?[/QUOTE]
Doesn't math.random() return faster than table.Random() since it's a standard lua library function?
table.Random would be slower because of the way it is implemented. table.Random should really only be used on a table that contains non-numerical keys or non-consecutive numerical keys. Picking a random element of a table with only consecutive numerical indices should be done using math.random, like so:
[lua]local v = t[math.random(#t)][/lua]
This is how table.Random is implemented:
[lua]function table.Random (t)
local rk = math.random( 1, table.Count( t ) )
local i = 1
for k, v in pairs(t) do
if ( i == rk ) then return v, k end
i = i + 1
end
end[/lua]
[editline]7th November 2013[/editline]
table.Count:
[lua]function table.Count (t)
local i = 0
for k in pairs(t) do i = i + 1 end
return i
end[/lua]
[QUOTE=JustSoFaded;42784756]Doesn't math.random() return faster than table.Random() since it's a standard lua library function?[/QUOTE]
Performance [I]does not[/I] matter for things like this. There's no need to complicate things further for the purpose of optimization before it actually becomes a problem (which in a case like this, it won't).
[QUOTE=JustSoFaded;42784756]Doesn't math.random() return faster than table.Random() since it's a standard lua library function?[/QUOTE]
It would only be beneficial if you'd have to call it like 50 times every frame.
Sorry, you need to Log In to post a reply to this thread.