‫Hello and welcome.
‫In this lecture, we are going to be making the win condition work so I can kill all of the enemies
‫in my level.
‫And it will show me this windscreen.
‫Hurrah!
‫Let's dive in and see how it's done.
‫So we'd like it to be possible for our player to actually win this game.
‫At the moment they cannot.
‫So how are we going to do that?
‫Well, you could do it in a number of ways.
‫One of them would be when Pawn Killed is called and the pawn is not the player controller but an AI.
‫We could subtract from a variable which says how many AI there are in the world, how many we have to
‫kill.
‫And then when that goes down to zero, we can end the game in favor of the player.
‫However, I think that's a little bit fragile because it requires us to put how many enemies there are
‫in the world in two places.
‫Essentially, it needs to be in the game mode, but also it needs to be in the world when you're placing
‫the enemies down and those two can get out of sync, and that's going to cause some problems.
‫So the way I would prefer to do this is to simply say, let's count all of the enemies in the world
‫every time a pawn is killed, let's do a recount, a head count of all of the AI and see which ones
‫are still alive.
‫If any of them are still alive, then we're not going to end the game yet.
‫Basically, the game's not over, but if they are all dead and the play controller is not, then the
‫player has won.
‫So in order for us to do this, we need to have a way to query a player controller as to whether it's
‫dead or not.
‫Sorry, I mean, the AI controller.
‫So let's look for our shooter.
‫AI controller.
‫And what we want to do here is create a new public method that allows us to query whether it is dead
‫or not.
‫So we're going to do a ball is basically is dead and it's going to be a contest because it shouldn't
‫change what's actually happening in the controller when it's called.
‫And we can go ahead and create an implementation.
‫Now we should be controlling a -- that can tell us whether or not it is dead.
‫If we go and have a look at that again, we have a look at the shooter character, which is a --.
‫Remember, we have already this is dead cannot function there.
‫So essentially what we want to do is just pass on the request to the shooter I control now.
‫Yes.
‫One thing is you could say in the KML game mode, you could get hold of the controller, get hold of
‫the -- that the that that's controlling, do a cast and then check whether that -- is dead.
‫However, that's generally not best practice because it means that if suddenly something changes about
‫how we calculate death and or the AI controller is going to control different types of --, suddenly
‫we need to go and change the game mode, which doesn't make sense.
‫So it's better to do this and do the passing on inside the AI control is more robust.
‫It stops us from fiddling around inside the internals of somebody else's class, which generally means
‫that we can restrict when things change, we can restrict how many classes we have to go and touch and
‫update and change, which is always a good thing.
‫So what we're going to want to do is get the -- and cast the -- to make sure it is a shooter character.
‫So let's do a cast.
‫It's going to be to a shooter.
‫Character.
‫And I think we're going to need to do a hash include at the top of our eye control to make sure we know
‫about the shooter character like so.
‫And then we're going to do the cast doing a get -- and we're going to want to store this in a variable
‫so it's going to be a shooter.
‫Character pointer called let's call it controlled.
‫Character.
‫I can't just call it character because that's already a variable on the AI controller and put a semicolon
‫at the end of the line and we're going to check whether this is null or not.
‫So control character is not equal to null pointer, then we should be able to query it for its status
‫as to whether it's dead or not.
‫So we should be able to return that the controlled character.
‫Is dead.
‫Basically, we forward on the request to control character if that control character is a shooter character.
‫However, notice here we don't have a return statement.
‫In the other case, we do need to have a return statement that always needs to be a return if the return
‫value isn't void.
‫So we need to put a return statement out here outside of the if statement to catch the case where we
‫haven't already returned inside the if statement.
‫And here we're just going to say, is this character dead?
‫Well, yes, essentially, if it isn't so true, basically it is dead.
‫If we don't have a pawn, if we're not controlling a pawn, then essentially this controller is as good
‫as dead to us.
‫And that could happen because remember that the pawn detaches from its AI controller when it dies.
‫So we're going to go back over in to the game mode and what are we going to want to do here?
‫Essentially what we want to do is a for loop over all of the I the shooter A.I..
‫In world.
‫And with that for loop.
‫What do we want to do?
‫We want to see is dead.
‫Basically and or basically is not dead.
‫And if any of them are not dead, then we instantly know we don't have to check all of the eye at all.
‫If even just one A.I. remains alive, then the game is not over.
‫So we can do what's known as an early return, which you've probably seen before, which is going to
‫just exit from this function without executing any more of the code in it.
‫So everything after this early return will stop executing the for loop, will stop executing anything
‫after it will stop executing.
‫But then we can say if we manage to successfully get through this full loop without it early returning,
‫meaning that there are no living A.I. in the world, then we need to go ahead and end the game.
‫Simple as that.
‫Now, one part of the puzzle that we need to look at here is the end game.
‫Cause our game has ended on the player controller.
‫Let's have a look at how the shooter player controller currently deals with this.
‫If we go over to its CP, what we currently see is that it's currently totally ignoring the B is winner
‫is currently ignoring whether or not it is the winner.
‫So what we need to do is say if B is winner.
‫Then we're going to do something otherwise else.
‫We are going to display our lose screen.
‫So cut these lines here that are responsible for displaying the lose screen.
‫Put that in the alt statement and we'll restart in both cases anyway.
‫So currently the B is winner.
‫What are we going to do?
‫Well, we're just going to go ahead and probably create a wind screen as well, which is going to be
‫fairly straightforward.
‫We go over to our header file for the shooter player controller.
‫We can duplicate our lose screen class and make a win screen class in here.
‫We can also go ahead.
‫And in fact, this is a mini challenge for you.
‫Maybe go ahead and set it up so that it can do a windscreen.
‫Okay.
‫Welcome back.
‫So hopefully you've given that a shot.
‫We're going to just go in and put windscreen here instead of all of the loses and copy essentially everything
‫else.
‫Like so.
‫We're going to go ahead and compile this, then head on over into the editor and find ourselves the
‫player controller class.
‫And we should now see there is an empty windscreen class and what we're going to do is go over into
‫the UI and I'm going to duplicate the lose screen to call the windscreen, and you could create yours
‫from scratch if you wanted to.
‫But I'm just going to be using it very similarly, except I'm going to change out the colour for green.
‫And make it into a you win instead of a you lose.
‫Because the restart logic is effectively just going to be the same.
‫Some reason it reset my color there.
‫I'm just going to go and set the color to green again.
‫Okay, so that's great.
‫But we still have this lump of pseudo code over in the KML game mode that has not been filled out.
‫And that's because it is your challenge to calculate the win.
‫Fill out the pseudocode.
‫Use the T to range from below in that class to help you out.
‫Use the new is dead function that we created on the controller and test it out.
‫See if your windscreen shows up when you kill all the AI in the world.
‫So just going to leave it with the pseudo code on your screen for you to have a look at.
‫K Welcome back.
‫So let's start with the for loop.
‫So we're going to do a four.
‫What do we for looping over?
‫It's not a controller at this time.
‫It's going to be the shooter AI controller.
‫So we need to hash include that.
‫It's going to be the shooter.
‫I controller h.
‫And that means that we're going to be looping over a shooter, A.I. controller, star.
‫Just call the controller for shorts and then we're going to do a t actor range over again that a shooter
‫A.I. controller.
‫We need to get these two types to match then get world.
‫So we're doing it in this level.
‫And then we've got the body of the for loop here where we're going to be asking if it's not dead.
‫So if not, controller arrow is dead.
‫Basically we're saying make sure that we aren't dead.
‫And if it's not dead, then we just go ahead and return to this early return because we know that the
‫game can't be over just yet.
‫And otherwise, if we got through the hole for loop without returning, we do need to end the game because
‫the player must have one killed all of the AI in the level.
‫So in this case we could do an end game.
‫True.
‫Because the player has one.
‫And that should notify all of the player controllers and controllers around.
‫Cool.
‫So let's go over and compile that.
‫First of all, then I'm going to go over into Unreal and have a play.
‫Some reason my editor thinks that I'm in this edit specified color operation, so I'm just going to
‫close it down and restart it.
‫Now that it's rebooted, I'm going to go ahead and hit play and I'm going to get killed.
‫First of all.
‫Yes, I still lose.
‫That's great.
‫Now let's see if I can kill this guy before he kills me.
‫Let's go.
‫Boom, boom, boom, boom, boom, boom, boom.
‫I lose too quickly.
‫Let's go and find the gun again and set.
‫We have that damage from the gun really high for testing purposes.
‫I'm going to go and set the damage down back to maybe 20 this time.
‫So it's still fast, but not as fast.
‫Go and play and hit this guy a few times.
‫And what we saw there was that actually I did win because the level restarted.
‫So if I go and kill this guy again, you see nothing's happening.
‫It's saying in the message log that create which it was called with a null class.
‫And the reason for that, if we go over to our BP shooter player controller again, you can see we haven't
‫hooked up our windscreen.
‫One thing that I forgot to do, so you can go and compile, save and play.
‫And now hopefully if I kill this guy to New Doom, I win and I'm going to restart in 5 seconds.
‫And I want to show you that the same is true.
‫If I put more player controllers in the world, it's going to require me to kill both of them.
‫So let's go and bring in the shooter character over here.
‫And this is going to be my second enemy to kill.
‫So kill this guy, boom.
‫And it hasn't won.
‫Then I go over to the second guy, boom.
‫And now I have one because I've killed all of the AI and we didn't have to go and change any variables
‫to do that because our for loop counts all of the living A.I. every time from scratch.
‫So currently we're now living in quite a silent world.
‫So in the next lecture, I want to start looking a little bit at how we can do some audio.
‫I'll see you there.