Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
1
00:00:00,000 --> 00:00:04,057
[MUSIC]
2
2
00:00:04,057 --> 00:00:07,895
In this video, we're gonna give ourselves
a way to have our waves loop over, and
3
3
00:00:07,895 --> 00:00:08,751
over, and over.
4
4
00:00:08,751 --> 00:00:12,232
So even if you have only two ways,
and I'm gonna change my to two here,
5
5
00:00:12,232 --> 00:00:16,362
it just goes wave one, wave two, wave one,
wave two, over, and over, and over so
6
6
00:00:16,362 --> 00:00:17,552
that our game can loop.
7
7
00:00:17,552 --> 00:00:21,841
So we'll hop over into EnemeySpawner.cs,
create ourselves a variable.
8
8
00:00:21,841 --> 00:00:24,377
So we'll serialize this, so
it's available in the inspector.
9
9
00:00:24,377 --> 00:00:28,019
We're making a bool, so it's either gonna
be either true or it's gonna be false and
10
10
00:00:28,019 --> 00:00:29,113
we'll call it looping.
11
11
00:00:29,113 --> 00:00:33,839
So I have a bool called looping,
which we will initialize as false.
12
12
00:00:33,839 --> 00:00:37,951
So bool looping = false;.
13
13
00:00:37,951 --> 00:00:41,889
And then within our start method
here's something really cool.
14
14
00:00:41,889 --> 00:00:48,389
We're we can say we can change the type
of return value for our start method.
15
15
00:00:48,389 --> 00:00:53,972
So that when the engine calls start, it is
doing something a little bit different,
16
16
00:00:53,972 --> 00:00:56,364
we can change it to an IEnumerator,
17
17
00:00:56,364 --> 00:01:00,696
so that we make our start a code routine,
which is really nifty.
18
18
00:01:00,696 --> 00:01:02,520
And then within our StartCoroutine,
19
19
00:01:02,520 --> 00:01:04,803
just gonna put my curly
braces down a line here.
20
20
00:01:04,803 --> 00:01:09,553
So within our StartCoroutine
we can create a do while loop,
21
21
00:01:09,553 --> 00:01:14,686
which basically means it goes over and
over and over at do while.
22
22
00:01:14,686 --> 00:01:17,028
And we'll make it while looping is true.
23
23
00:01:17,028 --> 00:01:21,391
So we'll give ourselves a little bit
of room here and start by typing do.
24
24
00:01:21,391 --> 00:01:24,897
And then give ourselves a code
block in here of curly braces.
25
25
00:01:24,897 --> 00:01:29,763
We're going to change our stock
StockCoroutines inot our yield conditions.
26
26
00:01:29,763 --> 00:01:36,001
So yield return
StartCoroutine SpawnAllWaves.
27
27
00:01:36,001 --> 00:01:38,399
So this now becomes the yield condition.
28
28
00:01:38,399 --> 00:01:43,581
Once we get into the do while loop, we're
saying do a thing, go, go, go, go, go.
29
29
00:01:43,581 --> 00:01:45,941
And then,
once it's finished doing all this,
30
30
00:01:45,941 --> 00:01:48,777
the yield will then return
back within our start method.
31
31
00:01:48,777 --> 00:01:55,245
And we can say, close our, curly braces
here, so we've got our yield condition.
32
32
00:01:55,245 --> 00:01:58,007
And then we can say, While (looping).
33
33
00:01:58,007 --> 00:02:00,625
And this is another way of saying
While (looping) equals true.
34
34
00:02:00,625 --> 00:02:03,160
We can say while, and
then in brackets, looping.
35
35
00:02:03,160 --> 00:02:07,103
So what we're doing here is saying when
the engine call start, do this thing,
36
36
00:02:07,103 --> 00:02:08,439
start the co-routines.
37
37
00:02:08,439 --> 00:02:10,767
This is a series co-routine.
38
38
00:02:10,767 --> 00:02:12,283
And we are gonna yield.
39
39
00:02:12,283 --> 00:02:13,917
So come back to start and
40
40
00:02:13,917 --> 00:02:18,994
continue with whatever is after this
statement once this has completed.
41
41
00:02:18,994 --> 00:02:20,947
So once this completed, we come back here.
42
42
00:02:20,947 --> 00:02:22,260
We see the while.
43
43
00:02:22,260 --> 00:02:25,717
If looping is true,
then it jumps back up to the do and
44
44
00:02:25,717 --> 00:02:27,771
it starts this all over again.
45
45
00:02:27,771 --> 00:02:31,611
And because it started as part of the
yield, then it waits until it's finished,
46
46
00:02:31,611 --> 00:02:33,146
then it comes back to the while.
47
47
00:02:33,146 --> 00:02:34,660
So this is a really neat loop and
48
48
00:02:34,660 --> 00:02:38,915
a really neat way of saying we can do this
particular thing here, this SpawnAllWaves,
49
49
00:02:38,915 --> 00:02:42,146
we can do it over, and over, and
over, so we can loop it in there.
50
50
00:02:42,146 --> 00:02:45,336
So we'll save this up and hop over
into Unity, check that it all worked.
51
51
00:02:45,336 --> 00:02:47,912
The first thing we need to
do is click On for Looping.
52
52
00:02:47,912 --> 00:02:51,350
So let's check it without looping on, and
make sure it's working as we're expecting.
53
53
00:02:51,350 --> 00:02:53,907
You might wanna stop at the end
of a number of waves, and
54
54
00:02:53,907 --> 00:02:57,314
then the player can go on to another
level, so you don't want it to loop.
55
55
00:02:57,314 --> 00:02:59,045
Okay, that's it without looping.
56
56
00:02:59,045 --> 00:03:01,973
We'll click Looping On, click on Play, and
57
57
00:03:01,973 --> 00:03:05,064
we should see these waves looped over and
over.
58
58
00:03:05,064 --> 00:03:09,186
Okay, once, twice, okay, so you can see,
they just keep getting spawned,
59
59
00:03:09,186 --> 00:03:10,691
and spawned, and spawned.
60
60
00:03:10,691 --> 00:03:13,940
And so you can imagine, this is a game,
if we're spawning all these waves, and
61
61
00:03:13,940 --> 00:03:15,249
the game is just going on and on,
62
62
00:03:15,249 --> 00:03:17,893
you don't have to have to have
created 300 different waves.
63
63
00:03:17,893 --> 00:03:22,411
You can create a certain number 10 or
20 or 30 and the game will go on and
64
64
00:03:22,411 --> 00:03:23,219
on forever.
65
65
00:03:23,219 --> 00:03:27,395
And if you make your enemies engaging,
if you make your waves interesting and
66
66
00:03:27,395 --> 00:03:30,992
all that kind of good stuff then
the player is gonna feel like this is
67
67
00:03:30,992 --> 00:03:33,642
a pretty awesome game to play for
a bunch of time.
68
68
00:03:33,642 --> 00:03:37,587
Okay, so it's just a quick
lecture on the do while loop and
69
69
00:03:37,587 --> 00:03:42,335
how we have used that within our
co-routine and how we have changed our
70
70
00:03:42,335 --> 00:03:47,188
Start method to be an IEnumerator so
that we can use it as a co-routine.
71
71
00:03:47,188 --> 00:03:50,803
Great work, well done, I'm sure you're
game is shaping up really nicely.
72
72
00:03:50,803 --> 00:03:53,558
I look forward you seeing your
sharing in the community.
73
73
00:03:53,558 --> 00:03:56,390
If you've got something neat that
you're enjoying with your waves,
74
74
00:03:56,390 --> 00:03:59,230
then please do share it with us, and
I'll see you in the next lecture.
7537
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.