Welcome back.
Now we're applying damage, but so far that's been pretty boring.
Aside from showing how much health we have on our enemy with these health bars, which do look good,
we would like our enemies to react in some way.
So in this video, we're concerned with a hit react of some sort.
So how are we going to accomplish this?
Well, a gameplay ability is a good idea.
We can activate an ability if we've damaged our enemy without killing them.
But what can that ability do?
Well, we need to be able to respond in some way.
We'd like to play a hit React montage, for example, and that montage can be something that we have
on the enemy.
So it's specific to the enemy, but we'd also like to be able to prevent things from happening while
the enemy is hit reacting.
Let's take a look at the assets folder and go to enemies Goblin animations, Spear and it looks like
we have hit React Spear.
So here's a hit React animation for when the enemy is hit.
While that's happening, I don't want the enemy to be able to move or attack or do anything.
So I'd essentially like my enemy to be immobilized for a second while this animation is being played.
Now, while that's happening, we can give this enemy a gameplay tag and we know that gameplay effects
are a good way to apply gameplay tags to enemies.
So I think it would be a good idea to make a gameplay effect that will give this enemy a tag.
And if we decide that that effect would like to modify any attributes or do anything else really, we
could add more functionality in the future.
But I'd like a basic gameplay effect that will apply this tag.
Now that means we need a tag to apply and we know that we can add native gameplay tags to our for a
gameplay tag.
Singleton.
If we're going to want to use those tags in C plus plus now we are going to want to use this particular
gameplay tag in C plus plus because I'd like to be able to respond with callbacks to delegates that
the Ability system broadcasts whenever an ability system component is granted a tag, say by a gameplay
effect.
So we have a number of things we'd like to do here.
We'd like to give this enemy a gameplay tag using a gameplay effect, which means we need that tag,
right?
So these will be our first steps.
So you now have a quest for this hit React gameplay effect.
We're going to need a couple of things.
First, I'd like you to create the gameplay effect G hit, react and create a native gameplay tag called
effect Hit React and you're going to grant effects dot hit react in this g hit react.
Now this can be an infinite gameplay effect so that we can remove it at some later point in time.
Go ahead and pause the video and conquer this quest now.
Okay, so we need a gameplay effect.
I'm going to go into content blueprints, ability system, gameplay effects, and we're going to have
a gameplay effect here for Hit React.
So let's go ahead and create a new blueprint class Search for gameplay effect.
Choose that and this will be our g underscore.
Hit react so we can open this up and this will be an infinite gameplay effect.
So we'll go ahead and change its duration policy to infinite and we need to add a tag to its granted
tags.
Now we're going to want to add that to our native tags.
So I'm going to go ahead and close the editor and open up our gameplay tags and we'll add a hit react
tag and this will go just under damage.
So we're going to say F gameplay tag and we'll call this effects underscore, hit, react.
And we need to add this in the CPP file as well.
So we'll copy the very last tag that we made, which is damage and we're going to go ahead and make
our new tag, which is effects underscore, hit, react.
Now the tag itself will be effects dot hit react and for the comment we can say tag granted when hit
reacting.
So now that we have this tag, we should be able to add this to our gameplay effect.
So we'll go ahead and compile and launch the editor now.
And back in the editor.
Let's open up our asset editors and here's G hit react.
We need to go down to its granted tags and add to added our effects dot hit react.
And now our gameplay effect will grant this tag.
So now that we have an effect that when we grant it the ability system component we grant it to will
have this tag, I'd like to be able to respond to getting this tag.
And we can do that.
We can respond in C plus plus.
I'm going to close the editor and I'm done with our gameplay tags.
I'm going to close that.
I'm done with our gameplay ability or a projectile spell.
I'm going to leave the attribute set open, but I'd like to go into my enemy class.
So I'm going to go to my public folder into character and open Aura Enemy, and I'll go ahead and get
the header and CPP files both open here.
And what I'd like to do in Aura Enemy is listen for a gameplay tag change on the ability system component.
We can do that.
We can listen for a change.
If a gameplay tag is added or removed, we can do something in response to that.
That's what I'd like to do for the hit React tag.
So I'm going to go into my begin play where we know that we're going to have access to our ability system
component, and I'd like to bind a callback or a lambda to a delegate that's broadcast from the ability
system component.
Whenever a tag is added.
We can do that.
Now I've got some extra empty space that I'm going to clear up right here before scrolling down.
And here we have our ability system component and we're binding to some delegates on it here.
I'd like to bind to one right here since we have our ability system component.
So I'm going to say ability system component.
And the function I'm after is register gameplay tag event.
So this says allow events to be registered for specific gameplay tags being added or removed so we can
specify a gameplay tag and we can also specify what happens in response.
In other words, we can have a callback for it.
So I'd like to make a callback that can be bound to this.
Now this function returns a delegate of type on gameplay effect.
Tag count changed so we can see what the signature needs to be for a callback that can be bound to this.
I'm going to go to declaration of this where I can see the type here.
I can right click on that, go to its declaration and here it is.
We see that it's a multicast delegate to params that takes a const gameplay tag and an int 32.
This is actually for the new tag count.
Remember, we can have tag counts, we can have multiple tags of the same kind.
So we need a callback with this particular signature.
So let's make one here in enemy.
I'm going to go ahead and close gameplay effect types and ability system component and make a callback.
It's going to be a void function called hit react tag changed.
So this will be the function we have called when our hit react tag is changed and I'm going to move
this all the way up to the public section here.
Now this needs a signature with a const F gameplay tag.
We'll call this callback tag and an int 32 called New Count.
And we'll receive the count whenever our tag count changes for this given tag.
So we'll create a definition for this.
And we can do in this function what we like, but we want to bind this right here and begin play.
So we're going to register gameplay tag event.
This function requires the tag to respond to.
Now we need to get that native tag.
So scrolling up to the top, I'm checking to see if we're including our gameplay tags and we're not.
So I'm going to include.
Or a gameplay tags.
H.
I know we added more to that path when we included this elsewhere, but we should just be able to include
it this way.
And what we can do here is use for gameplay tags, get dot effects, hit react and we can just pass
that straight in.
So there's the tag and the next input parameter is an enum called event type.
So it's an E gameplay tag event type.
Let's type that E gameplay tag event type.
Double colon.
And we have some options here.
We can respond with this callback when the count changes at all.
We can also respond to the callback only if a new tag is added or if the tag is completely removed.
So rather than just removing a single count, we only get our callback called if the tag is removed
completely.
So we're going to use this new or removed and this function returns this delegate.
So this whole expression here is going to return the delegate that we can bind our callback to with
dot add new object.
So I'm going to hit enter to go to the next line here.
Hit enter twice.
Actually, I'll put my semicolon there and for our input parameters we need to pass in the user object
that's going to be this and our callback, which is going to be the address of a ora enemy hit react
tag changed.
So now whenever our enemy ability system component receives the hit react tag.
Hit react tag changed will be called in response and we can do something here.
I'm going to go ahead and move this callback function to beneath Beginplay just because I want to.
So what are we going to do in this callback?
Well, we can do anything we want.
We can check to see if this tag has been added for the first time.
Or we can check to see if it's count is now zero, etcetera.
Now, if the count is greater than zero, we know that we should be hit reacting.
If the count is zero, we're not hit reacting so we can say const bool, be hit.
Reacting equals new count is greater than zero.
We know we're hit reacting in that case.
Now knowing if we're hit, reacting is actually kind of important.
We may need to know about this in blueprint.
Our enemies are eventually going to get their own behavior trees.
And there may be circumstances in blueprint where we need to know if we're hit reacting or not.
For instance, in our animation blueprint, etcetera.
So I'd like to have a member variable called Be Hit Reacting.
We're going to add that to the Ora enemy.
So instead of creating a const local boolean here, I'm just going to have on Ora enemy, a public variable
called Be hit reacting.
Now it'll be false by default, but this can be exposed to blueprint, so we're going to give it a new
property.
We'll make it blueprint read only and category.
Combat.
That way we'll just have the variable and we'll know at any point in time whether we're hit, reacting
and hit react.
Tag change can really do anything it wants.
We can stop any movement.
Now, I know our characters aren't moving yet, but we can stop movement.
We can set the character movement components.
Max Walk speed to zero, for instance.
So just to have something to do, let's do that.
Let's say get character movement.
Max Walk speed equals and we can check, be hit, reacting.
If we are hit, reacting, it can be zero.
If we're not hit reacting, it can be whatever it's supposed to be.
Let's make a member variable for the base walk speed for an enemy.
So this can be a float base walk speed.
We'll make it, say 250 f nice and slow.
We'll make it Blueprint read only and category combat is fine and we'll set our speed to base walk speed
if we're not stunned anymore.
So we've removed the gameplay tag in this case.
And if this variable is now going to determine the walk speed, we should set the walk speed and begin
play to this value as well.
So let's go to begin play and the first thing we'll do is say get character movement.
Max Walk speed equals base, walk speed.
So now this variable determines our walk speed.
So we have a callback that gets called in response to this gameplay tag being added, which we can add
easily by applying a gameplay effect.
So how are we going to do that?
Well, let's go ahead and compile and go back into the editor.
And back in the editor.
Let's go ahead and open up our asset editors.
I think I'm done with CT damage.
I don't really need my hit React spear.
I'll leave it open, though.
I'm just going to keep it there for now.
We have our G hit React and all it does is apply this tag.
But we have a callback responding to when this tag is added, don't we?
And we can easily apply this gameplay effect.
We know how to do that.
The question is when and where do we do that?
Well, I'd like a gameplay ability to do that.
I'd like to create a gameplay ability and in that ability apply the effect.
And that way if we ever activate the ability on the enemy, it will apply the effect to itself.
And then we can also do something like play a montage.
We know how to play a montage in wait, and if we play a montage and wait for that montage to finish,
well, we could remove that infinite gameplay effect once the montage is complete and that will be your
second quest for this video.
You're going to create a gameplay ability, so create a new gameplay ability.
You can call this GA hit React and this will be a pretty simple gameplay ability.
I want you to apply the hit React effect.
That's the first thing you should do and then play a hit React montage and remove the effect when that
hit React montage is complete.
You know how to do this?
You know how to play a montage and you know how to respond to different things happening with that montage,
such as the montage, completing or being interrupted and so on.
So you have all the building blocks that you need to implement this gameplay ability and make sure to
end the ability when you feel like it should be ended.
And then we'll go ahead and activate this ability together.
So pause the video and make the gameplay ability now.
Okay, so we need a game play ability.
I'm going to go into blueprints, ability system, gameplay abilities and we'll have a gameplay ability
right here.
Let's create a new blueprint class based on gameplay ability.
There we go.
And we'll call this G a underscore hit react.
So let's take a look at G hit, react.
Now, this is going to be pretty simple.
First, we want to apply a gameplay effect.
Now, I didn't tell you exactly how to do this, but I think you're capable of figuring that part out
by now.
And you've probably found that there are multiple ways to do it.
You might have created a C plus plus class for this and added a gameplay effect to apply.
Maybe you added a gameplay effect class to the base or a gameplay ability class.
All of those things are valid.
You can also simply right click and type apply gameplay effect to owner.
You don't have to go through a gameplay effect spec.
You can apply a gameplay effect directly and specify that gameplay effect here.
So we can choose g hit react here and that is really all we need to do.
Now we want to apply this effect, but we would like to remove it as soon as we're done and in order
to remove an effect later, we need a reference to it.
We need a handle to that gameplay effect and that's what this node returns.
A gameplay effect handle structure.
We can promote this to a variable and we can call this active G hit React.
So now that we have a handle, we can remove it later.
Now, as soon as we've done that, we want to play a montage.
Now we can use the ability task, play montage and wait.
Now this requires a montage to play, and for this I don't want to hard code it.
It's okay if we hard code the hit react because that applies to basically anything we can apply this
to.
We can apply this g hit react to anything we want.
It doesn't depend on the scenario.
It's just something we can hard code.
But for this we don't want to hard code the montage because we have a hit React spear for the spear
goblin.
But what about for the slingshot goblin?
We have a hit React slingshot here.
This is a completely different hit, react.
And then we have other enemies that have their own hit react.
So we really don't want to hardcode the hit React montage.
This should be something that we can get easily from the thing that we're hitting.
Now, the actor, the Avatar actor specifically, we can get and cast to the combat interface if we
like.
So we can use.
Get Avatar actor from actor info.
And just like we've done previously, we can cast this to a combat interface cast to combat interface
and from combat interface.
Well, we could retrieve a hit React montage if we like, and then our gameplay ability doesn't depend
on a specific enemy or character at all for that matter, but rather the interface.
So I'd like a function to return a hit React montage and that way we can call that and pass that in.
So let's go ahead and add that to our combat interface.
I'm going to save all and we'll go into public interaction combat interface, and we'll make a function
here and it should return a U anim montage pointer and we'll call this get Hit React montage.
Now, I'm going to go ahead and forward declare you and a montage here just outside the class so we
don't have to include that.
And we'd like to call this from Blueprint now making this function virtual and overriding it in C plus
plus is relatively straightforward for raw functions, but you have to understand the limitations of
U functions exposed to blueprint.
We've seen that we can make a blueprint implementable event and implement that in blueprint, no problem.
But if we're going to make this a blueprint callable function, then we're not going to be able to override
it and C plus plus.
So to get the best of both worlds, make something blueprint callable, but also have a C plus plus
implementation, we're going to use a U function with Blueprint Native event.
Now, Blueprint Native event means that we don't have to mark this as virtual blueprint.
Native event is going to automatically generate a virtual native version that exists in C plus plus
that we can override here in C plus plus.
And that's the version that has underscore implementation at the end of it.
So we're going to override this and I'd like to do it in a character base.
We're going to override the implementation version of it.
So we're going to say virtual.
And this returns a U anim montage pointer.
And this is get hit, React montage.
But look at what we get for autocomplete.
It's the implementation version.
That's what we'd like to override.
And I'd also like to forward declare the UN and montage type here so we don't have to include that type.
And this is going to return an anim montage, which means that we should have a montage variable for
hit react that we'd like to return.
So here on our character base, I'm just going to put it down here in the private section, a U and
a montage, and this can be a T object pointer.
And we can call this hit React montage.
This can be added anywhere.
So I'm going to copy the property from above.
And this can be in the category combat.
And as long as we set this on the character that hit reacts, then we'll be able to return this from
that interface function.
So let's go ahead and implement the implementation version which can return simply hit react montage.
So that goes for any character based on or a character base.
As long as hit React montage is set, then this interface function will return a valid pointer.
So this is what we can call in our gameplay ability.
Now we do want to call this from Blueprint in our game hit React ability, right?
So just because it's partially implementable in Blueprint doesn't mean it's callable.
We want that function blueprint callable as well.
So not only should this be blueprint native event, it should also be blueprint callable.
So we'll give it that specifier and now we can go ahead and compile and launch this and we can set that
hit react montage on our enemies and access it from within the hit React gameplay ability.
Okay, so let's get that game ability back open here.
We'll go to a hit React and after casting to the combat interface, we can call, get hit, React montage
and this will return a montage that we can pass in for our montage to play for play montage and wait.
So as it stands, the only thing we're doing here is applying a gameplay effect, storing its handle
and then taking our Avatar actor, casting it to a combat interface and calling this get hit React montage.
Now, if this is a valid hit React montage, then we should see that montage played and that'll only
be valid if we set that montage variable.
So why don't we go ahead and go into Blueprints Character and Goblin Spear open that blueprint and search
for Hit React montage and we'll set that.
Of course, we need a montage, don't we?
So we can go into our hit React spear montage, we can browse to the asset and just simply make a hit
react montage out of this.
So create and we'll use create and montage and we'll call this aim hit, react, underscore Goblin.
Spear.
And this is a very simple montage.
It just has one montage section playing the hit React.
I'd like to make one for the Goblin Slingshot.
So I'm going to go to hit React Slingshot and do the same thing here.
I'm going to right click go to create, create and a montage and call this a m hit React underscore
Goblin Slingshot.
So we have montages for both of those.
And we can go into Goblin Spear and set this to a m hit React Goblin Spear, and we can go into the
Goblin Slingshot blueprint, search for hit, React and set this to hit React Goblin Slingshot.
So now these enemies have a hit React montage.
Excellent.
So now we have this guy hit React.
Right?
And we're never actually activating this ability.
That's one of the last things that we need to do.
And of course we also need to remove that gameplay effect so that any C plus plus callbacks can respond
to that as well.
So we have a couple things left to do.
The video is getting a bit long, so I think this is a good stopping point.
And in the next video we'll take care of what happens when the montage is finished playing.
We'll remove that gameplay effect and we'll also handle activating this ability in the first place.
So a couple things left to do and we'll handle that next.
I'll see you soon.