1
00:00:03,820 --> 00:00:09,130
In this lecture, we're going to create an infinite number of waves using a brand new kind of loop.

2
00:00:09,460 --> 00:00:14,440
Now, when it comes to creating loops in our code, we've already seen one kind of this at least in

3
00:00:14,440 --> 00:00:15,700
two different forms.

4
00:00:15,700 --> 00:00:22,090
And that is our for loop and for loops are great for all kinds of things because they run a set number

5
00:00:22,090 --> 00:00:25,870
of times and we create a for loop with syntax similar to this.

6
00:00:25,870 --> 00:00:32,049
So this would loop through all of the children of a game object related to our for loop is the for each

7
00:00:32,049 --> 00:00:32,530
loop.

8
00:00:32,530 --> 00:00:37,720
And this works pretty much the same as a regular for loop but doesn't track the iterator.

9
00:00:37,720 --> 00:00:42,880
So again, we'd have syntax similar to this time loop through all of the children of a game object.

10
00:00:43,240 --> 00:00:48,370
So for loops are fantastic and we've already encountered them many times so far in the course.

11
00:00:48,370 --> 00:00:52,000
But there are actually two other types of loop that we haven't encountered yet.

12
00:00:52,210 --> 00:00:58,840
The first of these is known as a while loop and a while loop will run continuously, providing some

13
00:00:58,840 --> 00:00:59,800
condition that we give.

14
00:00:59,800 --> 00:01:00,850
It is true.

15
00:01:00,850 --> 00:01:06,280
So we might say that while our condition is true, then we're going to run some code.

16
00:01:06,760 --> 00:01:12,280
And this is actually very similar to how things like the update method in Unity work while the game

17
00:01:12,280 --> 00:01:13,060
is running.

18
00:01:13,060 --> 00:01:15,820
We want to run all of the code in an update loop.

19
00:01:16,240 --> 00:01:18,400
Then onto the final type of loop.

20
00:01:18,400 --> 00:01:20,710
We've got a do while loop.

21
00:01:20,770 --> 00:01:26,770
This is somewhat similar to a while loop, but instead it will always run our code at least once and

22
00:01:26,770 --> 00:01:30,310
then continuously repeat the loop while our condition is true.

23
00:01:30,340 --> 00:01:35,050
So we might say do some stuff while this condition is true.

24
00:01:35,410 --> 00:01:40,720
So while loops are great, if we don't know how many times we need to run code and because we don't

25
00:01:40,720 --> 00:01:47,320
specify the number of loops beforehand, we need to be very careful and beware of infinite loops because

26
00:01:47,320 --> 00:01:49,450
they're very easy trap to fall into.

27
00:01:49,450 --> 00:01:52,210
And I myself am notorious for doing this.

28
00:01:52,270 --> 00:01:57,520
So whenever we have a while loop, we need to make sure we've got some way of setting its condition

29
00:01:57,520 --> 00:02:00,100
to false to allow us to break out of it.

30
00:02:00,490 --> 00:02:06,070
So with knowledge of these new kinds of loops, let's jump into our enemy spawn and see if we can't

31
00:02:06,070 --> 00:02:08,889
infinitely loop through our list of wave configs.

32
00:02:09,220 --> 00:02:14,710
Now, when it comes to deciding where to place our loop within our enemy spawn, we have a couple of

33
00:02:14,710 --> 00:02:21,130
options, but I'm going to go with the simplest one and head down to our spawn enemy waves co-writing

34
00:02:21,130 --> 00:02:26,860
because in here we can get away with pretty much just wrapping all of our code inside of our loop.

35
00:02:27,070 --> 00:02:31,900
But the question still remains, do we want a wall loop here or a dual loop?

36
00:02:32,170 --> 00:02:38,050
Well, I think we want a dual loop here because regardless of whether we want to loop through our list

37
00:02:38,050 --> 00:02:42,100
multiple times, we do want to go through it at least once.

38
00:02:42,400 --> 00:02:47,230
But rather than just show you how to do this, I think this is going to be your challenge.

39
00:02:47,530 --> 00:02:53,530
I want you to go ahead and create a new serialized ball variable at the top of our script called is

40
00:02:53,530 --> 00:02:57,040
looping and this is going to control the condition for our wild loop.

41
00:02:57,250 --> 00:03:04,150
Then I want you to create a dual loop inside of our spawn enemy waves CO routine that will run all of

42
00:03:04,150 --> 00:03:08,380
our code once and then repeatedly loop through our wave config list.

43
00:03:08,380 --> 00:03:15,850
And to give you a hint for the syntax of this, we want to say do all of our code while the condition

44
00:03:15,850 --> 00:03:21,130
is true and do notice the semicolon there at the end of that wall condition.

45
00:03:21,220 --> 00:03:23,860
So pause the video now and give that a go.

46
00:03:29,150 --> 00:03:29,630
Okay.

47
00:03:29,630 --> 00:03:30,500
Welcome back.

48
00:03:30,530 --> 00:03:35,810
So at the top of our script, we're going to need a new serialized field of type bull.

49
00:03:36,230 --> 00:03:43,940
And we said we were going to call this is looping and then down in our spawn enemy waves co routine

50
00:03:44,330 --> 00:03:47,930
we want to say do all of our code here

51
00:03:50,570 --> 00:03:53,210
while is looping.

52
00:03:54,400 --> 00:04:00,700
So if we now save our script and jump into unity, and if we look at our enemy spawning here, we'll

53
00:04:00,700 --> 00:04:04,270
see that we've got this is looping flag to speed up testing.

54
00:04:04,270 --> 00:04:07,210
Let's set our time between waves down to zero.

55
00:04:07,240 --> 00:04:11,320
And for our first test we're going to leave this is looping unchecked.

56
00:04:11,440 --> 00:04:17,260
If we go ahead and hit play, we should find that we just loop through the two waves in our enemy config

57
00:04:17,260 --> 00:04:17,800
list.

58
00:04:17,800 --> 00:04:19,240
So there goes our first wave.

59
00:04:19,240 --> 00:04:23,260
Here comes our second wave, but no third wave is generated.

60
00:04:23,950 --> 00:04:30,250
If we come out of play mode, check our is looping boolean and then hit play again.

61
00:04:30,760 --> 00:04:36,100
This time we should loop through our two waves and then that cycle will continue to repeat.

62
00:04:36,100 --> 00:04:37,470
So there's our first wave.

63
00:04:37,480 --> 00:04:38,980
Here's our second wave.

64
00:04:39,310 --> 00:04:41,260
Our third wave is coming in.

65
00:04:42,640 --> 00:04:44,470
And now we have our fourth wave.

66
00:04:45,280 --> 00:04:50,070
So our looping structure seems to be working and this will continue forever.

67
00:04:50,080 --> 00:04:54,780
And if we want to break out of this loop, we just need to set is looping to false.

68
00:04:54,790 --> 00:04:58,450
We're not doing that in code, but we can do it in the inspector.

69
00:04:58,570 --> 00:05:04,540
So if we uncheck is looping here, we'll finish the final loop through all of our wave configs and then

70
00:05:04,540 --> 00:05:06,220
no more waves get generated.

71
00:05:06,670 --> 00:05:13,000
So congratulations on creating infinite waves using a do while loop and with our wave system pretty

72
00:05:13,000 --> 00:05:13,630
much complete.

73
00:05:13,630 --> 00:05:18,100
Now let's have a look at adding some player health and enemy damage.

74
00:05:18,100 --> 00:05:20,140
We'll start looking at that in the next lecture.

75
00:05:20,140 --> 00:05:21,100
So I'll see you there.

