‫Okay.
‫So our goal for this video is to implement death for our Pons.
‫So if I get hit a couple of times from our tower, you'll see that I have disappeared because my tank
‫has died.
‫And if I'm skilled enough, if I can go ahead and destroy one of them, you'll see that it disappears
‫as well.
‫So we're going to implement death for these pawns in this video.
‫Hello and welcome back.
‫Now we're already applying damage to the pawns when they're being hit by projectiles.
‫We now need to figure out how we're going to handle after death.
‫This means we're going to need some sort of function to handle the destruction of a particular actor.
‫So we're going to create a handle destruction function.
‫We also need an actor die to function, which can allow the game mode to handle what happens when an
‫actor in our world dies and we're going to call this actor died function when the health value reaches
‫zero for a particular pawn.
‫So let's talk about a bit of a roadmap for how we're going to implement this system.
‫We know that we have a projectile and it's calling apply damage and this results in the damage taking
‫function responding to that delegate broadcast in the health component.
‫So we need to figure out what to do from there.
‫Well, our game mode can have a function to handle when an actor dies.
‫So in damage taken, we can check to see if the health reaches zero.
‫And at that point we can call the actor died function on the game mode and from there our pawn can handle
‫its destruction.
‫So ultimately it'll be up to the game mode to call the handle destruction function on the actor that
‫died.
‫So we have a bit of a game plan, so let's outline the steps to take to implement this.
‫First, we're going to create the handle destruction function and we're going to place this on the base
‫pawn class to handle functionality that should happen when any of our pawns die.
‫And we'll also implement this in the tank and the tower classes, since they'll each have their own
‫unique functionality when they die, then we'll create an actor died function and place this on the
‫game mode class so that we can call handle destruction from there.
‫After that we'll call handle destruction in the actor died function on the game mode and then we can
‫call actor died from the health component when the health reaches zero.
‫So let's start with our first step creating the handle destruction function.
‫So I'm here in Base Pond H and I'd like to add this handle destruction function.
‫Now I'm planning on calling this from the game mode, which means it's going to need to be a public
‫function so I can place this function here in the public section.
‫It's going to be void and I'm going to call it handle destruction.
‫Now, it doesn't need to take any input parameters.
‫It's simply going to handle the destruction of this particular pawn.
‫So let's implement this function in the base pawn CP file and I'm going to place it right here under
‫the constructor.
‫So I have handle destruction and I'm going to place the a base pawn class name just before the function
‫name here.
‫Now this is going to be things that happen when any of the pawns based on base pawn dies.
‫So I'm going to place a to do here that says what we're going to do in the handle destruction function.
‫I'd like to handle all the visual and sound effects here that have to do with the pawn dying.
‫So I'm just going to say visual slash sound effects and we'll leave it at that and we'll make sure to
‫remember to implement this when we get to the special effects and sounds part of this section of the
‫course.
‫Now we have handle destruction here, but I'd like to also put a handle destruction function in both
‫the tank and tower classes to handle what happens when those pawns die.
‫And the most simple implementation of destruction is perhaps going to be for our tower.
‫So let's go into Tower H and we'll add a public function here called handle destruction.
‫So I'm going to say void handle destruction and I'm going to implement this and tower CP just under
‫the tick function.
‫So here's tick here's handle destruction and I'm going to prefix the function name with a tower just
‫like that.
‫Now in the handle destruction function for the tower, I'd like to call the super version so that handle
‫destruction in base pawn gets called and we'll get those visual and sound effects when a tower dies.
‫So I'm going to say super handle destruction just to make sure that that gets called.
‫And after that, I'm simply going to destroy the tower.
‫Now we've seen how this works.
‫We simply call the destroy function, and that's going to handle destruction for the tower.
‫So I can save this and close out of the tower and CP files and now I can place a handle destruction
‫function in the tank class.
‫So I'm going to go to tank and declare a public function here under the tick function called handle
‫destruction.
‫And I'm going to go to Tank CP and implement this just under tic here, not forgetting to prefix the
‫function name with a tank.
‫Now, just like in the tower class, we'd like to call super handle destruction.
‫And that way we'll get those visual and sound effects.
‫Now for the tank, we're going to do something a little different.
‫I'd like to hide the tank rather than destroy it.
‫And then we can disable its TIC functionality so it'll still exist in the world and we'll still be possessing
‫it.
‫So we still get that view from the camera.
‫Only we won't be able to see it or move it around.
‫So to hide the tank we can call a function called set actor hidden in game and this will take a boolean.
‫And if we pass in true then we will effectively hide the tank.
‫Now I'd also like to disable ticking and there's a function for that as well.
‫It's called set actor tic enabled.
‫And for this I'm going to pass in false, which will disable ticking for the tank.
‫So that takes care of our handle destruction function.
‫So we have our handle destruction function.
‫Our next step is to create an actor died function in the game mode.
‫So let's implement that.
‫So I'm here in tune tanks game mode and here I can create the actor died function now I plan on calling
‫actor died from the health component class, which means this also needs to be a public function.
‫So I'm going to add a public section and create the actor died function.
‫Now this will be void and I'm going to call it actor died.
‫And this can simply take an a actor pointer called Dead Actor and now we can implement actor died in
‫tune tanks game mode.
‫CP So I'm going to place it here and prefix the name with a tune tanks game mode and now we have an
‫actor died function.
‫Now I'd like to check to see if the dead actor is the tank, which means I'm going to need a variable
‫for the tank.
‫So back in tune tanks game mode.
‫I'm going to make a private section and I'm going to add a tank variable which I'm going to forward
‫declare.
‫And it's of type A tank pointer and I'm simply going to call this tank and I'm going to need to initialize
‫this with the address of the tank pond, and I can do that and begin play.
‫Now we've seen that begin play is a protected function and we're going to keep it protected here in
‫the game mode so we can override, begin, play.
‫So I'm going to say virtual void, begin, play, override, and I can implement it here in tune tanks,
‫game mode.
‫Yep.
‫So it's going to be void begin play and I'm going to use the tune tanks, game mode class name.
‫And the first thing I'm going to do is call super begin play.
‫That way we get parent functionality and I can get the player pawn using gameplay statics.
‫So I'm going to first include gameplay statics and that's in kismet slash gameplay statics as we've
‫already seen.
‫And here I can take Tank and assign it the value of youth gameplay statics.
‫Guitar player palm.
‫Now, this takes a world context object which we can use this for, and I'm going to use zero as the
‫player index since our tank will always be player zero.
‫Now we know that jet player -- returns an A -- pointer and tank is of type A tank, a child class.
‫And we've seen that we need to cast to that a tank class if we'd like to store this result in an A tank
‫pointer.
‫So first I'm going to include the tank header.
‫So I'm going to say include tank H and then I'm going to cast to a tank.
‫So I'm going to say cast to a tank and I'm casting the value returned from get player --.
‫So I'm going to place that in the parentheses here.
‫And now we'll have a pointer to the player -- in the form of an A tank pointer.
‫So now that we have the tank, we can check to see if our dead actor is the tank.
‫So we can say if dead actor double equals tank now tank may be null.
‫And that's okay because if Tank is null and dead actor refers to the tank itself, then this will fail
‫the if check and we won't actually attempt to access a null pointer because we won't make it to the
‫inside of this.
‫Now, if the dead actor is in fact the tank, then I'd like to call the tank handle destruction function.
‫So we're going to call tank handle destruction.
‫Now, after I call the handle destruction function, I'd also like to disable input for the tank pod
‫so I can get the tank and call the built in function that tank inherits called disable input.
‫Now disable input requires an A player controller.
‫Now we know that the tank class, if we come here into tank H has this player controller ref variable,
‫which is a pointer to a player controller, which is why I'm starting to think that ref is a very awful
‫name for this variable.
‫Since it's not a reference, it's a pointer.
‫I'd almost rather call it tank player controller.
‫So in fact, why don't we go ahead and rename this?
‫This is something that developers often do when they realize that they've given a variable, a bad name.
‫So this code has a way to do this easily.
‫I can highlight this variable and hit the F two key and it's asking for me to rename it.
‫I'm going to call this tank player controller.
‫Now check this out.
‫It's called Tank Player Controller here.
‫If I go to Tank CP, I'll see that it's renamed here as well.
‫So I didn't have to go in and look for it and find every instance of player, controller, ref.
‫I can search for tank player controller and see that it's changed everywhere that it's used here.
‫So that's a nice refactoring skill to have.
‫So anyway, back here in tune tanks game mode, I need to disable the input for the tank and this requires
‫a player controller.
‫So I'm first going to check to make sure the tank player controller is valid by saying if tank and I
‫need to access that player controller.
‫But recall that in tank, our player controller variable is private.
‫Now we could expose this and make it public, or we could make a public getter function for this variable.
‫I'd rather make a public getter as that's better programming practice.
‫So up here in the public section, I'm going to create a public getter for tank player controller.
‫Now it's going to return an A player controller pointer and this function is going to be called get
‫tank player.
‫Controller getters are typically the name of the variable with get in the front, and they're also typically
‫made cost as this function is not designed to change anything in this class, it's only going to return
‫the player controller.
‫So we're going to say return tank player controller and that's all it's going to do.
‫So now that we have a public getter function for the tank player controller, we can access the player
‫controller here from within the game mode.
‫So we're going to say if tank get a tank player controller and make that function call here.
‫And if we reach the inside of this if statement, then the player controller is valid so we can move
‫this call to tank disable input right here and we're going to pass into disabled input the tank player
‫controller.
‫So we'll call tank get player controller here as well.
‫Now, while we're accessing the player controller, I'd also like to take the player controllers be
‫show mouse cursor Boolean member variable and set this to false.
‫So we're disabling input and we're also making sure that the mouse cursor is not shown.
‫So that takes care of handling the functionality for when the tank dies.
‫Now, in the case that the dead actor is not the tank, I then like to check to see if it's an instance
‫of the tower class.
‫So I'm going to add an else if here.
‫So what's going to be the conditional for this elseif here?
‫Well, I'd like to see if dead actor can be cast to an A tower, which means I'm going to need to include
‫the tower class.
‫So include tower.
‫And then I'm going to attempt to cast.
‫So I'm going to say cast, which takes angle brackets and parentheses.
‫And what we're casting too is a tower and what we're casting is dead.
‫Actor will attempt to cast dead actor.
‫Now we can actually take the result of this cast and assign it and create a local variable.
‫Here in the conditional we can say a tower pointer and we can call this destroyed tower and assign it
‫the value returned from this cast.
‫And if we do that, then we have access to this destroyed actor, which is our dead actor cast to a
‫tower.
‫Now, if this is successful, then we'll make it to the inside of the elseif.
‫And then we can take our destroyed.
‫Tower and we can simply call handle destruction.
‫So now we have an actor died function here in the game mode which calls handle destruction and also
‫handles some more death related functionality for the tank.
‫So that takes care of step two.
‫Creating an actor died function and while we were at it we also called handle destruction in the actor
‫died function.
‫So our last step is to call actor died when health reaches zero.
‫Now in order to call this function that exists on the game mode, our health component is going to need
‫a variable that stores the address of the game mode.
‫So let's go into our health component class.
‫Here's health components, and I'm going to add a private variable here just at the bottom.
‫And this will be of type A tune tanks game mode pointer and we'll call this tune tanks game mode.
‫Now I'm going to forward declare this so I don't have to include its header file here and then we need
‫to access the game mode.
‫So let's go to health component CP and if I scroll up, I see the begin play function here and that
‫is the perfect place to get a hold of the game mode.
‫Again, putting it in the constructor is to early.
‫We need to make sure that the game mode has been constructed and exists before we try to access it.
‫Now here is a handy function on game play statics called Get Game Mode and it says returns the current
‫game mode base or null if it can't be retrieved such as on the client.
‫Now on the client has to do with multiplayer programming and we don't have to worry about that.
‫We are running on the one and only machine for our game, so let's scroll down and we'll see that a
‫game mode base takes a world context object.
‫This is just an object in the world and we've seen how to handle world context objects.
‫So this exists in gameplay static h so let's get that include.
‫And here in health components, CP will paste our include for gameplay statics and then we'll call get
‫game mode here and begin play.
‫So we're going to say tune tanks, game mode.
‫And set that to the value of you.
‫Gameplay statics get game mode and for the world context object.
‫I'm going to pass in this and now we have the game mode.
‫But of course get game mode, returns a game mode base and we're attempting to store it in the child
‫class, a tune tanks game mode, which means we're going to need to cast.
‫That's right.
‫So we need to say cast to a tune tanks game mode and we're casting the result from get game mode.
‫So I'm going to place that in the parentheses there.
‫And we're using tune tanks game mode.
‫So I'm going to include that header file at the top.
‫So I'm going to say include.
‫Tune Tanks game mode.
‫So we have the game mode here in the health component.
‫We're ready to go down here into damage taken and check to see if our health reaches zero.
‫And in that case we can use our tune tanks game mode pointer to call the function actor died passing
‫in the actor that died and this is going to be your challenge so in the health component class in damage
‫taken you're going to check to see if the value of health has reached zero or gone below zero.
‫If that's the case, then call actor died using the game mode.
‫Since we have a pointer to the game mode, don't forget to check to see if this pointer is not null
‫first.
‫And once you call actor died, you're going to need to pass in an actor.
‫You're going to pass in the actor that died.
‫So pause the video and implement this in the damage taken function now.
‫Okay.
‫So we've already subtracted damage from health.
‫We're now ready to check to see if that health value has reached zero.
‫So I'm going to say if health is less than or equal to zero F and if that's the case, then it's appropriate
‫to call.
‫Actor died now because we need to check to see if the game mode is valid.
‫I can place an ad here in this if check and check to see if tune tanks game mode is valid.
‫If it's null, then it will return false here and we won't reach the inside of the if statement.
‫So if we reach the inside of the if statement, tune tanks game mode is valid and health has reached
‫zero.
‫So we can say tune tanks, game mode.
‫And we can call actor died.
‫And this takes an actor and we know that the actor that died is going to be the damaged actor.
‫So we're going to pass in damaged actor.
‫And now we can compile this and look at that.
‫We have a successful compile.
‫So let's hop back into the editor.
‫And here in the editor, why don't we go ahead and test out death?
‫Let's see if I can destroy some of these tanks before becoming destroyed myself.
‫That's going to be kind of a challenge.
‫Oh, there's one.
‫I killed one of them and I got the second one.
‫Oh, and I died.
‫So you can see that this game is a lot more fun now that we've added death and destruction to our ponds.
‫So this is great.
‫Okay, so what I want you to do is give yourself a huge pat on the back, because in this video we handled
‫Actor Death, which involves quite a bit of interaction between these classes in our game.
‫Now the way death is implemented is that the pond simply disappears, which means we still need a few
‫things.
‫We need win lose conditions.
‫We need a way to show the player that they've won or lost the game.
‫And we also need those special effects.
‫We need some sounds, we need some explosions.
‫Those things are going to come very, very soon.
‫So congratulations.
‫You've already come a long way and there are only a few steps left to go.
‫I'll see you in the next video.