WEBVTT

00:02.640 --> 00:08.340
Before we start writing our code, let's just remind ourselves what we're aiming to build.

00:08.340 --> 00:11.690
And that's basically this tree structure here.

00:11.700 --> 00:19.260
So our behavior tree is made up of a whole bunch of nodes, and a behavior tree is just a graph where

00:19.260 --> 00:26.880
each box, I guess is a node which has some action or condition or whatever associated with it.

00:26.880 --> 00:30.300
And the lines show which ones are connected to which.

00:30.300 --> 00:38.730
Now, if you remember, the ones right at the bottom are the actual actions or things that will happen

00:38.730 --> 00:44.880
and that will make your character do it's behavior that you will see on the screen.

00:44.910 --> 00:47.520
The other ones link it all together.

00:47.520 --> 00:52.650
So there's two type of leaf nodes is an action and a condition, and we'll get to those a little bit

00:52.650 --> 00:53.640
later on.

00:53.820 --> 00:58.980
Then we have our sequence and our selectors, which will also write a little later on.

00:58.980 --> 01:07.980
But all of these are basically nodes and so a node will be our base class that we will implement.

01:08.370 --> 01:17.610
And when we start writing this, you will be absolutely surprised at just how simple the code is and

01:17.610 --> 01:20.460
yet how powerful it becomes.

01:21.270 --> 01:25.890
Okay, so let's go ahead and write our Node class.

01:25.890 --> 01:29.040
Now, I will put all of my script into a new folder.

01:29.220 --> 01:35.010
There's already a B tree folder that you'll find when you've brought in that project, but that has

01:35.010 --> 01:36.990
the NAV mission, so we don't want to use that.

01:36.990 --> 01:40.470
So let's just go down outside of everything.

01:40.470 --> 01:42.150
So I don't create it inside something else.

01:42.150 --> 01:50.010
And let's call this behavior tree, or you could call it script if you want to put it in there, let's

01:50.010 --> 01:50.790
open this up.

01:50.790 --> 01:51.180
Okay.

01:51.240 --> 01:56.970
So in here, we're going to create a new C-sharp file and we're going to call it Node.

01:56.970 --> 01:59.610
Then you want to open that up in your editor.

01:59.610 --> 02:04.530
Now, as a very basic class, it's not going to be a mono behavior.

02:04.530 --> 02:09.120
In fact, all of these classes won't work on mono behavior.

02:09.120 --> 02:12.870
So we have to design them in a way that they will update themselves.

02:12.870 --> 02:20.400
But because it's in a tree structure, it's going to run from the top root node of the tree, and then

02:20.400 --> 02:26.430
the whole structure will look down to find the first available leaf that should be run and then run

02:26.430 --> 02:27.150
that.

02:27.150 --> 02:30.090
So let's get rid of Start and also update.

02:30.090 --> 02:37.500
Now any particular leaf or any node actually can have one of three statuses.

02:37.500 --> 02:43.800
It can have that it was successful when you attempted to run it, that it failed when you attempted

02:43.800 --> 02:50.220
to run it, or that it could be in a state of running, which means it's still being executed.

02:50.220 --> 02:50.570
Okay.

02:50.640 --> 03:00.960
So we're going to create a public enum for that and we'll call it status and we've got three so successful

03:00.990 --> 03:10.380
or success running and then failure, and then we need a value to actually store that in so that we

03:10.380 --> 03:13.020
can record which one we achieved.

03:13.020 --> 03:18.000
So public status and then we'll just call that status.

03:18.630 --> 03:19.080
Okay.

03:19.080 --> 03:24.780
Now each node can possibly have children nodes.

03:24.780 --> 03:24.980
Okay.

03:25.050 --> 03:32.640
So if it's the root node, it has the nodes immediately below it as its children, the selector and

03:32.640 --> 03:38.190
the sequence nodes, they have actions and conditions as they're children.

03:38.190 --> 03:41.550
So basically every node can have a set of children.

03:41.550 --> 03:45.840
Of course the leaf nodes are going to have an empty set of children, but that's okay.

03:45.840 --> 03:47.100
We're just going to store them in here.

03:47.100 --> 03:52.110
So I'm going to create a public list to store those in four nodes.

03:52.110 --> 04:05.700
So a list of nodes, children, and we will make that equal new list node like that.

04:05.970 --> 04:06.510
Okay.

04:06.510 --> 04:16.920
Now every time the node has to find out the status of any of its children, it's going to run through

04:16.920 --> 04:23.160
them basically from left to right, as though it's reading one after the other, especially if you're

04:23.160 --> 04:29.520
in a sequence, for example, and you've got three things that you want to do in sequence.

04:29.520 --> 04:34.380
You want to have them put into the list in order that they will run.

04:34.500 --> 04:40.920
And then you need the node to actually keep track of which one it's running at any particular time.

04:40.920 --> 04:48.090
So we'll create a public int and we'll call that the current child and we'll set that to zero.

04:48.330 --> 04:54.270
So right at the beginning it's going to be looking at its very first child notice of one.

04:54.270 --> 04:56.640
It's going to run first.

04:57.210 --> 04:57.720
Okay.

04:57.720 --> 05:01.890
Now, to make your own debugging life a little easier, we'll.

05:02.000 --> 05:04.820
Also give each note a name.

05:04.910 --> 05:09.410
And this is just so that we can print it out and see what our tree looks like.

05:09.590 --> 05:14.270
So public string name like that.

05:14.270 --> 05:17.090
Then we're going to create a constructor.

05:17.090 --> 05:29.810
So public node and we'll put string in and in here we'll put name equals and and we will also make a

05:29.810 --> 05:36.710
constructor without anything in it because C-sharp likes to do that.

05:36.710 --> 05:43.520
Now in this case you'll end up with name is practically empty, but we will use this one here instead.

05:43.520 --> 05:50.060
Now, the other thing I want to do before we just finish up with this for now is add in a public method

05:50.060 --> 05:51.320
for adding our child.

05:51.320 --> 06:02.540
So public void add child, we're going to pass it a node n and then inside of here we will just go children

06:02.870 --> 06:07.220
dot add and we'll add n to it.

06:07.220 --> 06:11.300
So the first one that you put in to the list will be at the top of the list.

06:11.300 --> 06:13.220
So that will be child zero.

06:13.250 --> 06:16.940
And then you'll get child one, child two as you go through that list.

06:16.940 --> 06:22.640
And therefore you have to put them into the list in order that they appear or that you want them to

06:22.640 --> 06:25.820
execute within your behavior.

06:25.820 --> 06:28.370
TREE okay, so we've got that.

06:28.370 --> 06:30.290
Let's now save it.

06:30.830 --> 06:32.720
Let's go back into unity.

06:32.720 --> 06:37.580
We've got nothing to run at this point, but we want to get it to a point where we can actually create

06:37.580 --> 06:38.540
a tree.

06:38.660 --> 06:43.730
We're going to create a script for our behavior tree.

06:43.730 --> 06:48.950
So let's call this behavior tree.

06:48.950 --> 06:52.820
Now I'm using the Australian spelling of behavior.

06:52.970 --> 06:54.560
It's up to you which one you use.

06:54.560 --> 06:56.480
Let's open that up in our editor.

06:56.480 --> 07:01.460
And once again, we'll we'll get rid of mono behavior, but we'll actually say that our behavior tree

07:01.460 --> 07:08.120
is basically a node because it is it's the root node and has children underneath it, which then have

07:08.120 --> 07:08.780
their own children.

07:08.780 --> 07:09.830
They're in their own children.

07:09.830 --> 07:15.620
So public behavior tree.

07:15.830 --> 07:22.520
Now, of course, you don't even really need this particular class because you could just create a node

07:22.520 --> 07:24.410
and call it root and then use that.

07:24.410 --> 07:30.140
But just for some kind of completeness sake here, I'm just going to make sure that I distinguish a

07:30.140 --> 07:32.060
behavior tree or something different.

07:32.060 --> 07:35.780
So let's call this name equals tree.

07:35.780 --> 07:43.460
And of course you could pass it through a particular type of behavior if you wanted a sort of a guard

07:43.460 --> 07:45.890
and PC behavior for what it should do.

07:45.890 --> 07:49.190
It could have a particular behavior tree and then you could have another behavior tree.

07:49.190 --> 07:55.100
So this this class here could be expanded in the future to kind of read in a behavior tree that you've

07:55.100 --> 08:04.190
written up as some kind of JSON file or XML file or even just text to load in the actual behaviors.

08:04.310 --> 08:08.270
We won't be doing that here, but that's the kind of thing you could do with this.

08:08.270 --> 08:19.850
And then let's put in public behavior tree and we will pass through a string in and set up the name

08:19.850 --> 08:20.480
in here.

08:23.010 --> 08:23.500
Okay.

08:23.520 --> 08:24.760
So let's save that.

08:24.780 --> 08:29.400
Now let's go back to Unity and we will create another script.

08:29.400 --> 08:34.230
And this will be the script that we actually put on our thief, our robber object.

08:34.230 --> 08:41.010
So let's create C-sharp script and we will call this robber behavior.

08:41.550 --> 08:42.660
Open that up.

08:42.660 --> 08:49.980
And this time we will keep it as a behavior, because this is actually going to go on to our agent and

08:49.980 --> 08:51.660
it's going to run.

08:51.660 --> 08:58.320
So up here, the first thing we'll do is actually create a behavior tree so we can go behavior tree,

08:58.320 --> 08:59.640
let's call it tree.

09:00.360 --> 09:07.410
And then in the start, we're going to construct a very, very simple tree.

09:07.650 --> 09:15.450
So tree equals new behavior tree, and I'll just let it be called tree at this point.

09:15.450 --> 09:25.110
Then we create a node and the node under that is going to be steel, and that's going to equal new node

09:25.620 --> 09:28.620
steel, something we'll call it.

09:29.700 --> 09:36.900
And then what's entailed in stealing something is, well, we can steal the diamond, for example.

09:36.900 --> 09:51.780
So we'll create a node for go to diamond and we'll equal new node, go to diamond, and then another

09:51.780 --> 09:58.200
node go to Van, which is the little truck that's in the scene there.

09:58.200 --> 10:02.850
So we're going to get our agent to move to where the diamond is and then move back to the van.

10:02.850 --> 10:05.580
So that's going to equal new node.

10:06.240 --> 10:08.880
Go to van.

10:10.530 --> 10:12.810
Now, how do we construct this tree?

10:12.840 --> 10:21.130
We've got to kind of do it in reverse so our steel does not add child first.

10:21.150 --> 10:33.300
We're going to add in, go to diamond and then Steel Dot, add child, go to van and then we get our

10:33.300 --> 10:34.140
tree.

10:34.380 --> 10:37.470
Dot add child for steel.

10:38.670 --> 10:46.380
So at this point, our tree should look very simple and as it does on the screen here.

10:46.920 --> 10:49.050
How do we know that this is correct?

10:49.080 --> 10:49.380
Right.

10:49.380 --> 10:50.790
Well, I'm just going to save this.

10:50.820 --> 10:57.420
I'm going to go back into our behavior tree and create a little print tree method in here so that we

10:57.420 --> 10:59.520
can see something in the console.

10:59.800 --> 11:00.090
Right.

11:00.090 --> 11:05.310
So that'll be a public void print tree.

11:06.840 --> 11:09.390
And then inside of here.

11:09.720 --> 11:13.440
Well, you didn't think I was going to make it that easy for you, did you?

11:13.470 --> 11:17.770
This is going to be a challenge for you to come up with the code that's going to go in this print tree.

11:17.790 --> 11:25.740
Now you'll want to use debug logs because we want this just to print onto the console at any time that

11:25.740 --> 11:29.700
we call it from inside the rubber behavior code or wherever.

11:29.700 --> 11:36.420
So all you've got to do is kind of figure out how are you going to traverse through the tree in the

11:36.420 --> 11:40.050
right order and display it in the console?

11:40.410 --> 11:41.510
Give that a go.

11:41.520 --> 11:49.710
When we come back in the next video, we will complete this code and continue writing the code that

11:49.710 --> 11:54.570
will actually get our little rubber agent moving on the screen.
