• How do I make Entities/Npcs spawn in random spots every 2hours?
    13 replies, posted
I was wondering how to make a timer spawn entities / npcs... Or basically making it so that every 2hrs or so, a random entity or npc will be spawned on the map. It would be very helpful in a scavenge type scenario, so that players can search for items that spawn every 2hrs in random spots. Any help? [I]Thanks in advance![/I]
Work our how many seconds are in two hours, so 7200. We then do a timer based off of that. [lua]timer.Create( "NPCSpawnTimer", 7200, 0 function() -- Do your NPC Spawn stuff in here end[/lua]Now i'm going to explain what that timer does. The "NPCSpawnTimer" is the name of the timer, so if need be, we can use timer.Destroy or any other timer function on it later on. The 7200 is the amount of seconds that has to pass before the function is run. The 0 means this timer runs indefinately, until stopped. [editline]07:18AM[/editline] For NPC Spawns, you must create a table with the NPC Names [lua]NPCs = { "npc_combine_s", "npc_metropolice", "npc_citizen", "npc_alyx", "npc_mossman", "npc_breen" }[/lua] Then when you set the NPC you want to spawn, you do [lua]table.Random(NPCs)[/lua] [editline]07:24AM[/editline] For spawning the NPCs, I think you would do [lua]local ents = ent.Create( table.Random(NPCs) ents:SetPos(x, y, z) ent:Spawn()[/lua] All together it should look like: [lua]NPCs = { "npc_combine_s", "npc_metropolice", "npc_citizen", "npc_alyx", "npc_mossman", "npc_breen" } timer.Create( "NPCSpawnTimer", 7200, 0 function() local ents = ent.Create( table.Random(NPCs) ents:SetPos(x, y, z) ent:Spawn() end[/lua] [editline]07:24AM[/editline] You can add more NPCs into the table, just remember the name needs to be the exact name, and it needs to be followed by a comma unless it is the last one in the table.
local ents = ent.Create( table.Random(NPCs) ) you missed a ) [editline]09:13AM[/editline] Also x y and z isnt defined anywhere, and SetPos wants a vector [editline]09:14AM[/editline] The poistions is the problem
[QUOTE=Tobba;22458876]local ents = ent.Create( table.Random(NPCs) ) you missed a ) [editline]09:13AM[/editline] Also x y and z isnt defined anywhere, and SetPos wants a vector [editline]09:14AM[/editline] The poistions is the problem[/QUOTE] I think heavy misread, he wants entities in random spots not random entities in the same spot..but couldn't you do the same basic concept but for position, of course it won't be truly random because all the positions are pre-defined by you but still if you put enough of them then it might appear to be random.
Alright, [U]First[/U] I'm assuming this is a shared file (if used in a gamemode) right? [U]Second[/U] how can I define the X Y and Z coordinates(and making it work) also wouldn't they spawn under the map/in the skybox and such? [U]Third[/U] Is there a way I can make a huge table with all of the possible entities that could be spawned, and then with the 7200second timer it will use a math random to pick, something like 10 entities from the list? (I know its a lot to ask, sorry) [B]Thank you again, and for your help![/B]
[QUOTE=Diet Taco;22459432] [U]Second[/U] how can I define the X Y and Z coordinates(and making it work) also wouldn't they spawn under the map/in the skybox and such? [/QUOTE] Well, you could go in game then do getpos I think it is and get a large collection of coordinates and put them into a single table, those coords will define the spawn pos...or there might be a way to check if you are in the ground (I know you can do it with water) I'll see if I can find anything on it.
Second: Make your Table of X,Y,Z valaues, if you want to find a perfect posistion, play singleplayer (or multiplayer) at that map and write "getpos" in console. Now, it may look like this: Look at this: [b][url=http://wiki.garrysmod.com/?title=Table.Random]Table.Random [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] You have to use the last example with numbers, and put a , between them. Like: myTable={} myTable[1]=1223.44541(x),1234.4353(Y), 68.445(z) (dont write (x) (y) and (z)) And then, you implent the table.Random inside the timer.
You could figure out how to do some advanced true random spawning system using [b][url=http://wiki.garrysmod.com/?title=Entity.OnGround]Entity.OnGround [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] to make sure the thing isn't spawning in the air or stuck in the ground. But it would probably be ridiculous and not worth it in the end.
Like: [lua] timer.Create( "NPCSpawnTimer", 7200, 0 function() local ents = ent.Create( table.Random(NPCs) ents:SetPos(Vector(table.Random(myTable))) ent:Spawn() end [/lua] Is this right, or could you drop the Vctor()?
[QUOTE=userman122;22459657]Like: [lua] timer.Create( "NPCSpawnTimer", 7200, 0 function() local ents = ent.Create( table.Random(NPCs) ents:SetPos(Vector(table.Random(myTable))) ent:Spawn() end [/lua] Is this right, or could you drop the Vctor()?[/QUOTE] drop the vector
o.K [editline]09:02AM[/editline] Simply drop the Vector(....,.,.,.,.,.,.,) if you are gonna use hte code :v: [editline]09:04AM[/editline] [QUOTE=Diet Taco;22459432]Alright, [U]First[/U] I'm assuming this is a shared file (if used in a gamemode) right? [U]Second[/U] how can I define the X Y and Z coordinates(and making it work) also wouldn't they spawn under the map/in the skybox and such? [U]Third[/U] Is there a way I can make a huge table with all of the possible entities that could be spawned, and then with the 7200second timer it will use a math random to pick, something like 10 entities from the list? (I know its a lot to ask, sorry) [B]Thank you again, and for your help![/B][/QUOTE] Third: Make 10 timers :v:
If you look at mine, no I didn't define x, y and z, because they were meant to be changed by this guy to the values he wants. You could just create a table with the vectors instead [lua]Spawns = { Vector1, Vector2, Vector3 }[/lua] and then use table random to choose random spawns
Thats what I showed him.. .-. [editline]03:59PM[/editline] :v:
nvm
Sorry, you need to Log In to post a reply to this thread.