• Spawning weapons/entities randomly across a map
    7 replies, posted
Hey all, I was just wondering, how i would go around spawning random entities and weapons on a map. I want them to spawn using random vectors instead of specific locations. Any help would be cool. Cheers.
[lua] local items= {} items[1] = "weapon_357" items[2] = "weapon_357" local positions = {} positions[1] = Vector(0,0,0) positions[2] = Vector(0,0,0) timer.Create( "spawn_items", 60, 0, function() local ent = ents.Create(table.random(items)) ent:SetPos(table.random(positions)) ent:Spawn() end) [/lua] Wrote this quickly, something along these lines, if you need any help with what anything does let me know.
[QUOTE=Alig96;44685753][lua] local items= {} items[1] = "weapon_357" items[1] = "weapon_357" local positions = {} positions[1] = Vector(0,0,0) positions[2] = Vector(0,0,0) timer.Create( "spawn_items", 60, 0, function() local ent = ents.Create(table.random(items)) ent:SetPos(table.random(positions)) ent:Spawn() end) [/lua] Wrote this quickly, something along these lines, if you need any help with what anything does let me know.[/QUOTE] Thanks man, i'll give it a try later. I assume it goes into lua/autorun/server
[QUOTE=Lemmie;44685774]Thanks man, i'll give it a try later. I assume it goes into lua/autorun/server[/QUOTE] It does indeed, however, you will need to modify it to your needs, as this will infinitely spawn items, and you will end up with a lot of dupes of items. I've written a quick function to check within the area if there's already an item there. (Untested) [lua] local items= {} items[1] = "weapon_357" items[2] = "weapon_357" local positions = {} positions[1] = Vector(0,0,0) positions[2] = Vector(0,0,0) function CheckArea( vec ) local founditems = ents.FindInSphere( vec, 128) for k,v in pairs(founditems) do for k2, v2 in pairs(items) do if v:GetClass() == v2:GetClass() then -- There's already an item there return false end end end -- Nothing was found in that area return true end timer.Create( "spawn_items", 60, 0, function() local pos = table.random(positions) if CheckArea( pos ) then local ent = ents.Create(pos) ent:SetPos() ent:Spawn() end end) [/lua]
[QUOTE=Alig96;44685792]It does indeed, however, you will need to modify it to your needs, as this will infinitely spawn items, and you will end up with a lot of dupes of items. I've written a quick function to check within the area if there's already an item there. (Untested) [lua] local items= {} items[1] = "weapon_357" items[2] = "weapon_357" local positions = {} positions[1] = Vector(0,0,0) positions[2] = Vector(0,0,0) function CheckArea( vec ) local founditems = ents.FindInSphere( vec, 128) for k,v in pairs(founditems) do for k2, v2 in pairs(items) do if v:GetClass() == v2:GetClass() then -- There's already an item there return false end end end -- Nothing was found in that area return true end timer.Create( "spawn_items", 60, 0, function() local pos = table.random(positions) if CheckArea( pos ) then local ent = ents.Create(pos) ent:SetPos() ent:Spawn() end end) [/lua][/QUOTE] Thanks so much! Do i keep the vectors blank or use getpos?
[QUOTE=Lemmie;44686250]Thanks so much! Do i keep the vectors blank or use getpos?[/QUOTE] Go around the map and fill in the table with all the vectors, and don't forget to add all the entity names to the items table
If you wanted to make it easier on yourself you could make a command that will save the locaiton you are currently at in a txt file. This way will make it faster for you to do every single position you want on the map. Also if you wanted to make it even easier you can make it read from that file <b>once</b> and fill a table on server start. Just make sure you do it where the file name has the map name also in there. Finally there was some code that allowed you to spawn stuff within the maps parameters, but I can not find it and I am pretty sure it would still spawn stuff within some of the props of the map. It ends up being a better option in my opinion to manually set everything.
I am sorry I randomly clicked on it, your post actually helped me. Wasnt going to write but I missclicked so I tought you should know. have a great day -Tamo
Sorry, you need to Log In to post a reply to this thread.