﻿1
00:00:04,360 --> 00:00:05,860
‫Hello and welcome this lecture.

2
00:00:05,860 --> 00:00:12,940
‫We're going to be learning how we can execute a piece of code variable numbers of times so that we can

3
00:00:12,940 --> 00:00:20,050
‫print out every object that overlaps inside this area, this trigger component.

4
00:00:20,050 --> 00:00:24,400
‫So you can see here that when we're both, I'm standing inside and there's a gargoyle, too.

5
00:00:24,430 --> 00:00:29,410
‫It's printing out both the gargoyles name and the player blueprint name.

6
00:00:29,410 --> 00:00:31,360
‫Let's dive in and see how this is done.

7
00:00:31,930 --> 00:00:38,890
‫Now, the issue with our code here is that our actors array may have far more than just one actor in

8
00:00:38,890 --> 00:00:39,220
‫it.

9
00:00:39,220 --> 00:00:44,380
‫And if we wanted to display all of the actors overlapping or we want to do something with all of those

10
00:00:44,380 --> 00:00:47,680
‫actors, we need to be able to run through the code.

11
00:00:47,680 --> 00:00:53,740
‫It would be highly inefficient to write an if statement for the case where we've got two or three or

12
00:00:53,740 --> 00:00:58,840
‫four or five, etc. And it certainly would never catch every case because we have got a limited amount

13
00:00:58,840 --> 00:01:00,220
‫of lines in our file.

14
00:01:00,580 --> 00:01:08,230
‫So the way we deal with this, when we want to repeat a piece of code multiple times, but a number

15
00:01:08,230 --> 00:01:14,530
‫of times that we don't actually know when we're compiling the code, we need to replace our if statement.

16
00:01:14,770 --> 00:01:20,170
‫If statement essentially says, should I execute this code or not with something that says, should

17
00:01:20,170 --> 00:01:25,300
‫I execute this code or should I execute it many times, two times, three times, four times, etc.?

18
00:01:25,450 --> 00:01:29,440
‫The way we do this is with something called a wild statement.

19
00:01:29,440 --> 00:01:32,440
‫Now it's built very much like an if statement.

20
00:01:32,440 --> 00:01:41,350
‫So the while you then put some sort of boolean value inside the parentheses, and then we have some

21
00:01:41,350 --> 00:01:42,430
‫curly braces.

22
00:01:42,430 --> 00:01:48,340
‫And inside the curly braces, that block of code that will be keep getting executed again and again

23
00:01:48,340 --> 00:01:54,340
‫until the condition in our while statement becomes false.

24
00:01:54,340 --> 00:01:55,990
‫That's why it's called while.

25
00:01:55,990 --> 00:01:59,320
‫So while this thing is true, executes this code.

26
00:01:59,320 --> 00:02:04,630
‫So it's a bit like an if statement in that sometimes it might not execute the code at all, but unlike

27
00:02:04,630 --> 00:02:09,940
‫an if statement that can only execute a piece of code once, the while statement could basically keep

28
00:02:09,940 --> 00:02:14,890
‫on executing it forever if this boolean never becomes false.

29
00:02:15,460 --> 00:02:22,420
‫So why is this useful when we could create an int 32 and call it index and start it off at zero?

30
00:02:22,900 --> 00:02:31,390
‫And then every time we go through the body of this wild loop, we increase the value stored in that

31
00:02:31,390 --> 00:02:31,750
‫index.

32
00:02:31,750 --> 00:02:36,130
‫So we could say index equals index plus one.

33
00:02:36,640 --> 00:02:42,520
‫Now in C++, there is a shorthand to doing this instead of having to do index plus one like this, you

34
00:02:42,520 --> 00:02:49,480
‫do plus plus index, and that just adds one to whatever is stored in the index variable.

35
00:02:50,110 --> 00:02:50,360
‫Okay.

36
00:02:50,470 --> 00:02:52,090
‫So we start off at zero.

37
00:02:52,120 --> 00:02:54,940
‫We every time we go around, we increase the index.

38
00:02:54,940 --> 00:03:02,620
‫And what we can do is we can make it so that we only go around this while loop, while our index is

39
00:03:02,620 --> 00:03:05,200
‫within the bounds of our array.

40
00:03:05,200 --> 00:03:11,440
‫So we can say that the index must be less than the actors dot num.

41
00:03:11,440 --> 00:03:13,210
‫So that is the size of the array.

42
00:03:13,210 --> 00:03:19,570
‫Remember that when we've got five items in array, the last item in the array is index four.

43
00:03:19,870 --> 00:03:28,450
‫And so we can see that this will be true when the index is valid, when index four is less than five.

44
00:03:28,450 --> 00:03:34,540
‫But as soon as our index goes past the end of the array, when it gets to index five, then the actor

45
00:03:34,540 --> 00:03:37,390
‫number is going to be not no longer.

46
00:03:37,390 --> 00:03:39,220
‫It's five is no longer less than five.

47
00:03:39,220 --> 00:03:45,220
‫So the while loop will stop executing and continue with the rest of the function.

48
00:03:45,700 --> 00:03:53,110
‫So what that means is we could now take the code that we had above and rewrite it.

49
00:03:53,290 --> 00:03:58,270
‫Let's take it, copy it, cut it out and stick it in here in our wild loop.

50
00:03:58,270 --> 00:04:03,520
‫But before we increase the index, most importantly, because what we're going to do is replace this

51
00:04:03,520 --> 00:04:08,560
‫zero where we were getting actor zero, we're going to replace it with actor index.

52
00:04:08,800 --> 00:04:10,210
‫So let's follow this through.

53
00:04:10,240 --> 00:04:11,950
‫How would this work?

54
00:04:12,580 --> 00:04:15,910
‫Let's remove our if statement because that's confusing matters a little bit.

55
00:04:15,910 --> 00:04:20,230
‫So we start off the value in the variable index is going to be zero.

56
00:04:20,530 --> 00:04:23,470
‫Now imagine we have two actors to keep things simple.

57
00:04:23,680 --> 00:04:27,070
‫Is zero less than two?

58
00:04:27,100 --> 00:04:27,760
‫Yes.

59
00:04:27,760 --> 00:04:28,570
‫Yes, it is.

60
00:04:28,570 --> 00:04:37,510
‫So we go in and we get actor zero, we get its name and we log it out and then we increase the value

61
00:04:37,510 --> 00:04:38,680
‫in index by one.

62
00:04:38,680 --> 00:04:40,840
‫So it goes from being zero to being one.

63
00:04:41,320 --> 00:04:47,320
‫Now we go back to evaluate the while loop condition and we say is one less than two?

64
00:04:47,320 --> 00:04:48,190
‫Yes it is.

65
00:04:48,190 --> 00:04:55,540
‫We can then go in and get actor one and then we're going to get its name and print its name.

66
00:04:55,540 --> 00:05:02,380
‫Then we increase the index again to index two and we ask the question again in the wild loop, saying

67
00:05:02,380 --> 00:05:03,760
‫is two less than.

68
00:05:04,240 --> 00:05:05,890
‫Well, that answer is no.

69
00:05:05,890 --> 00:05:12,070
‫So we skip over the body of the while loop and we go on and execute the rest of the code.

70
00:05:12,430 --> 00:05:15,120
‫So that's basically what we want to achieve.

71
00:05:15,130 --> 00:05:22,990
‫We want to print out the name of each actor in the array, and that's what this piece of code is going

72
00:05:22,990 --> 00:05:23,590
‫to do.

73
00:05:23,620 --> 00:05:26,370
‫Let's go ahead and compile it and check that I'm right.

74
00:05:26,380 --> 00:05:33,010
‫If we go ahead and hit play and step into the box, you can see that we were overlapping just the BP

75
00:05:33,010 --> 00:05:33,610
‫player.

76
00:05:33,730 --> 00:05:36,270
‫What if we go and get our gargoyle?

77
00:05:36,280 --> 00:05:41,070
‫If we stick only it into the bounce, you can see that we get overlapping the gargoyle.

78
00:05:41,080 --> 00:05:43,250
‫But what if I step in as well?

79
00:05:43,270 --> 00:05:49,600
‫Now you can see it's printing both that we're overlapping the player and the gargoyle all in the same

80
00:05:49,600 --> 00:05:50,080
‫frame.

81
00:05:50,080 --> 00:05:56,770
‫So our while loop is working, it's executing a piece of code multiple times when there are multiple

82
00:05:56,770 --> 00:05:58,520
‫actors in the array.

83
00:05:58,540 --> 00:06:04,570
‫Now, because this pattern is so common that we have some sort of index and we increase it every time

84
00:06:04,570 --> 00:06:09,340
‫we go through the loop, there's actually some shorthand for this in C++.

85
00:06:09,340 --> 00:06:18,310
‫And if you type four in your Visual Studio code, then there is a snippet for a four loop here that

86
00:06:18,310 --> 00:06:23,890
‫you can expand and you can see that this is a general structure.

87
00:06:23,890 --> 00:06:25,420
‫It's got something called a size T here.

88
00:06:25,420 --> 00:06:29,470
‫We can just replace that with INT 32 for our purposes.

89
00:06:29,620 --> 00:06:35,980
‫And essentially what this is saying is there are three statements inside the parentheses of the for

90
00:06:35,980 --> 00:06:36,390
‫loop.

91
00:06:36,400 --> 00:06:43,240
‫The first one is equivalent to where we set up our index variable before our while loop.

92
00:06:43,780 --> 00:06:49,120
‫The second one is the condition which is equivalent to what we put inside our wild loop.

93
00:06:49,510 --> 00:06:54,670
‫And you can see we could replace the count with actors dot num.

94
00:06:54,670 --> 00:07:01,240
‫And then the last bit is the basically the plus plus whether you put it before or after, the index

95
00:07:01,240 --> 00:07:07,240
‫is not too important, but you can see that this is increasing the value in the index.

96
00:07:07,240 --> 00:07:11,080
‫So it's equivalent to the line that we put at the end of our wild loop.

97
00:07:11,470 --> 00:07:17,980
‫So this just brings together all in one line what we were having to spread out across multiple using

98
00:07:17,980 --> 00:07:18,910
‫our wild loop.

99
00:07:18,910 --> 00:07:20,380
‫So here's a simple challenge for you.

100
00:07:20,380 --> 00:07:23,470
‫Convert your wild loop to a for loop.

101
00:07:23,470 --> 00:07:30,220
‫You're going to copy over the body of the code, change the index from that name index to I.

102
00:07:30,250 --> 00:07:36,550
‫We typically use the abbreviation in for loops of I just an abbreviation for index really and compile

103
00:07:36,550 --> 00:07:38,020
‫and test pause video.

104
00:07:38,020 --> 00:07:38,800
‫Now have a go.

105
00:07:41,860 --> 00:07:42,180
‫Okay.

106
00:07:42,190 --> 00:07:42,760
‫Welcome back.

107
00:07:42,760 --> 00:07:44,710
‫So this should be fairly straightforward.

108
00:07:44,710 --> 00:07:47,050
‫We don't want the index plus plus in here.

109
00:07:47,050 --> 00:07:53,530
‫So we're going to cut the rest out, channel change over where we are indexing the actors array with

110
00:07:53,530 --> 00:07:54,370
‫the index variable.

111
00:07:54,370 --> 00:07:58,450
‫We're going to change that to IE the variable that's available only within this for loop.

112
00:07:58,450 --> 00:08:04,750
‫That's another advantage by the way of the full loop syntax is that the IE variable is only available

113
00:08:04,990 --> 00:08:10,210
‫only available within the for loop, whereas when we declared it here in the Y loop, it's available

114
00:08:10,210 --> 00:08:16,990
‫outside, which kind of pollutes the indexes we can use later on so we can delete that wild loop.

115
00:08:16,990 --> 00:08:23,230
‫And you can see that the for loop is a lot more condensed and you will get used to the slightly strange

116
00:08:23,230 --> 00:08:25,390
‫syntax that it has.

117
00:08:25,390 --> 00:08:30,670
‫Let's go ahead and compile and just check that it does indeed do the same job.

118
00:08:30,670 --> 00:08:38,050
‫If I go and grab my gargoyle and come up to the overlapping actors, you can see that indeed is printing

119
00:08:38,050 --> 00:08:42,100
‫both of our actors when we're both overlapping in the alcove.

120
00:08:42,130 --> 00:08:42,850
‫Great stuff.

121
00:08:42,850 --> 00:08:49,840
‫So in this lecture we've seen a new programming idea, the idea of loops, and we've particularly come

122
00:08:49,840 --> 00:08:53,230
‫across the while loop and the for loop.

123
00:08:53,230 --> 00:08:54,910
‫I'll see you in the next lecture.

