• Spawning Zombie NPC's in a Gamemode
    4 replies, posted
I'm in my early stages of learning lua and how to create a basic gamemode. I've come a long way in the past week I've mastered teams timers and many other things. Now for the next step in my simple gamemode is not so simple (for me at least). [B]What I'm trying to do right now:[/B] I would like to have zombies spawn randomly on the map (gm_construct or any other normal map). How would I go about doing this I have no clue were to start all I know is you need to use vectors (lol not much help for me tho). I've tried hard to find a gamemode that spawns zombie npcs but have had no luck in finding anything or any code to help me out. [B]What I'm trying to accomplish after the basics are done:[/B] I would like to have zombies spawn and be able to script there AI to make them smart, have them climb ladders, jump off hills, and attack a certain part of the map every so often (a base or headquarters) but this is ofcourse after I have learned and "understand" the basics of spawning npcs in my gamemode. So without more crap to read you guys get the point any help would be much appreciated thanks all!
So first of all we need to create the timer spawn function. [lua] function MehZambeh() local Zambeh = ents.Create( "npc_zombie" ) Zambeh:SetPos( ZambehPosition[ math.random( 1, 3) ] ) -- I explain this table spawning below, do not panic! ZambehSpawn() end timer.Create( "Zambeh", 1, 0, MehZambeh ) [/lua] "Zambeh" is our unique timer name. 1 represents the delay ( in seconds ) 0 represents how many times it's going to do it, in this case 0 means always. MehZambeh links this timer to our MehZambeh function! You need to create several spawn positions, if not they will all spawn in the same position every second. ( To get your position type getpos in console ) I sujest making a table. [lua] ZambehSpawn = {} ZambehSpawn[1] = Vector( x, y, z ) ZambehSpawn[2] = Vector( x, y, z ) ZambehSpawn[3] = Vector( x, y, z ) [/lua] And so on I don't know how you're going to modify it's current AI, but I do know you can CapabilitiesAdd as seen here [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexde2e.html[/url] Sorry for not fully explaining this, but I was on my phone and it's kinda annoying typing here. I hope you can get something out of it, also, checkout the maurits.tv wiki, lots of useful tutorials related to NPC's.
Assuming you're using a custom map with custom spawn point entities for zombies, [lua] local spawn = table.Random( ents.FindByClass( "info_npc_spawnpoint" ) ) local zombie = ents.Create( "npc_zombie" ) zombie:SetPos( spawn:GetPos() ) zombie:Spawn()[/lua] If you don't have a map with pre-determined spawns: [lua] local spawntable = { Vector(0,0,0), Vector(0,200,0) } local spawnpos = table.Random( spawntable ) local zombie = ents.Create( "npc_zombie" ) zombie:SetPos( spawnpos ) zombie:Spawn()[/lua] If you want to specify where the zombies spawn then run around the map and stand where you want them to spawn, then type: [code]lua_run MsgN( Entity(1):GetPos() )[/code] And add the vector to the table. To spawn zombies continually, use your gamemode's think hook. [lua] function GM:Think() if ( self.ZombieTimer or 0 ) < CurTime() then self.ZombieTimer = CurTime() + 10 // wait 10 seconds before spawning another // spawn a zombie end end [/lua] If you want to make your zombies do specific things then you're going to have to write a SNPC, and SNPCs are kind of broken at the moment. If you're just learning lua then you shouldn't even be bothering with that right now, focus on making a gamemode that actually works first and foremost.
Thanks McDunkable and twoski, I don't have a custom map with custom spawn points so I will be using the vector tables. Thanks for also letting me know about SNPC's that gives me a start later on but for now I will just be focusing on a fully working gamemode and doing the simple spawns of NPC's. I will get to work on this now and try it out thanks again!
THx for the script realy helped me out to create my own one
Sorry, you need to Log In to post a reply to this thread.