‫Hey.
‫Welcome back.
‫Now we have our projectile, and we know that it's generating hit events, so we need to do something
‫when the projectile actually hits our target.
‫Now, that's going to involve doing damage, which means we need a way to keep track of health.
‫So we're going to create a class for handling health and damage called a health component.
‫And the reason this functionality gets its own class is so that we can reuse it for different classes
‫such as the tank and the tower.
‫So let's discuss this health component.
‫Now we're familiar with the use scene component class.
‫Well, this class is derived from a class called you actor component.
‫In fact, here's the use scene component documentation.
‫And in the inheritance hierarchy, you can see that it's parent is you actor component.
‫Now the you act a component is more basic than the you seen component.
‫It has no transform.
‫So location, rotation and scale is not something that the you actor component even has.
‫It doesn't support attachment.
‫You can't attach an active component to, say, a static mesh component or a capsule component.
‫It's just something that belongs to the actor.
‫And because there's no visual representation or transform, we don't need to attach it to anything.
‫Compare this to the use scene component, which actually does have a transform and the ability to attach
‫itself to other components or have other components attach themselves to it.
‫So to keep track of health and take care of damage, we don't need any of those things.
‫So you act a component is going to be perfect for this.
‫So let's hop back here into the editor and make our new act a component class.
‫So we'll go up to tools and new C++ class.
‫And if I scroll down, I can see actor component.
‫This is what we're going to select.
‫Now be careful.
‫Don't select actor.
‫That's a different class.
‫It's actually a bigger class and has a lot more things than the actor component has.
‫We need actor component specifically, so select that, click next.
‫And this we can call simply Health Component.
‫Let's click Create Class.
‫Now here we have our new health components and CP files and you can see here that you health component,
‫the class we just created is derived from you act a component.
‫So make sure you have the correct type here and you'll see that it has in its U.
‫Class macro.
‫This meta specifies where and it says blueprints spawn table component.
‫This is what's going to allow us to add this component directly in blueprints, which is what we're
‫going to do now.
‫The new health component is going to take care of health, which means we need a couple of variables.
‫Let's go ahead and add these and I'm going to make them private.
‫So let's go ahead and add a private section just under the protected one, and we can add a couple of
‫health variables.
‫Now, these are going to be floats and I'm going to call the first one Max Health.
‫This will be the maximum amount of health that we can have and we can set this to a default value such
‫as 100 F.
‫Now, in addition to Max health, we're going to have a health value which will be the current health
‫and this will be what changes as we take damage.
‫So let's make another float and simply call this one health.
‫Now we can initialize health to zero F and then set its value and begin play.
‫And the reason we'll do that is because Max Health is going to have its own you property allowing us
‫to set it from within the details panel.
‫So let's give this a U property and make it edit anywhere.
‫That way we can change it as we're developing.
‫And then in health components CP here and begin play, we can simply set health equal to the value of
‫max health.
‫So that way when the game starts, health will have the value of max health even after we've changed
‫it in the details panel.
‫Okay, so let's compile this and back here in the editor, I'm going to open BP -- tank and open the
‫full blueprint editor.
‫And now I can add this health component here in the components panel.
‫So I'm going to click Add and Search for Health.
‫And here we see health.
‫Now, we called it health component in C++.
‫And in fact, the pop up shows that it's called health component.
‫So it strips away that component part when showing it here.
‫And you may have noticed that when you look for static mesh components or capsule components, you don't
‫see the word component here either for those.
‫So click on health and now we have our health component and you'll see that under health component we
‫have max health and it's set to 100 and we don't see health because we didn't expose that variable.
‫So now BP -- tank has a health component and it said that the tank owns this health component, the
‫BP -- tank.
‫Is the owner of this health component.
‫Let's compile and save and close and open BP pawn turret and open the full blueprint editor and we can
‫add a health component to the pond turret as well.
‫So let's click add components, search for health and select health.
‫And now we have a health component on the tower as well.
‫So I'm going to compile and save these close out and save all.
‫So we now have a health component and we mentioned that we'd like to have a health component so that
‫we can reuse it with different classes.
‫So if we have a pawn in the world, say, our tower, it's going to have its own health component and
‫then our tank can have its own health component.
‫Now, when our projectile interacts with either one of these, for example, when it generates a hit
‫event, when hitting one of these things, then our pawns can trigger some functionality in the health
‫component and change those health values.
‫Putting our health functionality in a component and then adding that component to a class has some advantages
‫over the alternative, which would be to simply place the health functionality in the base pawn class,
‫for example, and inheriting that in the tank and tower.
‫You see, this allows us to keep our tank and tower classes small and only deal with specified functionality
‫for those classes.
‫Whereas the health component can be added to any other actor, such as things that don't act like tanks
‫or towers.
‫We can make a destructible wall, for example, which could get destroyed as soon as it takes enough
‫damage.
‫So let's talk a little bit about how we can make this happen now.
‫Unreal Engine has built in damage functionality and its damaged system is actually quite sophisticated.
‫Any actor in our game inherits a number of damage events, one of which is called on take any damage.
‫This is a multicast delegate, just like on component hit.
‫Now we're going to add health components to our pawns and these will be based on our U.
‫Health component class.
‫So since we know that our pawns are going to inherit this on take any damage delegate, then if we create
‫our own custom function in the health component class, then we can use this function as a callback
‫and bind it to the on take any damage delegate.
‫So then whenever a damage event gets generated, the on take any damage delegate can broadcast and any
‫functions bound to it in its invocation list will be called.
‫So the next question is how do we actually get that delegate to broadcast?
‫We need a way to generate a damage event.
‫Well, there's a function in your gameplay statics that will do just that and it's called apply damage.
‫If we use this static function from your gameplay statics to apply damage, apply damage takes a number
‫of parameters, including the actor that's getting damaged.
‫So if we call apply damage the actors on take any damage delegate will be fired and that callback function
‫will be called in response.
‫So apply damage is what generates the damage event.
‫So to set all of this up, we have three steps.
‫First, create a callback function and since this will be bound to on take any damage, it must have
‫the correct input parameters so that on take any damage will call it and pass in all of the associated
‫information about the damage event.
‫Step two is to bind this callback to the delegate.
‫We've seen how to do this with the on component hit delegate using the add dynamic function.
‫We'll be doing the same thing here.
‫And the third step is to generate the damage event by calling the you game play static function, applied
‫damage.
‫So let's hop back into code and start setting this up.
‫Okay.
‫So we're here in health component and we're going to add a callback function.
‫Now, this can be void and we can call it anything we like.
‫I'm going to call this damage taken.
‫And because we're binding this to a delegate, it has to have the correct input parameters appropriate
‫for that delegate.
‫Now for functions bound to the on take any damage delegate, the input parameter list has to be as follows.
‫The first is an a actor pointer called Damaged Actor, and this is what it sounds like.
‫It's the actor taking the damage.
‫Next is a float called damage and this is the actual damage amount.
‫Now, next, we have a cost you damage type pointer called damage type.
‫And the reason we have this input parameter is because Unreal Engine has the concept of damage types.
‫You can create custom damage types that have extra data that can inform you to do different things depending
‫on the type of damage you may have.
‫Fire damage.
‫Poison damage.
‫Explosive damage and so on.
‫And if you want to implement custom functionality for each damage type, then you would implement that
‫in your own custom damage type.
‫Now we're not creating our own custom damage type, but because we're binding this function to the on
‫take any damage delegate, it has to have these input parameters.
‫So we're including it here.
‫Now the next parameter is an A controller pointer and this is called instigator and the Unreal Engine
‫Damage System.
‫An instigator is the controller responsible for the damage.
‫So if the damage was ultimately caused by a player controlling a pawn, then the instigator is the controller
‫that's possessing that pawn.
‫Now, there doesn't always need to be an instigator.
‫If there was none, then this would simply be null.
‫And now I'm getting the red squiggles so I can forward declare the A controller class here in the parentheses
‫for the function.
‫Now the last parameter is an A actor pointer and this one is called Damage Causer.
‫This is the actual actor causing the damage.
‫This would be the projectile itself.
‫So we've declared this damage taken callback function and just like our other callback function on hit.
‫This has to be a u function.
‫So we're going to give it the function macro just to make sure that it works once we bind it to the
‫on take any damage delegate.
‫And now that we've declared it, we're ready to define it in the CPP file.
‫So here in health components, CPP.
‫Down at the bottom, I'm going to go ahead and give this a function body and fully qualify the function
‫with the class name.
‫And now we have a callback.
‫So now we need to bind this to the delegate.
‫But the delegate doesn't exist on the health component, it exists on actors specifically.
‫We want to bind this to the on take any damage delegate in the pawn that owns this component.
‫So how do we access the owner of this component so we can bind to one of its delegates?
‫Well, we would do this and begin play and the way we can access the owner of a component is through
‫the git owner function.
‫If you hover over this, you'll see that it returns an a actor pointer.
‫This is the pointer to the actor that owns a component.
‫Components can be owned by pawns, they can be owned by actors, so it will return the owner in the
‫form of an actor pointer.
‫So we have our callback function and our second step is to bind it to the delegate on take any damage
‫that's going to exist on the pawn that owns the health component.
‫So since you have experience binding callbacks to delegates, this will be your challenge in the health
‫component class.
‫In Begin Play, you're going to bind damage taken to the on take any damage delegate that exists on
‫the owner of the component.
‫So you're going to access that delegate through the get owner function.
‫In other words, you're going to call get owner and from that you're going to access on take any damage.
‫And then from there you know how to bind callbacks to delegates using add dynamic if you get stuck look
‫in how we bound the on hit callback to the on component hit delegate pause the video and bind damage
‫taken to the on take any damage delegate now.
‫Okay.
‫So we're back here in health component and we're ready to do the binding of our callback function.
‫So here and begin play, we're going to access the owner using the git owner function and from there
‫we're going to use the arrow operator to access that delegate called on, take any damage and on take
‫any damage has the add dynamic function and it simply takes a user object and a function name.
‫Now the user object is the object we're binding the function for.
‫It's the class that has the callback function on it and that's going to be this.
‫And finally we have our callback function, which is going to be you.
‫Health component damage taken.
‫Now we need to remember the address of operator because this actually takes an address of the function.
‫So now damage taken is bound to on take any damage and it's added to that delegate's invocation list.
‫So whenever we generate damage events, we will get a broadcast from this delegate which will result
‫in the damage taking function, getting called.
‫So in summary, we've created our very own health component and we added one of these to both the tank
‫blueprint and the tower blueprint.
‫We created a callback function called Damage Taken and we bound it to the on take any damage delegate.
‫The next step is to generate a damage event which will result in broadcasting the on take any damage
‫delegate so that our callback function damage taken will get called in response.
‫And we'll set that up in the next video.
‫I'll see you soon.