‫Hello and welcome.
‫In this lecture, we're going to be introducing this piece of code here, the RT actor range, and see
‫how it allows us to iterate over all of the actors in a given level.
‫Let's dive in and see how it works.
‫So at the moment in the pawn killed of our kill all game mode we are calling game has ended on the player
‫controller but in actual fact game has ended is a function on all controllers where there A.I or player
‫controllers and we should probably be notifying the AI controllers as well that the game has ended and
‫that's certainly going to be the case or it's going to make it easier to do a win condition as well
‫because we won't necessarily have the player pawn coming in here.
‫So how do we go ahead and do this?
‫Well, I'm going to create a new function in the KML game mode, a private one called End Game, which
‫is going to be responsible for this act.
‫So End Game and it's going to need some information.
‫It's going to need to know who has won.
‫Has it been the player or has it been the A.I.?
‫So I'm going to create a bool here called B is player winner.
‫And that is going to be what tells us whether or not we should be telling the AI that it's won or telling
‫the player that it's won.
‫Because remember in here our game has ended.
‫Takes a boolean telling the controller whether or not it is one of the winners.
‫So what we're going to have to do is figure out, first of all, let's go ahead and create the implementation.
‫So I'm going to do my control shift P and then create implementation.
‫And then we're going to have to figure out how we can find all of the controllers in our scene so that
‫we can call game has ended on those controllers.
‫Now the way we're going to do this is using a an engine helper call.
‫So if we go to hash include, we're going to use engine utils, first of all.
‫And that's going to bring into scope the helper that I want and the helper is called T Actor Range and
‫T actor range is a template thing.
‫So we can do the angle brackets here.
‫We can then put a controller and then the parentheses and get world.
‫Now what's this going to do?
‫It's going to return us a range object, which is basically sort of like a list that goes over all of
‫the controllers.
‫That's why we've got that controller parameter in there in the world a.k.a the level, and we can use
‫this in a for loop.
‫So let me start wrapping this in a for loop.
‫So we're going to do a for over the A controller.
‫So the thing that is the variable that we're going to be ranging over is going to be an A controller
‫pointer, which I'm going to call controller.
‫And then we use the colon for what we're going to range over.
‫It's going to be this T active range.
‫And then we can put our closing parentheses and our curly brackets.
‫So what's this saying?
‫It's saying that we're going to do a four loop over every controller in the world and it's going to
‫be in this variable called controller.
‫And now there's a couple of things that you might need to know.
‫So one thing is that we can query a controller for whether or not it's a player control without having
‫to cast.
‫We can actually just do controller arrow and then I think I'm going to actually need to include my controller
‫here.
‫So hash include at the top, we're going to do a game framework forward slash controller H and now Autocomplete
‫will probably work.
‫Yes, it has.
‫So what we can do is obviously we can call game has ended.
‫That's what my first autocomplete here but also we can say is player controller.
‫Basically it's going to say is this thing in the world of player controller and that will allow us to
‫decide whether or not we should be passing in true or false to game has ended.
‫So my challenge to you is to fill out this for loop by calling game has ended with the correct boolean
‫parameter.
‫So we're going to do this so that basically it should be true if the if the controller that we're talking
‫to the controller currently in the for loop is a player controller and the boolean that we had passed
‫in says that the player controller is the winner should also be true.
‫If it is not a player controller and not the player, the player is the winner basically saying if this
‫is an ally and the I is the winner false if the player controller but the player isn't the winner and
‫false if not the player controller, but the player is the winner.
‫You kind of see where this is going, hopefully.
‫So pause the video and have a go at implementing it like this.
‫You might use if statements.
‫You might use some other logic to allow you to calculate this boolean pause video and have a go.
‫Okay.
‫Welcome back.
‫So you could do this a number of ways.
‫One way might be to say if the bee is player, control a winner and then we're going to put something
‫in this if statement we might do a bool up here to decide that basically B is player controller.
‫Which we're going to save from the fact that we do a controller.
‫Arrow the is player controller like so.
‫And then we might say, okay, well we want to call controller.
‫Arrow game has ended.
‫We're going to pass in the null pointer for now.
‫And then we're going to say, well, if it is a player that is the winner and we are a player controller,
‫then yes, basically go with that value.
‫So play a controller.
‫Otherwise, if it's not the player controller, it's going to have lost.
‫And we can just negate this so we can say else there we're going to do call the controller game has
‫ended with not is player controller basically because we're saying the player is not the winner, the
‫eye is the winner in this statement.
‫And so what we want to do is say that the depends on whether basically the opposite of whether the player
‫has won, which is to say that the eye has won.
‫So that's one way of doing it for sure.
‫But there is another and simpler way, which is simply to say, are we the winner?
‫So be is winner.
‫And this can simply be to say that whether you are the player controller is equal to be is player winner.
‫Now let's think about this a little bit more and I'll just complete this fully and then we'll talk about
‫it.
‫So we're going to say controller game has ended is going to be the null pointer with the be is winner
‫passed in here.
‫Now, why does this statement work?
‫Well, let's think about the cases, right?
‫So we have a player controller in here and the player is the winner, then?
‫Yes, we are the winner and they are equal.
‫True and true.
‫How about if this is not the player controller and the player is not the winner?
‫Well, then this is false.
‫And this over here is also false so that the two of them are equal and the result is a winner.
‫Is that true?
‫Yes, because if we are the AI and the AI is the winner, then we should be the winner.
‫Otherwise, if these two are not equal, i.e. that we've got an AI controller, but the player is the
‫winner, or we've got the player controller and the AI is the winner, then we're going to have a true
‫and false or a false and true.
‫These two aren't going to be equal and the boolean is going to evaluate to false.
‫So you can see that in each of the cases and I've gone through all of the cases there, this ends up
‫with the right answer.
‫So we know that this is the right answer.
‫This is the great thing about Booleans is that usually you can evaluate all of the cases because you
‫can say, okay, well, what are the cases?
‫It's going to be true and true.
‫It's going to be true and false.
‫It's going to be false and true.
‫It's going to be false and false.
‫You can evaluate all those cases.
‫If all of those cases are correct, then you know you've got a correct Boolean statement.
‫Cool.
‫So that's how we're going to calculate the B is winner.
‫Finally, we just need to call our end game mode.
‫At the moment we're doing a direct call to the play controller game has ended and what I actually want
‫to do here is go ahead and call end game and we want to pass in false because this is a situation where
‫the player has actually lost and that's basically what we want to achieve.
‫Now, one more thing to note here is that this null pointer we've been passing in is going to lead us
‫to problems down the road, because if the player has actually won, it's going to try and take away
‫the focus from our player pawn.
‫So what we want to pass in here is simply that the focus should be on the controller, dot get pawn.
‫And that's basically saying that you should put the focus of your own player controller, be it basically
‫where should your camera be pointing?
‫It should still be pointing on your pawn.
‫Why is it important that we do this and why is there this option here in the first place?
‫Well, in a lot of games, you often or a lot of match style games, you might have that if the game
‫has ended, you want to go and follow the pawn.
‫That was the winner.
‫So imagine like a Fortnite scenario, you often go and follow the person who killed you.
‫This is the mechanism for doing this.
‫You basically pass in the pawn of the person who did the killing.
‫That's not what we're doing in our game.
‫We're going to carry on looking at Apple and whether we'd win or we die.
‫So that's pretty much it for the lecture.
‫Let's just compile and see whether it's working.
‫Let's dive on over into Unreal and hit play.
‫Let's go and get ourselves killed.
‫There you go.
‫Everything's working as before.
‫We're still getting that call to the player controller and then we restart.
‫Great stuff.
‫So in this lecture, just to recap, what have we seen?
‫Well, we've seen how we can iterate over actors in the level in order to do something like calling
‫a function on each of those actors.
‫Next lecture we will be using the T actor range again, this time to calculate whether the player has
‫won.
‫See you there.