right we're back once again end up to this point we've actually accomplished
quite a bit if we just do a really quick play test we've got a character that we
can move and rotate he can shoot his weapon we have an enemy that will chase
him down but right now our projectiles don't really do anything
what we want is for them to damage the enemy and then eventually destroy him
so that's what we're going to do in this video we're going to set up that
damaging behavior between the projectiles and the enemy to start this
off we're going to begin inside the enemy character class we need to
basically expose the ability to transfer damage to talk to that health value that
we set up in c++ because actually did two things in c++ if you recall we set
up these variables and then we also created the calculate health function
which is really just going to take an incoming Delta add it to the existing
health and then store whatever that new value is as the new health which is
great but we need though is a way to talk between blueprints the projectile
is one blueprint the enemy is another and we need a way to communicate in
between them and the way we do that communication or one of the ways in
which we can do that communication is with an interface so we're going to make
a blueprint interface and use that to communicate with that calculate health
function that we set up in c++ so to to set this up
actually before we send anything out let me kind of describe i guess in general
terms what an interface is an interface is a collection of unimplemented
functionality that sounds like a real that's really a programmer a way to
explain it so let me think of a different way to say i have two people
in front of me was a bob and tom
we have these two characters Bob and Tom and if I tell Bob to tell me a story Bob
might tell me about how he walked home
last night and stop by the grocery store and bought something for dinner but tell
Tom to tell me a story he might tell me about something he did last weekend
maybe he went camping maybe he but well the point is he did something totally
different than Bobby's they both had two different results that they gave back to
me even though I gave them the same command both just told him tell me a
story when interface is how you set up those generic commands like tell me a
story and then within each receiving thing with it within each thing that
knows what that command is you can make it define what that really means if
that's confusing in anyway just watch will make it all makes sense so let's
start by building our interface we're going to right-click and go under the
blueprint submenu and there it is
blueprint interface so click and we're going to call this I lowercase I I short
for interface damageable that is a common code naming convention for
interfaces so I damageable we're going to double-click it now we have no what
it has here is a function that will have no implementation basically it's like
kind of declaring that we're going to do something with this given name but we
don't really know what yet and we're going to name this function is affect
health now affect health is going to need to take in some value some numeric
value that we can use to affect our health by some amount so under the input
section down here in the details panel
I'm sorry under it is in the details panel excuse me I'll click on the new
button and let's set this to a name of Delta go and then take its type and set
it to a float
that's it that's all we have to do we're not going to notice we can't even
connect any nodes to this because it's all grayed out the purpose of an
interface is that you just define the structure for what a-what functionality
you want and you define what that functionality is within any object that
implements that interface so the function could mean different things to
different classes so let's compile and save this and then we're actually done
with I damageable we don't
to do anything else to it we can close it however we need to make sure that our
enemy character implements I damageable now another fun fact just kind of
introducing you to some of the concepts of working with interfaces any class in
your game can talk to this interface now so like right now I could say affect
health and there it is an interface message so he can send out a call
through the interface to talk to other things he doesn't have to have anything
special to do that the interface just has to exist but if he needs to receive
data source receive some sort of call from the interface then he must
implement that interface and here's how you do that if you go under your class
settings not class defaults class settings there's a section here called
interfaces and there's implemented interfaces right now none are
implemented so click the Add button and from the jaw drop down you'll see I
damageable click on that and there you go that's it that's all you have to do
make sure you compile and save once you've done that now when you
right-click check it out if you type affect health
there is now an event for it so this event will be fired
whenever any other blueprint calls affect health on this object so now you
basically just created a pathway where other blueprints can talk to this guy
now what's going to happen when we call this this is where r c++ code becomes
handy we can drag a wire out from this and just say calculate health so we have
affected it from some outside source
now let's calculate and get our new result and Delta just plugs right into
Delta that's it
now let's talk about what happens when this guy dies so I remember calculate
health is going to take an incoming Delta it's going to add that to the
existing health and then store that is the new health so if if the value is
negative 20 the new result will be 80 because it's counting down and then it's
going to run that little helper function we set up called calculate dead
and calculated just simply says hey did help make it 20 or less yet because if
it did then set is dead too true
it hasn't yet leave is dead at false
that's great because what that means is we can grab is dead and notice something
do you remember actually let me I'm just gonna I'm gonna open up and show you
watch this this is pretty awesome if we open up our base character class which
is going to open up visual studio will let it do its thing
thank you prepare solutions haha we're back that was nice and fast if you
remember
so there's calculate dead and you can see it's just checking to see if
something's dead or not and it's setting the value but if we take a look when we
declared the is dead boolean property we had those keywords right blueprint
read-only it's not blueprint read/write we can actually verify that check this
out if I close out of visual studio so we don't need anymore let's go back over
to unreal and we go inside our enemy character look at this my drag is dead
into the world
notice that set is grayed out i cannot set that's because this is read-only but
i can get it which is all i need to do i just wanted to point that out that side
of those keywords were actually doing cool stuff so now we can take is dead
and let's test it to a branch and if is dead
test out to be true then let's destroy this actor so the enemy will just cease
to exist because that's how destruction tends to work now let's just for
debugging purposes I'm going to set up some temporary quote-unquote code here
as soon as we calculate health i want to get our health and I want to print it
out to the screen let's do print string will connect this into the line
now notice this takes in a string and health is a float value if i try to
connect them
unreal is like oh you don't know the difference between a float in a string
that's fine don't worry I'll convert them for you
so cool what this means is we will get a message from some outside source
spoiler alert it's going to be from the laser blazer is going to call this we
will calculate our health will print out whatever our new health happens to be
then we'll check and see if the character is dead if it turns out they
did die then we're just going to destroy them and they'll just disappear
let's compile and save now this won't do anything just yet
we had to tell the project dial that it can talk to the enemy by way of the
interface and here's how we're going to set that up so if we take a look at our
projectile we have this nice collisions fear we're going to use this to our
advantage
so let's uh start off by grabbing here in our event graph we're going to begin
with event actor begin overlap and overlap is what happens when two actor
two actors components are intersecting the same space so basically that
collisions fear we set up if thats overlapping something then we're gonna
get this this event call also it's a good idea to double check your collision
settings so let's click on our projectile collision this is currently
set to overlap all dynamic I'm kind of okay with that but since we're only ever
shooting at pawns let's set this to overlap only pawn it will probably work
better in the long run so compile and save now back over here in the event
graph as soon as we detect an overlap what's going to happen
well week we have at we have access to the other actor
this is the other thing that we just overlapped with so check this out we can
check to see if it implements a given interface does it yes or no and the
interface we want to look for obviously is I damageable
so I just held on the B key and click that created a branch node super useful
shortcut once again i'll show you hold down be click you get a branch and we'll
connect that up so we just tested to see if whatever we just hit or whatever we
just overlapped with implements the I damageable interface awesome
now if it does if it does we could just damage that whatever it is we hit right
and that would work just fine but one extra spoiler depending on how you set
up your arrow component where you're shooting depending on where your gun is
placed it is possible in extreme circumstances that you could end up
colliding with overlapping the player in effect it's kind of like saying you have
the ability for the player to shoot himself or put another way you could
shoot other hero characters if you modify this game to be multiplayer so
what I'd like to do is establish a system where we then check to see if we
just overlap with the player and if we overlap with the player
we're not going to do anything so I would because in the at the end of the
day the reason I'm checking for this is the player will eventually also
implement I damageable he doesn't right now but he would test true at this point
we need to verify we didn't actually hit him
here's how we're gonna do it let's go to the hero character class so here we are
in the hero
let's go over to the event graph and we're going to select his class default
properties not settings is defaults and over the details panel at the very
bottom there's this actor category
expand that and there's this little hidden property called tags tags are
just like tags on a whole lot of different internet activities right
there just the ability to put some sort of name in place that could mean
anything
so I like it hits the little plus B let's add a new tag and let's call this
friendly awesome so we have a tag name friendly so now we can look for that tag
it's compile and save and then over here in our projectile we can take the other
actor now i'm going to use rear out notes just so it's easy for you to see
what we're doing we can say has tagged and look at there's a note for that
actor has tagged now what tagger we looking for
obviously we're looking for friendly now that just reports out a boolean value
didn't find the tag
yes or no let's do another branch will down be and click so if this tested true
let's then test and see if he's got the tag now here's the deal
whenever I get the result that I want out of a branch i would rather that
result be true
basically i would like all of my tests to return true if they can so we're
going to take actor has tagged actually going to attach that to a not node its
kind of inverting it if you read this out in plain English you would say hey
does this object not have the friendly tag which is a little different than how
you actually say that because it turns out you can see that in common everyday
we speak which is kind of annoying
ok so we check and see if they do not have the tag we make sure they don't
have the tag and if they don't have the tag then finally we can affect their
health so let's do another route node now to make sure that we're calling
affect health on the right object let's do it this way let's just say affect
health so now that is asking for a delta right how much you're affecting the
health
let's set this to negative 20 remember our c++ code is adding the values
together so if we want to decrement health we need to set this to a negative
number
ok so we've done that but let's so let's make this into a variable so I'm gonna
take Delta i'm going to right click on it and promote that to a variable will
call this
damage
cool that's being stored now pretty much no matter what happens I want us to
destroy this laser or this projectile when we're done so make a destroy actor
node and after we've affected health we want to destroy the actor if we hit
something that did not implement the interface also destroy the actor but if
we hit something and it turned out to be the player
we're gonna do nothing so we'll leave false disconnected and i'll use some
rear out notes to make this a little bit cleaner
ok so if you need to see that whole network again let's stretch this out
just a little bit i'll i'll try to condense things down as best I can so we
can fit them all on the screen for just a moment bear with me
bear with me there we go
and then pull listen so there you go so begin overlap check and see if it
implements the interface if it does check and make sure that it doesn't have
the friendly tag and if that's true meaning it doesn't have the friendly tag
let's go ahead and affect its health alright awesome so let's compile and
save let's come out here to our map let me hit ctrl s to save everything let's
hit play and he is i'll check it out you see his health dropping account all the
way down to 20 just like that
hit him again he's gone booms we got it we're doing damage to the enemy now that
we've confirmed that however just to be on the nice and neat side let's go into
our enemy and let's remove this little portion of the graph that is reporting
that out we don't really need it we know it's working so compile save play one
more time and now we shoot after five shots player of the enemy goes away
which is perfect so we have the ability to damage the enemy
that's great that is going to wrap things up for this video thanks a lot