WEBVTT

0
00:10.240 --> 00:15.080
'Behavior Trees' are an A.I. technique used to control the behavior of NPCs.

1
00:15.080 --> 00:19.610
They became popular after use, for the NPCs in Halo.

2
00:19.610 --> 00:25.520
They are a special kind of graph called a 'tree' (as the name would suggest). Its structure begins with a single

3
00:25.520 --> 00:32.030
root and extends out from that, adding branches, nodes and finally end nodes.

4
00:32.030 --> 00:33.960
There are 3 types of nodes.

5
00:34.040 --> 00:42.230
Composite, Decorator and Leaf. A Composite node is one that can have "1 or more children nodes". A Decorator

6
00:42.260 --> 00:50.360
node can have "only 1 child". A Leaf node is a "node at the end of a branch" and corresponds to an actual

7
00:50.450 --> 00:57.740
action in a game. A group of leaf nodes at the end of a branch may represent a sequence of actions

8
00:58.010 --> 01:04.370
or a set of actions to select from. How they operate depends on the type of composite node, of which there

9
01:04.370 --> 01:11.510
are 2 types: Sequences and Selectors. When processed, a Sequence node will ensure that "each child is

10
01:11.510 --> 01:18.230
visited in order". A Selected node however indicates that "only 1 of its children need to be visited" and

11
01:18.230 --> 01:21.230
that child can be picked at random.

12
01:21.230 --> 01:25.970
Let's take a look an example. Imagine you (or your NPC) are hungry.

13
01:26.150 --> 01:28.750
You have a decision to make about what to eat.

14
01:28.760 --> 01:34.630
This could be shown using a Selector node in which you can select from just 1 of the children.

15
01:34.670 --> 01:37.160
You don't need all of them.

16
01:37.160 --> 01:43.760
So this is when a Selector comes into play, when just 1 leaf node needs to be executed to perform

17
01:43.820 --> 01:45.320
the behavior of 'eat'.

18
01:45.320 --> 01:52.340
However, if your NPCs actions were at a lower level than this, such as in The Sims, then you could associate

19
01:52.340 --> 01:56.270
the eating of each item with a well-defined process.

20
01:56.270 --> 01:59.010
In this case, you'd need a Sequence node.

21
01:59.360 --> 02:07.280
For simplicity, let's say such a sequence includes: Gather ingredients; Make pizza; Cook pizza and

22
02:07.280 --> 02:08.060
Eat pizza.

23
02:08.090 --> 02:12.160
Notice each leaf node corresponds to an actual action.

24
02:12.170 --> 02:17.540
It's important that the leaf nodes can be performed with logic or with animation because these

25
02:17.630 --> 02:21.770
are the actual things you will see the NPC doing on the screen.

26
02:21.770 --> 02:24.140
They are also self-contained.

27
02:24.140 --> 02:30.920
This means they can be performed and then the sequence abandoned. Nodes can also represent decisions

28
02:30.920 --> 02:35.360
that when, in a sequence, can cause a sequence to continue or fail.

29
02:35.390 --> 02:40.480
In this case, the Behavior Tree will execute completely only if 'Am I Hungry?'

30
02:40.550 --> 02:48.410
and 'Do I have food?' is satisfied, at which the decision to 'eat burger' or 'eat pizza' will be satisfied.

31
02:48.410 --> 02:54.800
Behavior Trees can be as simple or as complex as you need, including actions at all levels, joined by different

32
02:54.800 --> 02:56.690
composite nodes.

33
02:56.690 --> 02:58.520
So how does all this work in code?

34
02:58.700 --> 03:03.620
Well, each leaf node must have a matching method that can be executed.

35
03:03.950 --> 03:09.080
The Behavior Tree itself is a kind of scripting language sitting above your codebase.

36
03:09.110 --> 03:14.750
You must write a bunch of self-contained modules in a C# file that can be called when a leaf node

37
03:14.780 --> 03:16.930
is being run.

38
03:17.060 --> 03:22.550
The method itself will have to report whether or not it has failed or succeeded.

39
03:22.550 --> 03:28.960
This gives the leaf node a fail or success state, which is reported back to the running of the

40
03:28.970 --> 03:29.790
Behavior Tree.

41
03:29.990 --> 03:37.460
In the case of a sequence, this is essential, as if one leaf fails the entire sequence fails. In the remainder

42
03:37.490 --> 03:39.900
of our investigation into Behavior Trees,

43
03:39.920 --> 03:45.440
I'll have you follow along with more examples, as it will start to make more sense as you use them.

44
03:45.440 --> 03:51.650
For this, we will be using the freely available 'Panda BT' plug-in, which allows scripting of Behavior Trees

45
03:51.680 --> 03:53.060
in simple text files.