WEBVTT

00:06.850 --> 00:08.050
Welcome back.

00:08.080 --> 00:12.760
Now, we've seen that we can use a number of built in behavior tree tasks.

00:12.760 --> 00:13.990
And these are really handy.

00:13.990 --> 00:21.730
We have common ones ones to make the AI character wait make them move to a specific location, or really

00:21.730 --> 00:28.210
any blackboard key that has a location in space, whether that's an actor, a character, or what have

00:28.210 --> 00:28.570
you.

00:28.570 --> 00:35.260
Now, these built in tasks are nice, but we don't really have any built in tasks for exactly the things

00:35.260 --> 00:35.680
we need.

00:35.680 --> 00:41.920
In every case, I don't have a built in behavior tree task called attack, for example.

00:41.920 --> 00:44.230
Now for combat we need to attack.

00:44.230 --> 00:48.730
So we're going to need our own behavior tree task and we can create our own.

00:48.730 --> 00:51.070
Hey there Steven from the future here.

00:51.070 --> 00:58.090
And I'm just coming back to let you know that in the original version of the course, we created a behavior

00:58.090 --> 01:04.180
tree task in C plus plus based on Behavior Tree task blueprint base.

01:04.180 --> 01:10.930
Basically doing the same wrong thing as when I created the behavior tree service.

01:10.930 --> 01:17.290
So the GitHub commit for this lecture has a C plus plus class that you can ignore.

01:17.290 --> 01:21.280
We're just going to create the blueprint for the behavior tree task.

01:21.280 --> 01:26.530
In this video you can safely ignore the GitHub commit with the C plus plus class.

01:26.530 --> 01:32.950
We're going to do things the right way in this updated version with the blueprint, I'm going to open

01:32.950 --> 01:34.390
up my behavior tree.

01:34.390 --> 01:41.380
And we know that we're reserving this branch for ranged attackers and this branch for melee attackers.

01:41.380 --> 01:43.870
And our behavior tree knows which one is which.

01:43.870 --> 01:52.330
It knows which one it's controlled pawn is because it has a ranged attacker boolean blackboard key.

01:52.330 --> 01:58.450
And we're setting that from the enemy as soon as it's or I controller variable is set and that's an

01:58.450 --> 01:59.290
on possess.

01:59.290 --> 02:03.670
So quite early on it knows whether it's a ranged attack or not.

02:03.670 --> 02:06.880
We're going to handle the melee attacker situation first.

02:06.880 --> 02:11.080
And we're going to want to create a blueprint based on our behavior tree task.

02:11.080 --> 02:21.640
So we're going to right click create a new blueprint class, search for BT task and get BT Task Blueprint

02:21.640 --> 02:22.420
Base.

02:22.420 --> 02:26.680
We'll select that and we're going to call this BT.

02:28.400 --> 02:33.050
Attack and we have our blueprint, task, attack.

02:33.050 --> 02:39.860
And of course we can override functions such as receive, execute I so we can receive that.

02:39.860 --> 02:46.520
And the most important thing that we have to be concerned with here is that we finish execute so that

02:46.520 --> 02:50.480
our behavior tree doesn't get hung up on this task.

02:50.480 --> 02:53.240
The task has to return success or failure.

02:53.240 --> 02:58.730
And before we return that success or failure, we'll be stuck inside this task.

02:58.730 --> 03:02.360
So the way to return that is to use finish, execute.

03:02.570 --> 03:05.000
And this returns success or failure.

03:05.000 --> 03:06.110
I'm going to check that.

03:06.110 --> 03:08.690
So it returns success by default.

03:08.690 --> 03:12.950
And we're going to go ahead and just drag in the execution pin here.

03:13.070 --> 03:15.560
So our task does nothing.

03:15.560 --> 03:18.140
But we're going to do things with it definitely.

03:18.140 --> 03:25.610
For now though, let's go back to our behavior tree and drag off of our sequence and expand tasks.

03:25.610 --> 03:27.740
And there's our BT attack.

03:27.740 --> 03:31.400
So let's use our blueprint BT attack.

03:31.400 --> 03:32.360
And there it is.

03:32.390 --> 03:36.050
And if we click on it we see that it has a details panel.

03:36.050 --> 03:38.060
We can rename the node here.

03:38.060 --> 03:43.940
And of course we have nothing really else unless we go in here and add things.

03:43.940 --> 03:45.830
Now we have the owner controller.

03:45.830 --> 03:48.080
We also have controlled pawn.

03:48.080 --> 03:53.630
Now I'm going to drag off of the controlled pawn and use get object name.

03:56.860 --> 03:59.950
And we're going to print the name of this controlled pawn.

03:59.950 --> 04:01.330
So print string.

04:01.840 --> 04:04.750
We're going to run that in and finish execute.

04:04.750 --> 04:08.620
Let's just see what that controlled pawn is.

04:09.930 --> 04:15.750
So if we're close enough to attack, we should see that BT task attack.

04:15.750 --> 04:17.310
Let's go ahead and press play.

04:17.310 --> 04:18.930
Here come our enemies.

04:18.930 --> 04:21.720
Let's see if we get anything printed to the screen.

04:21.720 --> 04:29.280
And here we see BP Goblin spear were basically spamming the window with those messages.

04:29.610 --> 04:36.180
So as long as the enemy is close enough to attack and it's a melee attacker, we're getting some messages

04:36.180 --> 04:40.320
and we know that the controlled pawn is set to that actual pawn.

04:40.320 --> 04:43.140
Let's change this to owner controller.

04:45.080 --> 04:48.170
And just see what we get printed to the screen in this case.

04:49.980 --> 04:54.270
And as soon as they're close, we see our AI controller.

04:54.270 --> 05:00.630
So we know that in Bot Attack, we have access to the pawn and the controller here in event receive

05:00.630 --> 05:01.680
execute AI.

05:01.680 --> 05:02.820
And that's looking good.

05:02.820 --> 05:09.750
So now that we have a behavior tree task for attacking, let's show something visual here in the world.

05:09.750 --> 05:13.590
Say we'll draw a debug sphere in the place of the enemy.

05:13.590 --> 05:15.720
So in the place of our controlled pawn.

05:15.840 --> 05:20.550
So I'm going to delete those nodes and do a draw debug sphere.

05:23.170 --> 05:26.050
The center will be at our controlled pawns location.

05:26.050 --> 05:28.240
So I'm going to use get actor location.

05:29.580 --> 05:33.990
Drag that in for the center and we'll make the line color red.

05:35.370 --> 05:38.670
Will make the duration three seconds.

05:38.700 --> 05:41.490
The radius can be 40.

05:42.030 --> 05:44.250
And we'll go ahead and hook this up to finish.

05:44.250 --> 05:45.360
Execute.

05:45.360 --> 05:48.480
And let's get those debug spheres.

05:49.080 --> 05:50.130
There they are.

05:56.100 --> 06:00.300
So now we have some visual feedback that will tell us when we're attacking.

06:00.860 --> 06:08.000
Now if we're close enough to attack, which is a distance of less than 500, I'm going to want a little

06:08.000 --> 06:13.400
bit more sophisticated AI behavior than just execute this attack.

06:13.400 --> 06:19.040
So far, attack does nothing but draw a debug sphere, but we're going to make a gameplay ability to

06:19.040 --> 06:20.300
execute for that.

06:20.300 --> 06:26.780
But until then, I'd like our enemy to get close enough to quote and unquote attack.

06:26.780 --> 06:33.920
And what that means is I'd like our enemy to move in closer, because 500 is not that far, really.

06:33.920 --> 06:40.040
These enemies are coming in close because we're not aborting this other move to target here.

06:40.040 --> 06:46.070
You see, if I'm far enough to approach, we're seeing if the distance is greater than or equal to 500.

06:46.190 --> 06:53.690
If we were to abort this branch, as soon as this is no longer the case, then we should not reach closer

06:53.690 --> 06:54.650
than 500.

06:54.650 --> 06:55.670
So we can take this.

06:55.670 --> 06:59.780
Am I far enough to approach and set this to abort?

06:59.780 --> 07:08.330
Self, for example, and notice that this entire thing, this entire branch has green and it says nodes

07:08.330 --> 07:10.190
aborted by mode itself.

07:10.190 --> 07:15.980
And that's because if we abort this self branch, then whatever tasks are running here, those will

07:15.980 --> 07:19.730
be aborted and we'll return back up the tree.

07:19.730 --> 07:22.460
So if I go ahead and press play now.

07:24.080 --> 07:25.280
And let these guys run.

07:25.280 --> 07:28.550
You see that they stop 500 units away.

07:28.640 --> 07:30.980
So I think that's what I'd like.

07:30.980 --> 07:36.470
I'd like this to only handle running up to 500 units away.

07:36.470 --> 07:42.740
And once we're close enough to attack here, then I'd like our melee attackers to move in.

07:42.740 --> 07:44.750
We'll get to the ranged attackers later.

07:44.750 --> 07:49.610
They're going to hang back a little farther so they can launch their projectiles and such.

07:49.610 --> 07:52.790
But our melee attackers, they're going to move in close.

07:52.790 --> 07:56.180
So I'm going to drag out and use a move to task.

07:56.810 --> 07:58.100
Let's get our move two.

07:58.130 --> 08:00.590
We're going to move to our target to follow.

08:00.590 --> 08:03.830
So we're going to set the blackboard key here to target to follow.

08:04.130 --> 08:08.600
This is going to have an acceptance radius five is a little bit close.

08:08.600 --> 08:10.460
So I'm going to bring it up to about 20.

08:10.460 --> 08:14.330
And we can rename this node by changing the node name.

08:14.330 --> 08:16.220
We can call this get close.

08:16.220 --> 08:19.400
So this task gets us in a little closer.

08:19.400 --> 08:21.920
And then we'll execute it attack.

08:21.920 --> 08:27.140
And with BT attack selected I'm going to actually rename this node to just attack.

08:27.140 --> 08:34.340
So being able to rename the nodes like this can really make our behavior tree a lot more easy to read

08:34.340 --> 08:35.420
at first glance.

08:35.420 --> 08:38.120
So now our melee attackers are going to get closer.

08:38.120 --> 08:39.740
Let's see how this works.

08:40.600 --> 08:41.800
Here they come.

08:42.370 --> 08:43.330
And look at that.

08:43.330 --> 08:45.520
Our melee attackers are getting close.

08:46.870 --> 08:48.280
Those two back there.

08:48.310 --> 08:50.080
They are ranged attackers.

08:50.080 --> 08:53.020
So our melee attacker came in close.

08:53.020 --> 08:55.840
The ranged attackers stayed back a bit.

08:55.870 --> 09:00.130
Now if that distance gets farther than what was it, 4000?

09:00.130 --> 09:01.000
Something like that.

09:01.000 --> 09:03.670
Well then our ranged attackers stay in.

09:03.670 --> 09:04.960
The melee attacker comes.

09:04.960 --> 09:05.470
Now.

09:05.470 --> 09:12.820
If they get in each other's way, they don't create holes in the navmesh, so they're not going to create

09:12.820 --> 09:14.650
path points around each other.

09:14.650 --> 09:22.450
If I press P to show our nav mesh bounds volume, notice that these little guys are not making holes

09:22.450 --> 09:23.410
in the nav mesh.

09:23.410 --> 09:31.090
So if these two form a wall and this guy tries to move through it, it's not going to be smart enough

09:31.090 --> 09:32.530
to avoid them.

09:32.890 --> 09:39.760
There are ways to have them avoid each other, but by default the way it is now, they're not going

09:39.760 --> 09:40.720
to avoid each other.

09:40.720 --> 09:47.230
But for now, it's enough that our ranged attackers hang back and our melee attacker comes in, and

09:47.230 --> 09:51.010
when it gets close enough, it hits that acceptance radius.

09:51.010 --> 09:51.790
It attacks.

09:51.790 --> 09:55.000
And we know because we see that red sphere.

09:55.390 --> 09:58.330
So that is a good stopping point.

09:58.330 --> 10:04.210
I think we are one step closer to having our AI behave as they should.

10:04.210 --> 10:06.550
We're taking care of our ranged attackers.

10:06.550 --> 10:13.150
They just hang back and our melee attackers, they decide to get close and they attack.

10:13.600 --> 10:20.560
Now after our attack, we're going to want to decide what to do there and we'll handle that next.

10:20.560 --> 10:25.270
And of course, we're going to need to actually make these little guys attack.

10:25.270 --> 10:30.910
So we have a few steps left to go and we'll continue with this in the videos to come.

10:30.910 --> 10:33.340
So great job and I'll see you soon.
