‫Hello and welcome this lecture.
‫We're going to be learning how we can execute a piece of code variable numbers of times so that we can
‫print out every object that overlaps inside this area, this trigger component.
‫So you can see here that when we're both, I'm standing inside and there's a gargoyle, too.
‫It's printing out both the gargoyles name and the player blueprint name.
‫Let's dive in and see how this is done.
‫Now, the issue with our code here is that our actors array may have far more than just one actor in
‫it.
‫And if we wanted to display all of the actors overlapping or we want to do something with all of those
‫actors, we need to be able to run through the code.
‫It would be highly inefficient to write an if statement for the case where we've got two or three or
‫four or five, etc. And it certainly would never catch every case because we have got a limited amount
‫of lines in our file.
‫So the way we deal with this, when we want to repeat a piece of code multiple times, but a number
‫of times that we don't actually know when we're compiling the code, we need to replace our if statement.
‫If statement essentially says, should I execute this code or not with something that says, should
‫I execute this code or should I execute it many times, two times, three times, four times, etc.?
‫The way we do this is with something called a wild statement.
‫Now it's built very much like an if statement.
‫So the while you then put some sort of boolean value inside the parentheses, and then we have some
‫curly braces.
‫And inside the curly braces, that block of code that will be keep getting executed again and again
‫until the condition in our while statement becomes false.
‫That's why it's called while.
‫So while this thing is true, executes this code.
‫So it's a bit like an if statement in that sometimes it might not execute the code at all, but unlike
‫an if statement that can only execute a piece of code once, the while statement could basically keep
‫on executing it forever if this boolean never becomes false.
‫So why is this useful when we could create an int 32 and call it index and start it off at zero?
‫And then every time we go through the body of this wild loop, we increase the value stored in that
‫index.
‫So we could say index equals index plus one.
‫Now in C++, there is a shorthand to doing this instead of having to do index plus one like this, you
‫do plus plus index, and that just adds one to whatever is stored in the index variable.
‫Okay.
‫So we start off at zero.
‫We every time we go around, we increase the index.
‫And what we can do is we can make it so that we only go around this while loop, while our index is
‫within the bounds of our array.
‫So we can say that the index must be less than the actors dot num.
‫So that is the size of the array.
‫Remember that when we've got five items in array, the last item in the array is index four.
‫And so we can see that this will be true when the index is valid, when index four is less than five.
‫But as soon as our index goes past the end of the array, when it gets to index five, then the actor
‫number is going to be not no longer.
‫It's five is no longer less than five.
‫So the while loop will stop executing and continue with the rest of the function.
‫So what that means is we could now take the code that we had above and rewrite it.
‫Let's take it, copy it, cut it out and stick it in here in our wild loop.
‫But before we increase the index, most importantly, because what we're going to do is replace this
‫zero where we were getting actor zero, we're going to replace it with actor index.
‫So let's follow this through.
‫How would this work?
‫Let's remove our if statement because that's confusing matters a little bit.
‫So we start off the value in the variable index is going to be zero.
‫Now imagine we have two actors to keep things simple.
‫Is zero less than two?
‫Yes.
‫Yes, it is.
‫So we go in and we get actor zero, we get its name and we log it out and then we increase the value
‫in index by one.
‫So it goes from being zero to being one.
‫Now we go back to evaluate the while loop condition and we say is one less than two?
‫Yes it is.
‫We can then go in and get actor one and then we're going to get its name and print its name.
‫Then we increase the index again to index two and we ask the question again in the wild loop, saying
‫is two less than.
‫Well, that answer is no.
‫So we skip over the body of the while loop and we go on and execute the rest of the code.
‫So that's basically what we want to achieve.
‫We want to print out the name of each actor in the array, and that's what this piece of code is going
‫to do.
‫Let's go ahead and compile it and check that I'm right.
‫If we go ahead and hit play and step into the box, you can see that we were overlapping just the BP
‫player.
‫What if we go and get our gargoyle?
‫If we stick only it into the bounce, you can see that we get overlapping the gargoyle.
‫But what if I step in as well?
‫Now you can see it's printing both that we're overlapping the player and the gargoyle all in the same
‫frame.
‫So our while loop is working, it's executing a piece of code multiple times when there are multiple
‫actors in the array.
‫Now, because this pattern is so common that we have some sort of index and we increase it every time
‫we go through the loop, there's actually some shorthand for this in C++.
‫And if you type four in your Visual Studio code, then there is a snippet for a four loop here that
‫you can expand and you can see that this is a general structure.
‫It's got something called a size T here.
‫We can just replace that with INT 32 for our purposes.
‫And essentially what this is saying is there are three statements inside the parentheses of the for
‫loop.
‫The first one is equivalent to where we set up our index variable before our while loop.
‫The second one is the condition which is equivalent to what we put inside our wild loop.
‫And you can see we could replace the count with actors dot num.
‫And then the last bit is the basically the plus plus whether you put it before or after, the index
‫is not too important, but you can see that this is increasing the value in the index.
‫So it's equivalent to the line that we put at the end of our wild loop.
‫So this just brings together all in one line what we were having to spread out across multiple using
‫our wild loop.
‫So here's a simple challenge for you.
‫Convert your wild loop to a for loop.
‫You're going to copy over the body of the code, change the index from that name index to I.
‫We typically use the abbreviation in for loops of I just an abbreviation for index really and compile
‫and test pause video.
‫Now have a go.
‫Okay.
‫Welcome back.
‫So this should be fairly straightforward.
‫We don't want the index plus plus in here.
‫So we're going to cut the rest out, channel change over where we are indexing the actors array with
‫the index variable.
‫We're going to change that to IE the variable that's available only within this for loop.
‫That's another advantage by the way of the full loop syntax is that the IE variable is only available
‫only available within the for loop, whereas when we declared it here in the Y loop, it's available
‫outside, which kind of pollutes the indexes we can use later on so we can delete that wild loop.
‫And you can see that the for loop is a lot more condensed and you will get used to the slightly strange
‫syntax that it has.
‫Let's go ahead and compile and just check that it does indeed do the same job.
‫If I go and grab my gargoyle and come up to the overlapping actors, you can see that indeed is printing
‫both of our actors when we're both overlapping in the alcove.
‫Great stuff.
‫So in this lecture we've seen a new programming idea, the idea of loops, and we've particularly come
‫across the while loop and the for loop.
‫I'll see you in the next lecture.