WEBVTT

00:06.870 --> 00:08.340
Welcome back.

00:08.610 --> 00:15.420
So our goal here is to get our enemies moving around and being active and being controlled by AI.

00:15.750 --> 00:19.980
Now we know that there are currently associated with a behavior tree.

00:19.980 --> 00:23.220
If we go to our blueprints folder, go to AI.

00:23.250 --> 00:25.020
We have a behavior tree.

00:25.050 --> 00:28.350
Now the behavior tree has a root node.

00:28.350 --> 00:32.370
And from the root node we can attach something.

00:32.370 --> 00:37.020
We can attach children to nodes in our behavior tree.

00:37.260 --> 00:42.750
Now one of the fundamental nodes that you can have in the behavior tree is the selector node.

00:43.170 --> 00:50.460
A selector node has the very specific quality that it will execute its children from left to right.

00:50.460 --> 00:56.610
And by children I mean I can drag off of the bottom of my selector and put nodes.

00:56.820 --> 00:58.200
We know what tasks do.

00:58.230 --> 01:06.180
We've seen the play animation task node, and there are a number of other built in tasks such as wait

01:06.510 --> 01:10.320
and other things such as move to.

01:10.350 --> 01:14.520
So these are tasks which perform some kind of action.

01:14.520 --> 01:20.850
Now when we have things connected to the bottom of a selector, we say that these nodes are children

01:20.850 --> 01:27.390
of the selector and the selector will execute them left to right, one at a time.

01:27.570 --> 01:32.430
Now it doesn't just do this all at the same time, but one after the other.

01:32.430 --> 01:36.960
In other words, it's going to use this play animation node and it's going to stop.

01:36.960 --> 01:42.720
It'll be held up here until the play animation node finishes, at which point it will then go to the

01:42.720 --> 01:43.860
next node.

01:43.860 --> 01:47.910
And once that node has finished, it will then go to the next node.

01:48.120 --> 01:54.960
Now, nodes in a behavior tree have the concept of success or failure, and whether a node succeeds

01:54.960 --> 01:59.670
or fails depends on the node and how it defines success or failure.

01:59.700 --> 02:07.200
Now, if a node succeeds or fails, it returns that success or failure information back to its parent,

02:07.200 --> 02:11.250
and the parent will then decide what to do with that information.

02:11.460 --> 02:18.630
A selector specifically has the quality that it will execute its children, one after the other, until

02:18.630 --> 02:24.930
one of its children succeeds, at which point it will then return success up to its parent.

02:25.470 --> 02:30.240
Now, once success returns to the root, then the whole tree starts over.

02:30.240 --> 02:36.690
So a selector with three children like this will execute its first child first.

02:36.690 --> 02:40.650
And if that child succeeds, it'll never get to the other children.

02:40.650 --> 02:47.550
And once it returns success to the root and starts over, if it succeeds again, then once again, we

02:47.550 --> 02:50.640
will never try to execute the other two child nodes.

02:50.640 --> 02:55.830
It will only ever keep executing the first node as long as it keeps succeeding.

02:55.860 --> 03:03.030
As soon as it fails though, then the selector will try to execute the next of its child nodes and continue

03:03.030 --> 03:04.170
the process from there.

03:04.170 --> 03:06.720
So that's the behavior of a selector.

03:07.080 --> 03:10.740
Now selectors can have things attached to them.

03:10.740 --> 03:18.720
We call a selector a composite node, and we can attach certain types of nodes to composite nodes and

03:18.720 --> 03:19.440
tasks.

03:19.440 --> 03:25.560
One of these types of nodes that I'd like to attach to my selector is called a service.

03:25.650 --> 03:32.130
Now I can right click on my selector and notice that we can add decorators and services.

03:32.130 --> 03:38.460
Decorators can be attached to nodes, and services can be attached to nodes.

03:38.460 --> 03:42.420
Notice that there are a number of different types of decorators.

03:42.720 --> 03:45.540
And we'll get to decorators in more detail later.

03:45.540 --> 03:53.400
But I'd like to add a service and a service once attached to a node, such as a selector will execute

03:53.400 --> 03:58.800
at its defined frequency as long as the branch is being executed.

03:58.800 --> 04:05.580
Now this selector is considered a branch, and even if it has multiple children connected to it, we

04:05.580 --> 04:09.330
consider the selector and all of its children to be a branch.

04:09.330 --> 04:17.130
If this branch is being executed, then any services attached to it will be executed and services execute

04:17.130 --> 04:19.410
at a frequency that we specify.

04:19.410 --> 04:26.340
They have their own tick functionality, but generally in behavior trees we don't often have things

04:26.340 --> 04:28.470
tick every single frame of the game.

04:28.470 --> 04:30.300
Like your typical tick function.

04:30.300 --> 04:34.680
We tick every so often, every half second, every quarter second.

04:34.680 --> 04:38.880
We specify how often our services can tick.

04:38.880 --> 04:45.630
Now, if I wanted to attach a service on here, I could or I can create my own.

04:45.630 --> 04:52.230
And that's what I'd like to do, is create my own service that will do exactly what I tell it to do

04:52.230 --> 04:53.820
every time it ticks.

04:53.820 --> 04:56.910
And for that, we need a new class.

04:57.510 --> 05:03.870
Hey there, it's Steven from the future here, and I'm coming back to let you know about a mistake that

05:03.870 --> 05:05.490
I made in the course.

05:05.490 --> 05:06.360
And how.

05:06.520 --> 05:08.620
How we're going to rectify it.

05:08.620 --> 05:17.860
You see, in the original version of this video, we created a new class for our behavior tree service,

05:17.860 --> 05:20.140
and we created that class in C plus.

05:20.140 --> 05:20.890
Plus.

05:20.890 --> 05:29.260
The class we created was based on UT Service Blueprint Base, which is designed to be subclassed in

05:29.260 --> 05:30.460
blueprint only.

05:30.460 --> 05:32.170
We can see the comment here.

05:32.170 --> 05:34.960
Base class for blueprint based service nodes.

05:34.960 --> 05:38.290
Do not use it for creating native C plus plus classes.

05:38.290 --> 05:44.440
This is the no no that I actually did in the course, and it's kind of silly.

05:44.440 --> 05:46.450
The class is called Blueprint Base.

05:46.450 --> 05:54.040
I don't know why I didn't snap there, but I created the class in C plus plus and this class is made

05:54.040 --> 05:56.380
to be Subclassed in blueprint.

05:56.380 --> 05:58.990
That's the appropriate way to subclass this class.

05:58.990 --> 06:00.880
That's how we're actually going to do it.

06:00.880 --> 06:04.720
But the original version of the course created it in C plus.

06:04.720 --> 06:10.270
Plus it still worked, but it just wasn't using the class the way it was designed to be used, which

06:10.270 --> 06:15.310
is as a blueprint rather than a native C plus plus class.

06:15.310 --> 06:21.550
So if you go and look at the GitHub commit for this lecture, you're going to see this behavior tree

06:21.550 --> 06:26.890
service C plus plus class based on UT Service Blueprint base.

06:26.890 --> 06:33.520
You can safely ignore that because we're going to be creating it in blueprint as is intended.

06:33.520 --> 06:41.110
So completely ignore the GitHub commit for this lecture as we're not going to create this class in C

06:41.110 --> 06:41.920
plus plus.

06:41.920 --> 06:44.200
Instead we're going to create it in blueprint.

06:44.200 --> 06:52.330
So we can go into our I folder and create our new behavior tree task directly.

06:52.330 --> 06:53.710
Here we can right click.

06:53.710 --> 06:55.960
We can create a new blueprint class.

06:55.960 --> 07:03.640
And we're going to open all classes and search for BT Service Blueprint Base.

07:03.640 --> 07:06.880
This is what we're going to create a blueprint based on.

07:06.880 --> 07:08.500
So let's make this blueprint.

07:08.500 --> 07:13.630
And we're going to prefix its name with Pts for behavior tree service.

07:13.630 --> 07:21.340
And I'd like to call this Find nearest player because I want this service to do just that.

07:21.340 --> 07:25.060
Find the nearest player to the I pawn.

07:25.060 --> 07:31.480
So let's open pts, find nearest player and take a look at our behavior tree service.

07:31.720 --> 07:39.280
Now a behavior tree service as we mentioned, is designed to perform some kind of action at a regular

07:39.280 --> 07:40.210
interval.

07:40.210 --> 07:43.300
In fact our service has an interval parameter.

07:43.300 --> 07:50.380
By default it's set to 0.5 seconds, which means every half second this service will tick.

07:50.380 --> 07:54.340
We can even add a random deviation to the service.

07:54.340 --> 07:58.810
Now our details panel has a couple other things that we'll briefly mention.

07:58.810 --> 08:02.020
One is the node name which we can customize.

08:02.020 --> 08:11.110
I'm just going to fill this in with find nearest player and a custom description which is optional.

08:11.290 --> 08:15.190
We could say finds nearest player if we like.

08:15.190 --> 08:20.650
And we can attach our custom service in our behavior tree to our selector.

08:20.650 --> 08:24.820
We can right click Add Service and there's our Pts.

08:24.820 --> 08:31.090
Find nearest player and notice that its name is just find nearest player because that's what I set it

08:31.090 --> 08:33.070
to for the node name.

08:33.310 --> 08:36.730
So now we have this attached to our selector.

08:36.730 --> 08:43.480
And it's going to tick according to its interval which by the way we can select find nearest player

08:43.480 --> 08:44.950
and change its interval.

08:44.950 --> 08:47.530
It's exposed to the details panel.

08:47.530 --> 08:49.420
I'm going to leave it at 0.5.

08:49.690 --> 08:57.100
And the way we give this ticking functionality is by overriding one of its overridable functions.

08:57.100 --> 09:05.050
We're interested in receive tick I if we choose that we have access to the owner controller, the controlled

09:05.050 --> 09:12.070
pawn, even delta seconds, and we can do something at this services interval.

09:12.070 --> 09:15.280
All I'd like to do for now is do a print string.

09:15.280 --> 09:16.570
We can print hello.

09:16.570 --> 09:22.690
Or we could even get the owner controller call get object name on it if we'd like to print the name

09:22.690 --> 09:24.520
of the owner controller.

09:24.520 --> 09:25.660
Let's see what happens.

09:25.660 --> 09:32.080
If we compile this and go into our game and press play, we can see that tick happening.

09:32.080 --> 09:40.000
And all three of the I pawns are controlled by the controllers that we have printed to the screen there,

09:40.000 --> 09:42.760
and we also have access to the controlled pawn.

09:42.760 --> 09:49.900
We could print the object name for the controlled pawn as well, and just see that those are the corresponding

09:49.900 --> 09:53.290
pawns that control these behavior trees.

09:53.290 --> 09:58.390
So as you can see, we have created our behavior tree service.

09:58.390 --> 10:02.590
We see that these services are ticking for each of our I pawns.

10:02.590 --> 10:05.830
It's quite easy to attach a service to.

10:06.130 --> 10:11.560
One of our selector nodes as we've done, but all we're doing is printing a message to the screen.

10:11.560 --> 10:15.760
What we'd like to do is find the nearest player.

10:15.760 --> 10:21.880
In other words, where's aura, how close is aura, etc. so for that, we'll start setting things up

10:21.880 --> 10:23.350
in the next video.

10:23.380 --> 10:25.270
Great job and I'll see you soon.
