• Entity spawn
    10 replies, posted
Hello. I want to entities spawn in random positions on the map. But they are spawn only on the player spawn points. How can i do it? function spawnent( ply, cmd, arg )                  local spawns = ents.FindByClass( "info_player_start" ) local random_entry = math.random(#spawns) local Ent = ents.Create( "cw_ammo_338lapua" ) Ent:SetPos( spawns[random_entry]:GetPos() + Vector (0, 0, 0 ) ) Ent:Spawn ()      end concommand.Add( "spawn_entities", spawnent )
Because you made them spawn in spawnpoints by yourself. local spawns = ents.FindByClass( "info_player_start" ) This is SpawnPoints. Also, instead of local random_entry = math.random(#spawns) -- and spawns[random_entry] You can do this Ent:SetPos( table.Random(spawns):GetPos() + Vector (0, 0, 0 ) )
function spawnent( ply, cmd, arg )                  local spawns = ents.FindByClass( "info_player_start" ) local Ent = ents.Create( "cw_ammo_338lapua" ) Ent:SetPos( table.Random(spawns):GetPos() + Vector (0, 0, 0 ) ) Ent:Spawn ()      end concommand.Add( "spawn_entities", spawnent ) still spawn on SpawnPoints
Way more inefficient for no reason.
I have a big map, and i want to entities spawn at random positions on the whole map. Does exist some function that can do it?
No - you could literally spawn boxes in random places but you would have to check if the position is in the world, not colliding with any objects, reachable by players, etc. In theory you could somewhat do it, but it would be a complex solution.
Okay When i put "spawn_entities" in console, it spawn only 1 crate, how can i spawn, for example, 100 crates in once?
You can use a for-loop to run the same code multiple times. Ex. for i = 1, 100 do -- Operations end I might be a good idea to spread out the spawning a bit so the server doesn't have to spawn so many at once, causing lag everytime spawning occurs.
It's works, ty)) So, if i want to spawn crates on the whole map, i should use something like this? Ent:SetPos( table.Random(spawns):GetPos() + Vector (0, 0, 0 ) ) Ent:SetPos( table.Random(spawns):GetPos() + Vector (1000, 0, 0 ) ) Ent:SetPos( table.Random(spawns):GetPos() + Vector (2000, 0, 0 ) )
I would do like that. local AmmoSpawnPoints = { Vector(123,-40,10000), Vector(56,-324,8900), Vector(123,-40,7000), Vector(123,-5000,11000) --It's random values, I fabricate them } function RefillAmmo() for I=1,30 do local E = ents.Create(*your ammo class*) E:SetPos(table.Random(AmmoSpawnPoints)+Vector(math.random(-100,100),math.random(-100,100),0)) -- I don't want to random height level, so only X and Y E:Spawn() E:PhysWake() E:Activate() end end This code doesn't fit with bigger amount of spawns. To fix it, you need to create cooldowns markers (timers, etc)
How many Vectors can i add? When i tried to add 1 more vector, ammo don't spawn there. This 4 is working. Vector(123,-40,10000), Vector(56,-324,8900), Vector(123,-40,7000), Vector(123,-5000,11000)
Sorry, you need to Log In to post a reply to this thread.