‫Hey, welcome back.
‫In this video, we're concerned with winning and losing conditions.
‫This is one of the last things we're going to handle in this game project.
‫Now let's take a look at our section plan.
‫Now, we've already checked off the first five items on our section plan, and the last two are the
‫special effects and winning and losing conditions.
‫And I'd like to switch these last two around and handle winning and losing conditions just because this
‫is the last of our gameplay programming logic and the special effects are cosmetic and more like finishing
‫touches so we can handle those at the very end.
‫So in this video we're going to create a game over function so that we know when the game has been either
‫won or lost.
‫And in order to win the game, we're going to need to count the number of towers in the world, because
‫once we've destroyed all of the towers, then we know we have won the game.
‫And if the tank itself gets destroyed, then we can consider that to be the lose condition.
‫So I'm back here in the game mode and the first thing I'd like to do is create another blueprint implementable
‫event.
‫We have this start game function, which we can call from C++, but we're implementing its logic in
‫the game mode.
‫Blueprint Thanks to Blueprint Implementable Event, I'd like to create another one of these for losing
‫the game.
‫So I'm going to create a void function called Game Over.
‫And for this one, I'd like to take a boolean input parameter called B one game.
‫And of course this is going to be a blueprint implementable event because the only thing I'd like to
‫handle with this function is displaying that we've lost or won the game and that we can do from blueprints.
‫So we have a game over function.
‫Now we need to know when we should call this function.
‫Well, first of all, we know we can call this function when we've lost the game, and that's going
‫to be if the tank has died.
‫So let's go into our actor died function here in tune tanks game mode CP And we see that if dead actor
‫is the tank, then we're handling the destruction of the tank and we're setting the enabled state to
‫false.
‫Another thing we can do here is end the game by simply calling game over and this boolean be one game
‫we can pass in false for this.
‫So that takes care of losing the game, at least from the C++ side.
‫Now we need to know when we should win the game.
‫Now here an actor died.
‫If the dead actor can be cast to a tower, we know that we've just destroyed a tower.
‫Now, this would be the perfect place to update a tower count or a variable of integer type that keeps
‫track of how many towers in the world there currently are.
‫Now.
‫In order to do that, we need a variable to keep track of the number of towers so we can add a private
‫variable here in the H file.
‫And I'd like to do that down here.
‫So I'm going to create an INT 32 and I'm going to call this target towers.
‫Now we can initialize this right off the bat to a value of zero, but we need to update this as the
‫number of towers decreases.
‫And we also need to set this to the number of towers there are in the world.
‫Now we could hard code this to four, but what happens when we start adding more towers to our levels?
‫Well, then it gets a little bit more complicated.
‫So what I'd like to do instead is see how many towers in the world there are.
‫Now, I could create a function for this that returns 32 and this function can find out how many towers
‫there are in the world and return the number.
‫So I'm going to call this function, get a target tower count.
‫So let's go ahead and give this function a body in the CP file.
‫I'm going to go all the way down to the bottom and create the function body and I can't forget to put
‫the class name before the function name.
‫Now this is going to return an end 32 and we need to decide how we're going to find out how many towers
‫there are.
‫Now there's a function that exists in gameplay statics called Get All Actors of class.
‫Here's the documentation.
‫It says Find all actors in the world of the specified class.
‫So we need to specify a particular class.
‫Now this function has three inputs.
‫First is a world context object.
‫That one's easy.
‫We know how to handle that.
‫But next we have a PT subclass of a actor and this is called Actor Class.
‫Now we've seen that functions that require a PT sub class of can accept a u class as inputs and the
‫third parameter is a T array.
‫It requires a t array of actor pointers and this is passed in as a reference and it's not a const reference
‫which seems to suggest that this function just might change this input parameter called out act.
‫Here's another hint that this is the case, is that it's called out actors.
‫It's an out parameter, meaning we pass it in and then after the function has been called, it has values
‫that have been put into it by the function.
‫So we can think of it as a sort of output for the function.
‫Now it says down here for parameters, actor class is the class of actor to find must be specified or
‫result array will be empty.
‫We want to find all of the towers and out actors is an output array of actors of the specified class.
‫So once we've called this function and passed in an array, when the function has finished, that array
‫will contain pointers to all of the actors in the world of the specified class.
‫So we know that this function requires a world context object.
‫Now, if we're going to be calling this from within the game mode, we can pass in this next.
‫We need the PT sub class of specifying the A actor and we know that this can be a new class pointer.
‫Now we need to specify the class to find.
‫If we specify a actor, then we'll get a big array filled with pointers to each actor in the world.
‫But we don't need all of the actors.
‫We just need the towers so we can use a tower static class which will return us a new class that represents
‫the A Tower C++ class.
‫Now the third parameter is a T array of a actor pointers.
‫So if we create a local variable of type T array specifying the a actor pointer in the angle brackets,
‫then we can call you game play statics.
‫Get all actors of class passing in those three inputs, and the result of this action will be that our
‫towers array should contain pointers to all towers in our world.
‫Now the tray is a nice container class which can contain a number of elements as long as they all share
‫the same type.
‫And this class has quite a great deal of functions that we can use when storing items in a tray.
‫Now I'm going to scroll all the way down and show you this function here called Num Short four number
‫and it says Returns the number of elements in array.
‫So we can use this function to get the number of elements in our array once we have all of the towers
‫in the world.
‫So if we call Tower's num, then we can use this as the return value for our function.
‫And as we saw, we set our return value for this function which we call get target tower count as an
‫n 32, we can return the number of elements in our tower's array.
‫So I'd like to challenge you to implement this get target tower count function.
‫So create a local t array of type A actor.
‫You can call it towers or anything you like.
‫Now use the you game, play statics function, get all actors of class and make sure you pass in the
‫correct input parameters.
‫Now, after you call this function, your tray is going to contain pointers to all of the towers in
‫our world, so you can simply return the number of towers using that num function that belongs to the
‫tray class.
‫So go ahead and pause the video and implement.
‫Get target tower count now.
‫Okay.
‫So here's get target tower count.
‫Now, the first thing I'm going to do is create a local t array specifying a actor pointer as the type
‫of element that this array should store.
‫And I'm going to call this towers now I need to call you gameplay statics, so I need to make sure that
‫that header is included.
‫Now I see that it is since we're using it in some of our other functions, but it's good to make that
‫check.
‫Next I can use you gameplay statics and I'm going to call get all actors of class.
‫Now the first input is a world context object I'm going to pass in this and next is the sub class of
‫representing the actor class.
‫And I mentioned that we can use a tower static class which will return a U class that represents the
‫tower class.
‫Now again, it's a good idea to make sure that we're including towers, which we are.
‫Now, the third and final parameter is going to be the tray and we can pass in towers.
‫So once this function has been called, towers will now contain pointers to all of the towers in the
‫world.
‫In fact, if there exist any objects of a class that derives from towers, we would also get those.
‫So since we have a towers array, we can simply return the number of elements in that array.
‫So I'm going to use a return statement and use towers dot num and now we have a get target tower count.
‫So this is great.
‫We can use this to initialize our tower count, which we called target towers and we can do that when
‫we start the game.
‫So I'm going to place that here and handle game start.
‫In fact, I'd like it to be the first thing we do.
‫So I'm going to take target towers and set it equal to the return value from get target tower count.
‫Now target towers should be initialized to the number of towers in our world.
‫So this is great, but we need to update this count every time a tower dies, which means we need to
‫go into our actor died function.
‫And here, if we've cast dead actor to a destroyed tower, then after we handle destruction, we should
‫update our target towers variable so we can say target towers and we can use the decrement operator.
‫We can use either the post decrement operator or the predicament operator.
‫Both of these will result in the same action which is taking target towers and subtracting one from
‫it.
‫Now, as soon as we've subtracted one from target towers, now we need to check to see if Target Towers
‫has reached zero.
‫So I'm going to place an if check here to see if Target towers has reached zero.
‫Now, as soon as it does, we know that the game should be over so we can call the game over function
‫passing in true 4b1 game since we've destroyed all of the towers and we can consider this a win.
‫Now let's go ahead and compile our code.
‫Now, back here in the editor, what I'd like to do before we wrap this video up is go into our game
‫mode blueprint.
‫So here's beep tune.
‫Thanks game mode and we have this event start game.
‫And what we're doing is creating our start game widget, adding it to the viewport so that we can see
‫that start game countdown timer.
‫So we know that start game is being called, but we also need to implement our game over blueprint implementable
‫event.
‫So I'm going to right click and type in game over and here's our event, game over and check this out.
‫Notice we have this one game and it's a boolean.
‫So when we call this function from C++, we're passing in a boolean value for B one game.
‫The blueprint event node strips out that lowercase B and places a space between one and game as sort
‫of a courtesy for us.
‫Now we're going to create some widgets so that we can see when we've won or lost the game, but we'll
‫do that in a future video.
‫For now, I just want to confirm that this is being called and called correctly so I can do that with
‫a print string node and we can print the value based on this boolean.
‫If I click and drag this boolean straight into the in string input, we'll see that there's a convert
‫boolean to string.
‫This is another convenient conversion node that will convert a boolean into a string for us, and its
‫functionality is such that it will convert to the word true or false based on the truth value of the
‫boolean.
‫So why don't we test this out?
‫I'm going to compile and hit play.
‫All right, now I'm ready to try to win the game.
‫Hopefully I don't lose before I can do so.
‫And I've lost the game and you can see false printed up at the top left of the screen.
‫So we now know that losing the game results in event game over being called with false being passed
‫in, we need to make sure that true is passed in when we win the game.
‫So let's try to kill all of the turrets.
‫So I need to really try not to get a hit.
‫Oh, I got hit already.
‫This is pretty challenging.
‫And there we go.
‫And true is printed at the top left of the screen.
‫It only shows up for a few seconds, so you might have missed it.
‫Okay, great.
‫So now we have winning and losing conditions.
‫The next step is to actually show this in the form of a widget on the screen so that we're not relying
‫on the print string node.
‫So in summary, we now have winning and losing conditions, and there's just a few things we need to
‫do to polish this, such as creating widgets that inform the player that they've won or lost the game.
‫We also counted the number of towers using a tray and the gameplay statics function.
‫Get all actors of class.
‫Great job.
‫This game is really coming along.
‫In the next video we'll create those widgets and then we're ready to start adding special effects and
‫polishing up our game.
‫I'll see you in the next video.