Waves! (SOLVED)


Hello everyone, :hugs:
One more question for the day - How do we do Plants vs Zombie like waves.
I have set up the factories with different enemies as their prototypes, but how do we tell the factories which enemies to spawn and when?
Do I have to write the entire level manager script, which might not be reusable and confusing(probably full of error too.), directing the entire sequence of when to spawn and were, or is there a better way of doing it?


You can have a table with a timeline. Then you count up a value, and when the timer is equal or above a value in the timeline you spawn an enemy and then mark the value in the table as spawned. Then when the game is paused you skip adding to the timeline timer. I’ll make an example in a few minutes…


ZombieTimelineSpawn.zip (2.8 KB)

One thing I’d bet PvZ does is use two sets of timelines. The main timeline which determines when waves spawn, and then a timeline for each wave. You can apply the elements in this example to make that kind of system. So when you reach a wave to activate in the main timeline instead of spawning a zombie you activate that wave’s own timer and begin to spawn zombies from it until all zombies in it have been spawned.

Make sense? Think you could do the rest of what you want to do now?


Surely. This solved my problem. THANKS a lot. I actually didn’t knew we could use variables like that in lua. Thanks a lot again…
But one more question-

for key, value in pairs(timeline) do

Can you explain this line? I mean why do we use pairs(timeline) here and I also don’t see any references to key, value in the code - So are they predefined??
(I am a beginner in lua, so not much experience with handling tables)


The key and value are defined by you (you can change their names if you want, to make the code clearer). The variables are automatically unpacked for you by Lua when using pairs() on a table. A table is a data structure which stores pairs of data, where the data is often referred to as keys and values.

Try for instance:

for key, value in pairs(timeline) do
    print(key, "=", value)


Thanks, maybe I will be able to make my levels easily.
Here’s an example ss by what I made from this.-

How to spawn wave of enemies and the boss