Welcome back.
Now, we've seen that we can use a number of built in behavior tree tasks.
And these are really handy.
We have common ones ones to make the AI character wait make them move to a specific location, or really
any blackboard key that has a location in space, whether that's an actor, a character, or what have
you.
Now, these built in tasks are nice, but we don't really have any built in tasks for exactly the things
we need.
In every case, I don't have a built in behavior tree task called attack, for example.
Now for combat we need to attack.
So we're going to need our own behavior tree task and we can create our own.
Hey there Steven from the future here.
And I'm just coming back to let you know that in the original version of the course, we created a behavior
tree task in C plus plus based on Behavior Tree task blueprint base.
Basically doing the same wrong thing as when I created the behavior tree service.
So the GitHub commit for this lecture has a C plus plus class that you can ignore.
We're just going to create the blueprint for the behavior tree task.
In this video you can safely ignore the GitHub commit with the C plus plus class.
We're going to do things the right way in this updated version with the blueprint, I'm going to open
up my behavior tree.
And we know that we're reserving this branch for ranged attackers and this branch for melee attackers.
And our behavior tree knows which one is which.
It knows which one it's controlled pawn is because it has a ranged attacker boolean blackboard key.
And we're setting that from the enemy as soon as it's or I controller variable is set and that's an
on possess.
So quite early on it knows whether it's a ranged attack or not.
We're going to handle the melee attacker situation first.
And we're going to want to create a blueprint based on our behavior tree task.
So we're going to right click create a new blueprint class, search for BT task and get BT Task Blueprint
Base.
We'll select that and we're going to call this BT.
Attack and we have our blueprint, task, attack.
And of course we can override functions such as receive, execute I so we can receive that.
And the most important thing that we have to be concerned with here is that we finish execute so that
our behavior tree doesn't get hung up on this task.
The task has to return success or failure.
And before we return that success or failure, we'll be stuck inside this task.
So the way to return that is to use finish, execute.
And this returns success or failure.
I'm going to check that.
So it returns success by default.
And we're going to go ahead and just drag in the execution pin here.
So our task does nothing.
But we're going to do things with it definitely.
For now though, let's go back to our behavior tree and drag off of our sequence and expand tasks.
And there's our BT attack.
So let's use our blueprint BT attack.
And there it is.
And if we click on it we see that it has a details panel.
We can rename the node here.
And of course we have nothing really else unless we go in here and add things.
Now we have the owner controller.
We also have controlled pawn.
Now I'm going to drag off of the controlled pawn and use get object name.
And we're going to print the name of this controlled pawn.
So print string.
We're going to run that in and finish execute.
Let's just see what that controlled pawn is.
So if we're close enough to attack, we should see that BT task attack.
Let's go ahead and press play.
Here come our enemies.
Let's see if we get anything printed to the screen.
And here we see BP Goblin spear were basically spamming the window with those messages.
So as long as the enemy is close enough to attack and it's a melee attacker, we're getting some messages
and we know that the controlled pawn is set to that actual pawn.
Let's change this to owner controller.
And just see what we get printed to the screen in this case.
And as soon as they're close, we see our AI controller.
So we know that in Bot Attack, we have access to the pawn and the controller here in event receive
execute AI.
And that's looking good.
So now that we have a behavior tree task for attacking, let's show something visual here in the world.
Say we'll draw a debug sphere in the place of the enemy.
So in the place of our controlled pawn.
So I'm going to delete those nodes and do a draw debug sphere.
The center will be at our controlled pawns location.
So I'm going to use get actor location.
Drag that in for the center and we'll make the line color red.
Will make the duration three seconds.
The radius can be 40.
And we'll go ahead and hook this up to finish.
Execute.
And let's get those debug spheres.
There they are.
So now we have some visual feedback that will tell us when we're attacking.
Now if we're close enough to attack, which is a distance of less than 500, I'm going to want a little
bit more sophisticated AI behavior than just execute this attack.
So far, attack does nothing but draw a debug sphere, but we're going to make a gameplay ability to
execute for that.
But until then, I'd like our enemy to get close enough to quote and unquote attack.
And what that means is I'd like our enemy to move in closer, because 500 is not that far, really.
These enemies are coming in close because we're not aborting this other move to target here.
You see, if I'm far enough to approach, we're seeing if the distance is greater than or equal to 500.
If we were to abort this branch, as soon as this is no longer the case, then we should not reach closer
than 500.
So we can take this.
Am I far enough to approach and set this to abort?
Self, for example, and notice that this entire thing, this entire branch has green and it says nodes
aborted by mode itself.
And that's because if we abort this self branch, then whatever tasks are running here, those will
be aborted and we'll return back up the tree.
So if I go ahead and press play now.
And let these guys run.
You see that they stop 500 units away.
So I think that's what I'd like.
I'd like this to only handle running up to 500 units away.
And once we're close enough to attack here, then I'd like our melee attackers to move in.
We'll get to the ranged attackers later.
They're going to hang back a little farther so they can launch their projectiles and such.
But our melee attackers, they're going to move in close.
So I'm going to drag out and use a move to task.
Let's get our move two.
We're going to move to our target to follow.
So we're going to set the blackboard key here to target to follow.
This is going to have an acceptance radius five is a little bit close.
So I'm going to bring it up to about 20.
And we can rename this node by changing the node name.
We can call this get close.
So this task gets us in a little closer.
And then we'll execute it attack.
And with BT attack selected I'm going to actually rename this node to just attack.
So being able to rename the nodes like this can really make our behavior tree a lot more easy to read
at first glance.
So now our melee attackers are going to get closer.
Let's see how this works.
Here they come.
And look at that.
Our melee attackers are getting close.
Those two back there.
They are ranged attackers.
So our melee attacker came in close.
The ranged attackers stayed back a bit.
Now if that distance gets farther than what was it, 4000?
Something like that.
Well then our ranged attackers stay in.
The melee attacker comes.
Now.
If they get in each other's way, they don't create holes in the navmesh, so they're not going to create
path points around each other.
If I press P to show our nav mesh bounds volume, notice that these little guys are not making holes
in the nav mesh.
So if these two form a wall and this guy tries to move through it, it's not going to be smart enough
to avoid them.
There are ways to have them avoid each other, but by default the way it is now, they're not going
to avoid each other.
But for now, it's enough that our ranged attackers hang back and our melee attacker comes in, and
when it gets close enough, it hits that acceptance radius.
It attacks.
And we know because we see that red sphere.
So that is a good stopping point.
I think we are one step closer to having our AI behave as they should.
We're taking care of our ranged attackers.
They just hang back and our melee attackers, they decide to get close and they attack.
Now after our attack, we're going to want to decide what to do there and we'll handle that next.
And of course, we're going to need to actually make these little guys attack.
So we have a few steps left to go and we'll continue with this in the videos to come.
So great job and I'll see you soon.