‫Hello and welcome.
‫In this lecture, we're going to be dealing damage to this actor here.
‫We're going to do that by overriding a virtual method called take damage on the actor.
‫And you can see in the log here, we've got the health decreasing as I shoot this character all the
‫way down to zero and then it stops at zero, not going into negative numbers.
‫So let's dive in and see how that's done.
‫So while we are currently being able to do damage to the actors that we hit here, such as this guy,
‫we are not actually receiving that damage.
‫So this is where virtual methods of last lecture come in because just like begin, play or tic, we
‫want to be able to receive damage on an actor.
‫Now, fortunately, if you go to the actor H and have a little look in here, you can see that there
‫is this take damage virtual method.
‫Now this can be done also with events and you will possibly have seen it done that way before where
‫you register for events.
‫Now what I'm going to do is just a different way of doing damage, where we actually have damage stored
‫against the actor rather than in a component.
‫And one easy way of receiving that damage is through a virtual method here.
‫And this is the virtual method that we're going to override.
‫So I'm going to go ahead and copy this and go over to our particular actor, which is the shooter character
‫is going to be quicker to go and search for the shooter character dot H And the one thing that I wanted
‫to check is where this comes in, the actor dot H.
‫So if we scroll up, we should be able to see which section we are in here.
‫So I'm just going to scroll right up until I find that what whether we're in public or what they go,
‫we're in the public section.
‫So this ought to be in the public section here as well.
‫So I'm just going to stick it underneath our tick and set up player input component over in the shooter
‫H and we just paste it in and as I said in the last lecture, you can use override to make sure that
‫you're indeed overriding a virtual method from the parent and it's not necessary to have virtual in
‫front here, but we'll keep it anyway.
‫And here you can see all of the data that's coming in to take damage.
‫We have got a damage amount, the damage event and the controller that is causing the damage, the instigator
‫and the damage cause.
‫All that stuff that we'd passed in from the gun is cropping up here as well.
‫So that's great.
‫Now if we go ahead and do a control shift P create implementation or copy and paste, then we can go
‫and make the implementation for take damage.
‫But the first thing to be wary of is that whenever we're overriding something, we want to think about
‫calling the the parent class version of take damage because there may be some implementation in there
‫that needs to run.
‫So that's what we're going to do.
‫We do this always.
‫We've done it with tech.
‫We've done it with Begin Play.
‫You can see we've always got this super cool at the beginning, and that's because when we override
‫a method, we override it completely.
‫We completely remove that behavior.
‫So if we do want that behavior, which we typically do, then we should always start with a cool to
‫super.
‫So we're going to do a cool take damage.
‫That's going to take the damage amount.
‫So this is where it gets a little bit tedious, sadly.
‫My autocomplete tool doesn't do this for us.
‫Damage event.
‫Then what was the next one?
‫We had the control.
‫So the event instigator, let's just copy.
‫And then we've got the damage causer, which is the actor.
‫So all of that gets called up to the parent implementation of take damage.
‫And the one other thing is that this has a return type.
‫So the actual damage applied so we're going to do float.
‫Damage applied is the return value from take damage.
‫So we're just going to keep hold of that damage applied there.
‫And realistically, that's going to be the same as the damage amount in our particular use case, but
‫not necessarily so we're going to use the damage applied.
‫The next thing we want to do is keep hold of the information about how much health we've actually got.
‫Now the way I'm going to do this is with two variables here.
‫The first one is I'm going to make both of them new properties because I want to make the health visible
‫in the inspector, at least for now.
‫And we're going to have the first property be the max health.
‫That's what we initialize the health to initially.
‫So you property, it's going to be an edit default only because it's going to be used in begin play.
‫And so changing that value of max health after the effect, it's not going to have any impact.
‫So Max health, I'm going to set the max health to 100 here and then we're going to have one other property
‫here, which, as I've said, is going to be a new property just for us to be able to inspect it.
‫And I'm going to have that as visible anywhere.
‫But not editable because we don't want to be able to change that health, at least not at the moment.
‫You might want that for debugging purposes, but I don't.
‫So we're going to have health here and in play.
‫We're going to set it up so that that health.
‫Is equal to the max health.
‫So we start off with spawn.
‫We have our maximum health and then we can start decreasing it.
‫So it's time for a challenge for you to decrease the health of the character.
‫So you're going to use the damage applied here rather than the other option here.
‫So we've got our shooter character, CP take damage.
‫We've got a damage amount, but we actually want the damage applied that comes back from take damage
‫because that could have been reduced by some process in the parent.
‫So we do that.
‫Then we reduce the value of health and log the results just for simplicity's sake so that you can check
‫that when you're shooting at that eye that his health is going down and then test it out.
‫So pause the video and have a go.
‫Okay, welcome back.
‫So let's give it a shot.
‫So health, we basically want to decrease that by the damage applied.
‫But is that enough?
‫Well, what if our damage applied is greater than the amount of health we have got left?
‫Well, in that case, we probably want the damage applied to be that amount of health.
‫So the way we can do this is we can say that the damage applied.
‫Should be equal to the f math dot min of the health.
‫And the damage applied.
‫So this is the damage that we basically want to apply.
‫So we're going to I'm actually going to go ahead and rename that instead of calling it damage applied.
‫I'm going to call it the damage to apply.
‫Damage to apply.
‫So go rename that throughout here and then the damage to apply.
‫We're going to health minus the damage to apply.
‫That makes sense.
‫And the basically at worst that's going to be the health.
‫So that's going to set our health down to zero.
‫And remember, we're supposed to return the damage that we actually applied.
‫So we'll do a return damage to apply, which has now been applied and we finally want a UII log at the
‫end here, which is just going to be a warning so we can see it.
‫And I want this to be the health left here and it's going to be a percent F to or percent lowercase
‫F to show a float.
‫And then we can pop in the health right there and go ahead and hit build.
‫Now.
‫Mine's compiled after fixing a missing semicolon.
‫Here, let's go and make sure that we have a window developer tools and we want the output log here
‫so that we can see that log message.
‫And I'm going to go ahead and hit play and go and try and shoot at this character if I shoot him.
‫There you go.
‫Health left 1976, blah, blah, blah, all the way down to zero.
‫And if I keep shooting, we don't go below zero because of that eff math dot min.
‫So that tells me that we've got our health set up correctly.
‫Just another way of doing it as opposed to doing it within a component.
‫You can do it within the actor as well as some pros and cons of that approach.
‫Eventually, you may want to strip this out into a separate component so that you can reuse it on different
‫elements.
‫But this is just another way, as I say now, because this character isn't looking particularly dead
‫after we've shot him down to zero health.
‫We need to fix up the animations to support that.
‫So we'll see that in the next lecture and I'll see you there.