‫Hello and welcome.
‫The subject of this lecture is going to be the take damage function of the actor and how we call it.
‫Let's dive in and find out all about it.
‫So in order for us to be able to deal damage to something, we need to actually have something which
‫we can hit.
‫So let's go to our content browser route and bring in one of the BP shooter characters.
‫Because actually what happens is if you bring in a character into the scene, then automatically Unreal
‫is going to make this character AI controlled and the ones that spawn at the player starts will be player
‫controlled.
‫So that's great.
‫That's exactly the behavior we want.
‫If I go ahead and hit alt p, you'll notice I'm actually in the wrong spawn point, so maybe I want
‫to delete this second player start.
‫If I hit alt p now we'll be in the right location.
‫There is my enemy.
‫I can shoot at the enemy.
‫I get the hit effect.
‫But obviously I'm not doing any damage.
‫Not yet anyway.
‫And doing damage is a two part process.
‫The first part is sending the damage and the second part is receiving it.
‫In this lecture, we're going to deal with sending damage to the player and the function calls involved
‫with that.
‫So the main one that we're going to be looking at is in the actor Dot H and we're looking for a function
‫call called take damage.
‫And you can see here there is this virtual function here.
‫It returns a float.
‫What it's returning is the actual damage applied to the actor, the actual amount that really got applied.
‫And what goes in is the damage amount, a damage event, which gives us more information about that.
‫The damage that happened, we have an instigator.
‫So that's saying who was the controller responsible for causing this damage?
‫And then the damage causer, which is what was the actor responsible?
‫So in this case, it might be a gun.
‫Or is it saying here the projectile that exploded, the rock that landed on you in this case, the gun
‫that shot you, is going to be the actor that was responsible.
‫So that's take damage in a nutshell.
‫Most of that should be pretty straightforward for you to put together with the exception of this damage
‫event struct.
‫And that's a little bit more complicated.
‫So let's have a look at this damage event structure and see what can what can we do?
‫How is it going to be composed?
‫How are we going to put this together?
‫Now, it's not immediately obvious, but there are actually two subtypes for this damage event.
‫In practice, you're not going to actually create an EF damage event, but rather you'd be creating
‫an EF point damage event, or you would be creating an EF radial damage event.
‫Those are the two main types of damage that exist in Unreal.
‫You can create more different types of damage for your own custom stuff, like you might have a fire
‫damage and with different parameters, but in that case, we're interested in the EF radial radial and
‫point damage.
‫So radial again would be something like a grenade going off, in which case you want to say, okay,
‫where is what's the radius, what's the fall off?
‫All of those kinds of things where as the point damage is, as it sounds much more appropriate for things
‫like guns.
‫So we're going to create an EF point damage event and we're going to go and look for the constructor,
‫here's the constructor for it, and we can see what it's taking in.
‫It's taking the damage amount again.
‫So that's a bit of repetition there.
‫We're having the hit result that caused this point damage to take place.
‫Then we're having the in shot direction.
‫Great.
‫We've already calculated that.
‫I was thinking a little bit ahead when we did that in the previous lecture and then this subclass of
‫damage type.
‫Now this can be used to pass more information so that actually, rather than creating a different damage
‫event type, you'd probably create a different you damage type.
‫So you might have a fire type or something like that in that case, because we don't need any kind of
‫damage type.
‫We're not doing fire damage or anything extra like that poison damage.
‫Then what we're going to do is just pass in a null pointer here instead.
‫So let's have a go at constructing one of these together and then I will let you call that take damage
‫event.
‫So the first thing to do is to paste in the type, we're going to call it the damage event and then
‫we're going to construct it.
‫So first of all, the damage amount, well, we don't have one of these for the gun yet.
‫We don't have how much damage it does.
‫So we can go ahead and create some information there.
‫We've got a max range already.
‫We want some more properties in here.
‫It's going to be an edit anywhere, which is going to allow us to say float damage.
‫How much damage do we do?
‫I'm going to do a damage of ten and then we can use this over in the C++.
‫So damage is what we're going to pass in as the first argument.
‫The second one is that hit result?
‫Well, fortunately, we've got the hit results called hit.
‫Then the next thing is going to be the shot direction which we already calculated for our emitter at
‫location.
‫Then we're going to look at the subclass, which is just going to be a null pointer.
‫Okay, so that is done.
‫The next thing would just be to get hold of the actor that we're trying to damage.
‫That's fairly straightforward from the hit you do.
‫Hit dot get actor on here.
‫But you will need to check that this isn't null because we don't want to end up with a null actor that
‫we're trying to do damage on or we're going to end up crashing.
‫So your challenge is going to be to call that take damage function check for the actor being null.
‫First of all, we don't want a null actor and call take damage and see if you can get it to compile.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we've got our guest actor in here.
‫I'm just going to call this I'm going to give this a little variable.
‫So a actor pointer is going to be the hit actor and we're then going to go ahead and do an if statement
‫in here, basically saying if the hit actor is not equal to a null pointer, then we're going to go
‫ahead with this hit actor to call the take damage function on it.
‫And we're going to do the damage amount in here is the first argument.
‫What was the second one?
‫I'm just going to navigate back to the take damage and have a little look.
‫So the next one is the struct that we've put together this damage event.
‫Let's put that in a second.
‫Then we're going to have a look at the controller that created this.
‫Well, that's our controller.
‫The gun's controller is the one doing the damage.
‫And then this is the instigator was the last parameter in our take damage.
‫And we probably only want to construct this struct inside this if actor not equal to null pointer because
‫it's not needed if we don't have an actor to apply this damage to.
‫So let's go over and compile and see if this is going to work.
‫So I got an error in my log.
‫Let's see what's going on.
‫Oh, I'm just missing a semicolon.
‫Well, that's pretty easy.
‫One to fix after the get actor there.
‫Let's go and try again.
‫Cool.
‫Everything has compiled successfully and we can just go ahead and play and just check that shooting
‫is going to not give us any null errors or try and crash the engine.
‫Looks like it's okay.
‫So we'll move on to the next lecture where we're going to try and receive the damage on this actor here
‫that we have placed in the level.
‫So you there.