Spawning Zombies
2 2

22 posts in this topic

Recommended Posts

Hi all. I've just started making a mod in which killing a zombie will spawn two zombies. Unfortunately I don't know how to spawn even a single zombie. So far literally all I've got is:

 

function duplicateFunction(player,item)
   zombie.scripting.commands.World.SpawnZombie();
   zombie.scripting.commands.World.SpawnZombie();

end

Events.OnZombieDead.Add(duplicateFunction);

 

Edit: I was searching through the JavaDocs and I found the bold parts, are they right? How do I tell the game where to put them?

 

I've got pretty much no prior knowledge of modding or, indeed, lua so please don't overcomplicate things. I basically just need some kind of function to spawn a zombie and I guess I'll need to find the position of the original re-dead'd zombie. Halp pls.

Share this post


Link to post
Share on other sites

http://theindiestone.com/forums/index.php/topic/6699-spawn-a-zombie/?p=87349

 

IIRC the OnZombieDead event gives you the original zombie as a paramter, so you could use its coordinates to spawn the new ones (with a slight offset of course).

 

Thanks!

 

This is what I have now, is it along the right sort of lines?

function duplicateFunction(zombie)   X = zombie:getX();   Y = zombie:getY();   Z = zombie:getZ();   zed1 = getVirtualZombieManager():createRealZombieNow(X, Y, Z);   zed2 = getVirtualZombieManager():createRealZombieNow(X, Y, Z);endEvents.OnZombieDead.Add(duplicateFunction);

New question, if that's okay, how could I add a random delay as in you kill the zombie, the game continues as normal for between 10 and 30 seconds and then the zombies spawn? Is there some kind of delay function and random function, do I use random function from Lua or from Java?

 

Edit: I just realised that the spawn a zombie thing was on the front page. I'm going to assume that it was there the whole time and I'm a complete idiot. Hur dur.

Share this post


Link to post
Share on other sites

Not that I know of.

 

You could use OnDeadZombie to fire a counter that keeps track of the OnPlayerUpdates for example and then something like this

 

if counter >= 100000 do    spawnZombies();    countUpdates = false; -- stop updating the counter    counter = 0; -- reset counter blabla :)end

 

This is a hacky and ugly approach, but it is the only way I can think of atm. I know that Stormy made a lib that allowed you to activate and deactivate events after some time (IIRC). Might be worth looking into that also :)

Share this post


Link to post
Share on other sites

Make variables that are only needed in the same function (or file for that matter) always local. If you add them to the global name scoop there is no guarantee that their value will be what you expect it to be, since it could be changed from somewhere else in the game. (PZ's global scoop is pretty dense already so you should add more stuff to it only if it is _really_ necessary).

 

Untested:

local function duplicateFunction(this)   local x = this:getX(); -- changed getx to getX - IIRC that's the correct name.   local y = this:getY();   local z = this:getZ();   local zed = getVirtualZombieManager():createRealZombieNow(x, y, z);endEvents.OnZombieDead.Add(duplicateFunction);

Share this post


Link to post
Share on other sites

Make variables that are only needed in the same function (or file for that matter) always local. If you add them to the global name scoop there is no guarantee that their value will be what you expect it to be, since it could be changed from somewhere else in the game. (PZ's global scoop is pretty dense already so you should add more stuff to it only if it is _really_ necessary).

 

Since the variables are getting upated every time the function executes, I don't think variable conflicts will be an issue.... Still, localising them is a good habit to get into for no other reason than saving on memory in more complex programs. I cringe every time I think of NF's UI table :blush:

 

 

function duplicateFunction(this)   X = this:getx();   Y = this:gety();   Z = this:getz();   ZeD = getVirtualZombieManager():createRealZombieNow(X, Y, Z);endEvents.OnZombieDead.Add(duplicateFunction);

Any idea why this doesn't work?

 

 

Welcome to the wonderfull world of syntax errors :P

Share this post


Link to post
Share on other sites

Apart from memory local variables are accessed way faster than global ones. You also can't create closures etc. without local variables (which especially plays a role for oop in lua).

Yeah it is true that the variables might be safe if they update faster than the other ones, but that will still lead to nasty bugs as the outside variables aren't safe ;)

Lastly I think good code isn't code that works but also code that is easy to read, "dry" and crafted so that it only does what it should.

Perfect code would be like a story where the vars and functions are named in a way that comments become superfluous.

It's always hard to find the right balance between design and performance, but when I code I try to write easy-to-read first and tweak it towards performance when it is necessary.

tl,dr: globals are slow and make the code hard to read.

Share this post


Link to post
Share on other sites

I'm not suggesting that only using globals is "right" only that the main purpose of localising variables is for performance. If every single variable you use is unique then you can write complex code without doing it. Will it be performance heavy, Yes. Will it be dirty, Yes. But its still possible ;)

Personally I think excessive design gets in the way of the creative process, I will get better results if I allow what I'm doing to "grow" & this applies everything that I do, not just code

Share this post


Link to post
Share on other sites

Horay! It works now!

 

Also, why don't mods work on build 25?

 

Also, also

 

counter = 0doCount = Falsefunction duplicateFunction(this)   dupeX = this:getX();   dupeY = this:getY();   dupeZ = this:getZ();   doCount = true;endfunction countFunction(player)   if doCount == true do      counter = counter + 1;      if counter >= 10000 do         getVirtualZombieManager():createRealZombieNow(dupeX, dupeY, dupeZ);         getVirtualZombieManager():createRealZombieNow(dupeX, dupeY, dupeZ);         doCount = False;         counter = 0;      end   endendEvents.OnZombieDead.Add(duplicateFunction);Events.OnPlayerUpdate.Add(countFunction);

 

What's wrong with this? I've tried to implement that counter thing but it's just not working at all.

Share this post


Link to post
Share on other sites

In build 25, you need to put your lua files into a sub-directory of your mods lua folder.... you can name the new folder either client, server or shared....

 

As for your code.... I'm not sure. Try adding some debugging statements to see what areas of the code are or are not being accessed.... for example:

counter = 0doCount = Falsefunction duplicateFunction(this)      print("getting Zed's co-ordinates & setting timer");   dupeX = this:getX();   dupeY = this:getY();   dupeZ = this:getZ();   doCount = true;endfunction countFunction(player)   if doCount == true do            print("timer: " .. counter);      counter = counter + 1;      if counter >= 10000 do                  print ("duplicating Zed");         getVirtualZombieManager():createRealZombieNow(dupeX, dupeY, dupeZ);         getVirtualZombieManager():createRealZombieNow(dupeX, dupeY, dupeZ);         doCount = False;         counter = 0;      end   endendEvents.OnZombieDead.Add(duplicateFunction);Events.OnPlayerUpdate.Add(countFunction);

Watch the console while testing & this will tell you if your conditions & events are being met

 

It COULD be that your using do in an if statement.... I've never seen do used like that, but that doesn't mean its wrong. Usually its ifthen or for/whiledo

 

Good start anyways.... Next up is to store Zed & his counter in a table so your code can handle multiple dead zombies at a time. At the moment, if you kill another zombie before the timer runs out, the new zombie will overwrite the previous ;)

Share this post


Link to post
Share on other sites

I don't know. I'm sure I heard that they were though. I guess I was mistaken. Also everything works now so  (clyde)

 

I have a new question, should I ask in this thread or make a new one. It's still about the mod, but a completely separate part of it.

Share this post


Link to post
Share on other sites

Yes, if you use while loops the Robo will remove your mod & ban you from the forums :P

 

Seriously though, did the ifdo conditions end up working? still seems odd to me :huh:

if anything, I would have expected it to loop while the condition was met

 

Don't see why you couldn't continue this thread.... I was helping someone figure out how to move corpses with a number of sub categories....

 

Have you got it handling multiple zombies yet?

Share this post


Link to post
Share on other sites

Yep, handling multiple zombies, and killing a zombie with fire permanently kills it.

 

The new question is about sandbox options, namely how to add a new one so that rather than having to download a bunch of different mods and fiddle about with the modloader every time they want to change something, people can just choose the relevant option in the sandbox menu. I've looked around in the lua files but all I can find is hundreds of files called IsUI, none of which seemed very relevant.

Share this post


Link to post
Share on other sites

Sandbox options are in ....\lua\client\OptionScreens\SandboxOptions.lua

 

You could just add a "Zombie Duplication" Options to the mainscreen using Events.OnMainMenuEnter.Add to initialise your UI.... There will be a way to add to the Sandbox options submenu, without overwriting TIS functions, only its a little too detailed for me to figure out for you, especially at this time of the morning (yawn)

 

You could also add your options to an ingame UI.... If you want to get tricky, you should learn getFileReader()getFileWriter() so you can save weather or not your mod is affecting game play or not between game sessions ;)

Share this post


Link to post
Share on other sites

Okay, didn't feel like making another thread but how the hell does one generate random numbers?

 

This:

require "math"x = math.random()print(x);

crashes my game as does any other use of the function. It isn't the math function itself because the debug window thing tells me that it's on line 2. What gives?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
2 2