In this lecture, we're going to create an infinite number of waves using a brand new kind of loop.
Now, when it comes to creating loops in our code, we've already seen one kind of this at least in
two different forms.
And that is our for loop and for loops are great for all kinds of things because they run a set number
of times and we create a for loop with syntax similar to this.
So this would loop through all of the children of a game object related to our for loop is the for each
loop.
And this works pretty much the same as a regular for loop but doesn't track the iterator.
So again, we'd have syntax similar to this time loop through all of the children of a game object.
So for loops are fantastic and we've already encountered them many times so far in the course.
But there are actually two other types of loop that we haven't encountered yet.
The first of these is known as a while loop and a while loop will run continuously, providing some
condition that we give.
It is true.
So we might say that while our condition is true, then we're going to run some code.
And this is actually very similar to how things like the update method in Unity work while the game
is running.
We want to run all of the code in an update loop.
Then onto the final type of loop.
We've got a do while loop.
This is somewhat similar to a while loop, but instead it will always run our code at least once and
then continuously repeat the loop while our condition is true.
So we might say do some stuff while this condition is true.
So while loops are great, if we don't know how many times we need to run code and because we don't
specify the number of loops beforehand, we need to be very careful and beware of infinite loops because
they're very easy trap to fall into.
And I myself am notorious for doing this.
So whenever we have a while loop, we need to make sure we've got some way of setting its condition
to false to allow us to break out of it.
So with knowledge of these new kinds of loops, let's jump into our enemy spawn and see if we can't
infinitely loop through our list of wave configs.
Now, when it comes to deciding where to place our loop within our enemy spawn, we have a couple of
options, but I'm going to go with the simplest one and head down to our spawn enemy waves co-writing
because in here we can get away with pretty much just wrapping all of our code inside of our loop.
But the question still remains, do we want a wall loop here or a dual loop?
Well, I think we want a dual loop here because regardless of whether we want to loop through our list
multiple times, we do want to go through it at least once.
But rather than just show you how to do this, I think this is going to be your challenge.
I want you to go ahead and create a new serialized ball variable at the top of our script called is
looping and this is going to control the condition for our wild loop.
Then I want you to create a dual loop inside of our spawn enemy waves CO routine that will run all of
our code once and then repeatedly loop through our wave config list.
And to give you a hint for the syntax of this, we want to say do all of our code while the condition
is true and do notice the semicolon there at the end of that wall condition.
So pause the video now and give that a go.
Okay.
Welcome back.
So at the top of our script, we're going to need a new serialized field of type bull.
And we said we were going to call this is looping and then down in our spawn enemy waves co routine
we want to say do all of our code here
while is looping.
So if we now save our script and jump into unity, and if we look at our enemy spawning here, we'll
see that we've got this is looping flag to speed up testing.
Let's set our time between waves down to zero.
And for our first test we're going to leave this is looping unchecked.
If we go ahead and hit play, we should find that we just loop through the two waves in our enemy config
list.
So there goes our first wave.
Here comes our second wave, but no third wave is generated.
If we come out of play mode, check our is looping boolean and then hit play again.
This time we should loop through our two waves and then that cycle will continue to repeat.
So there's our first wave.
Here's our second wave.
Our third wave is coming in.
And now we have our fourth wave.
So our looping structure seems to be working and this will continue forever.
And if we want to break out of this loop, we just need to set is looping to false.
We're not doing that in code, but we can do it in the inspector.
So if we uncheck is looping here, we'll finish the final loop through all of our wave configs and then
no more waves get generated.
So congratulations on creating infinite waves using a do while loop and with our wave system pretty
much complete.
Now let's have a look at adding some player health and enemy damage.
We'll start looking at that in the next lecture.
So I'll see you there.