WEBVTT

00:02.830 --> 00:03.460
Behavior.

00:03.460 --> 00:07.670
Trees are an er technique used to control the behavior of NPCs.

00:07.690 --> 00:11.710
They became popular after use for the NPCs in Halo.

00:12.100 --> 00:14.870
They are a special kind of graph called A Tree.

00:14.890 --> 00:21.010
As the name would suggest, its structure begins with a single root and extends out from that adding

00:21.010 --> 00:24.310
branches, nodes and finally end nodes.

00:24.490 --> 00:29.560
There are three types of nodes composite, decorator and leaf.

00:29.860 --> 00:34.270
A composite node is one that can have one or more children nodes.

00:34.360 --> 00:37.600
A decorator node can have only one child.

00:38.020 --> 00:44.920
A leaf node is a node at the end of a branch and corresponds to an actual action in a game.

00:45.040 --> 00:52.210
A group of leaf nodes at the end of a branch may represent a sequence of actions or a set of actions

00:52.210 --> 00:54.250
to select from how they operate.

00:54.250 --> 01:00.250
Depends on the type of composite node of which there are two types sequences and selectors.

01:00.250 --> 01:05.710
When processed, a sequence node will ensure that each child is visited in order.

01:05.740 --> 01:11.590
A selector node, however, indicates that only one of its children need to be visited and that child

01:11.590 --> 01:13.210
can be picked at random.

01:13.780 --> 01:15.540
Let's take a look at an example.

01:15.550 --> 01:18.690
Imagine you or your NPC are hungry.

01:18.700 --> 01:21.250
You have a decision to make about what to eat.

01:21.280 --> 01:27.010
This could be shown using a selector node in which you can select from just one of the children.

01:27.190 --> 01:29.350
You don't need all of them.

01:29.650 --> 01:36.490
So this is when a selector comes into play, when just one leaf node needs to be executed to perform

01:36.490 --> 01:37.930
the behavior of eat.

01:37.930 --> 01:44.350
However, if your NPC's actions were at a lower level than this, such as in The Sims, then you could

01:44.350 --> 01:48.580
associate the eating of each item with a well-defined process.

01:48.730 --> 01:53.020
In this case, you'd need a sequence node for simplicity.

01:53.020 --> 02:00.640
Let's say such a sequence includes gather ingredients, make pizza, cook pizza, and eat pizza.

02:00.670 --> 02:04.660
Notice each leaf node corresponds to an actual action.

02:04.660 --> 02:10.240
It's important that the leaf nodes can be performed with a logically or with animation because these

02:10.240 --> 02:14.410
are the actual things you will see the NPC doing on the screen.

02:14.410 --> 02:16.390
They are also self contained.

02:16.660 --> 02:20.620
This means they can be performed and then the sequence abandoned.

02:21.220 --> 02:28.020
Nodes can also represent decisions that when in a sequence can cause a sequence to continue or fail.

02:28.030 --> 02:35.950
In this case, the behavior tree will execute completely only if my hungry and do I have food is satisfied

02:35.950 --> 02:40.650
at which the decision to eat, burger or eat pizza will be satisfied.

02:40.660 --> 02:46.930
Behavior trees can be as simple or as complex as you need, including actions at all levels, joined

02:46.930 --> 02:48.820
by different composite nodes.

02:49.240 --> 02:51.230
So how does all this work in code?

02:51.250 --> 02:56.080
Well, each leaf node must have a matching method that can be executed.

02:56.500 --> 02:59.590
The behavior tree itself is a kind of scripting language.

02:59.590 --> 03:05.740
Sitting above your code base, you must write a bunch of self-contained modules in a C-sharp file that

03:05.740 --> 03:08.650
can be called when a leaf node is being run.

03:09.620 --> 03:14.750
The method itself will have to report whether or not it has failed or succeeded.

03:15.080 --> 03:21.680
This gives the leaf node a fail or success state, which is reported back to the running of the behaviour

03:21.680 --> 03:22.310
tree.

03:22.460 --> 03:25.670
In the case of a sequence, this is essential.

03:25.670 --> 03:29.300
As if one leaf fails, the entire sequence fails.

03:29.330 --> 03:35.540
In this section we're going to build the basic skeleton of a behavior tree system so that you can get

03:35.540 --> 03:42.650
to see what all the major code is going to look like and put together your own little simulation.

03:42.650 --> 03:49.940
So just in the same way that I did in the Hope series, the goal orientated action planning, I've created

03:49.940 --> 03:57.290
another little scenario here, and this scenario has an agent on a nav mesh again that I've built.

03:57.530 --> 04:04.940
The agent in this case is a robber, and he's going to go and rob something out of the little gallery

04:04.940 --> 04:07.250
that I've created over here.

04:07.310 --> 04:13.400
Now, if you bring in this data project that's attached to this lecture and open it up in unity, you're

04:13.400 --> 04:17.570
going to find this in the bee tree scene.

04:17.930 --> 04:20.600
The nav mesh will already be built.

04:20.600 --> 04:23.800
Now there's two door objects in here.

04:23.810 --> 04:30.140
They're not actually part of the nav mesh, but we will make them nav mesh obstacles a little bit later.

04:30.140 --> 04:34.580
So this is already set up and ready to go for you to use.

04:34.580 --> 04:41.000
So when we come back in the next lecture, we'll actually start writing some code to get our robber

04:41.000 --> 04:43.130
moving through the gallery.
