WEBVTT

00:03.160 --> 00:09.730
In the previous lecture we set up the basics of our behaviour tree and printed out the actual nodes

00:09.730 --> 00:11.560
and the structure that they come in.

00:11.590 --> 00:15.120
Now the nodes on the very bottom are the leaf nodes.

00:15.130 --> 00:21.010
They're the ones that are going to do the work and cause our agent, which is our robber in this case,

00:21.010 --> 00:25.330
to actually move around the map, which is simulating him actually doing something.

00:25.330 --> 00:31.450
So the first set of behaviours we've got is going to Diamond and then going back to the van.

00:31.600 --> 00:32.050
Okay.

00:32.050 --> 00:38.500
So first things first, our little agent down here, our robber, if you click on him in the hierarchy

00:38.500 --> 00:44.830
and have a look over in the inspector, you'll see that he already has a nav mesh agent on there.

00:44.830 --> 00:49.780
So we need to get hold of that nav mesh agent inside of the script.

00:49.900 --> 00:57.580
We also need to know in that script the location of the diamond, which is over in this part of the

00:57.580 --> 01:01.150
gallery and also the van which is over here.

01:01.330 --> 01:04.270
And those two objects are in the hierarchy, of course.

01:04.270 --> 01:08.860
So we've got them as well, and we can pass them through to the script for the robber.

01:08.860 --> 01:13.600
So let's just go into there and up the top here.

01:13.600 --> 01:15.310
We're going to pass those in.

01:15.310 --> 01:20.830
So we want a public game object and that will be our diamond.

01:21.940 --> 01:24.610
And then we want a public game object.

01:26.450 --> 01:27.380
Van.

01:27.680 --> 01:31.160
Now we also want to create our nav mesh agent here.

01:31.160 --> 01:37.250
So up the top let's go using unity engine dot I.

01:38.180 --> 01:42.590
And then down here we can actually create our agent.

01:42.590 --> 01:46.340
So it's a nav mesh agent agent.

01:46.910 --> 01:57.530
And to get hold of that just down in the start, we will go agent equals this dot get component nav

01:57.860 --> 01:59.960
mesh agent.

02:01.160 --> 02:05.870
Okay, so now we can actually direct him to go to the diamond.

02:05.870 --> 02:15.410
So if we put down in the start here, for example, agent set destination and pass through the diamond

02:15.680 --> 02:22.550
transformed position, he will then move there regardless of what the tree is doing.

02:22.550 --> 02:25.610
This is just testing our agent set up with our nav mesh.

02:25.610 --> 02:30.110
So just save that and we'll go into unity.

02:30.260 --> 02:32.060
Select the rubber.

02:32.090 --> 02:36.020
Now you're going to find Diamond and Van exposed in that script.

02:36.020 --> 02:41.510
And so you can grab hold of the diamond, which is actually attached to this object called plinth.

02:41.660 --> 02:44.660
And we will drag and drop that into diamond.

02:44.660 --> 02:50.540
And the van itself is up here, getaway van and drag and drop that there.

02:50.570 --> 02:50.840
Okay.

02:50.840 --> 02:58.220
Now when we press play, that agent set destination is going to make him move along our nav mesh.

02:58.220 --> 03:04.730
He's going to go through the nearest gap which is currently that door because it's actually only just

03:04.730 --> 03:08.600
an empty object without any particular collider or anything.

03:08.600 --> 03:10.610
And he goes over to the diamond.

03:10.610 --> 03:13.910
Okay, so we know that the nav mesh part is now working.

03:13.910 --> 03:18.140
We need to hook that all up with an actual action.

03:18.440 --> 03:23.240
So an action has a process associated with it.

03:23.240 --> 03:28.010
Let's go back into our code for the node and remind ourselves what's going on.

03:28.010 --> 03:35.420
We've set up this status so that every node is going to return success if it's finished and it was successful

03:35.420 --> 03:41.570
running if it's still running and failure if it's finished running and it's actually failed.

03:41.840 --> 03:46.400
So there are three things now, where do these actually get returned from?

03:46.400 --> 03:53.420
Well, they get returned from the process that happens within each node and depending on the type of

03:53.420 --> 03:56.300
node will depend on the type of process.

03:56.300 --> 04:05.750
So let's go down into here and I'm going to add in a new public virtual status process and this process

04:05.750 --> 04:10.910
here, which is basically just the node level, because we're going to override all of these, we will

04:10.910 --> 04:18.890
return status dot success as a default for that particular node.

04:18.920 --> 04:19.110
Okay.

04:19.190 --> 04:20.630
So just save that.

04:20.630 --> 04:26.720
Now we're going to go into our behavior tree and give it a process.

04:26.720 --> 04:32.780
So in the behavior tree, we're going to come in here and just above all this printing tree stuff,

04:33.830 --> 04:43.220
we will put in a public override status process like that.

04:43.220 --> 04:46.550
Now, we're not returning what the base does in this case.

04:46.550 --> 04:54.380
What we're going to actually return in this tree is whatever the child returns, because remember that

04:54.380 --> 04:59.690
your behavior tree has to run, its children doesn't run itself.

04:59.690 --> 05:02.600
So every node, if it's got children, it runs the children.

05:02.600 --> 05:07.310
And then based on what the children respond with, it then becomes that status.

05:07.310 --> 05:16.310
So in here I'm going to run return children current child dot.

05:17.020 --> 05:18.130
Process.

05:18.850 --> 05:23.920
Then whatever it returns, that will be the status returned by the behavior tree.

05:23.920 --> 05:30.580
And this actually makes the behavior tree then run the child objects process.

05:30.610 --> 05:38.890
Now, in this case, our child objects, if we go back into our robber, are go to diamond and go to

05:38.890 --> 05:39.270
Van.

05:39.280 --> 05:45.910
Now, currently, if they run, they're just basic nodes, so they're going to return.

05:46.540 --> 05:52.630
Success, which is pretty much useless to us at this point, means they're not actually doing anything.

05:52.630 --> 05:58.960
So we need to create another class for leaves or a leaf.

05:58.960 --> 06:00.670
So let's go back into unity.

06:00.790 --> 06:07.060
We will right click down in our behavior tree, create a new script, let's call it Leaf.

06:07.090 --> 06:10.060
Then we'll open that up in the editor.

06:10.060 --> 06:15.190
Now Leaf is going to inherit from Node.

06:16.150 --> 06:20.530
It's not going to have a start in an update so we can get rid of that.

06:20.560 --> 06:25.870
Now, the leaf itself, this is going to be a little bit trickier because the leaves have to actually

06:25.870 --> 06:31.060
run a particular process, which means there's going to be some method assigned to the leaf.

06:31.060 --> 06:33.610
We don't know what that method is.

06:33.610 --> 06:35.380
It could be anything.

06:35.680 --> 06:43.030
So in this case, it's going to be for a start running that set agents destination.

06:43.570 --> 06:45.670
That's what we want it to do because we're using a native mesh.

06:45.670 --> 06:52.210
But if you're using some other kind of AI in your system, you might want to run a finite state machine

06:52.210 --> 06:56.470
or you might want to run a neural network or something.

06:56.470 --> 07:03.430
But whatever those processes do, they must end up returning one of our node statuses.

07:03.430 --> 07:08.080
So either success, failure or running.

07:08.380 --> 07:17.170
Now what are we going to do with these leaf nodes is actually create a delegate so that we can pass

07:17.170 --> 07:21.370
through methods, so we can code up methods somewhere else and then pass them through and say, This

07:21.370 --> 07:23.620
is what we want our leaf to run.

07:23.650 --> 07:23.900
Okay?

07:23.920 --> 07:29.950
Now the way we do that is we create a public delegate and it's going to return a node status.

07:29.950 --> 07:35.350
So let's just put status and these are going to be called a tick.

07:35.830 --> 07:41.110
So a tick is essentially one turnover for your behavior tree.

07:41.140 --> 07:48.430
A tic is like a frame for the behavior tree, just like a frame happens when you run update in unity.

07:48.460 --> 07:48.590
Okay.

07:48.610 --> 07:56.020
So it's just sort of one, I guess, looping around that particular leaf or action in this case, then

07:56.020 --> 07:58.630
we need a pointer to set up.

07:58.630 --> 08:06.310
So public tic process method and that's the way we do it there.

08:06.370 --> 08:10.480
Then we're going to override our process.

08:10.480 --> 08:20.770
So public override status process again, we get rid of this base and instead what we're going to return

08:20.770 --> 08:28.390
is the process method and the process method, it's going to return status.

08:28.660 --> 08:34.150
So this is actually working out for us as long as we set up with this process method is here.

08:34.150 --> 08:44.470
So up here I might just put if process method does not equal null, we can return that.

08:44.470 --> 08:50.410
Otherwise we can return a status dot failure.

08:50.800 --> 08:52.900
Oh, what is this Pennie doing.

08:52.930 --> 08:53.620
Error checking.

08:53.620 --> 08:53.980
Yes, I know.

08:53.980 --> 08:58.540
It's very rare for me to do that, but I've done it for you there.

08:58.930 --> 08:59.530
Okay.

08:59.530 --> 09:04.060
And that will also remind you if you actually haven't set up a method for your leaf.

09:04.600 --> 09:06.010
Okay, so we've done that.

09:06.010 --> 09:10.120
How does the leaf know about the process method?

09:10.120 --> 09:15.550
Well, we could go in the behavior a behavior and then actually assign it in there.

09:15.550 --> 09:22.360
We could have like go to diamond dot process method equals this particular method or we could pass it

09:22.360 --> 09:26.560
through by doing an override of our constructor.

09:26.560 --> 09:31.570
So we want a public leaf which has pretty much nothing in it.

09:31.600 --> 09:33.100
That's our empty one.

09:33.250 --> 09:39.610
And then we want a public leaf and we're going to pass through a string for the name and then we want

09:39.610 --> 09:43.420
to process method p m.

09:45.300 --> 09:46.440
I like that.

09:46.440 --> 09:55.320
And we're then going to sit named equal n and p m.

09:56.610 --> 10:01.470
Process method to equal p m like that.

10:01.470 --> 10:03.480
Now, this is not right.

10:03.480 --> 10:06.630
That's got to be a tick like that.

10:06.900 --> 10:08.040
So we're passing it through.

10:08.070 --> 10:12.810
This is basically it's a pointer to our method that we're writing, passing it through, and then we're

10:12.810 --> 10:13.770
setting it up to here.

10:13.770 --> 10:20.400
So that then process method knows where it is down here and will actually run it for us and let's get

10:20.400 --> 10:22.410
rid of that semicolon so it doesn't like it.

10:22.530 --> 10:25.380
Okay, so we will now save this.

10:25.500 --> 10:30.420
Let's go back into our robber and implement it.

10:30.420 --> 10:36.690
So these diamonds and van's now become leaf instead like that.

10:36.690 --> 10:46.170
And they're now wanting to be constructed from leaf and leaf and they actually need to have some kind

10:46.170 --> 10:48.300
of method pass through as well.

10:48.300 --> 10:54.240
So we'll go ahead and just write something really quickly that will satisfy that.

10:54.360 --> 11:01.590
So under here, we're going to create a public node status.

11:01.590 --> 11:04.890
It's got to be in a same format that the tick is constructed in.

11:04.890 --> 11:08.700
So go to diamond like that.

11:08.700 --> 11:14.730
And when we go to the diamond, we actually want to basically be running this here.

11:14.730 --> 11:19.470
So let's take that out of there and we'll put it in here.

11:19.980 --> 11:20.430
Okay?

11:20.580 --> 11:23.640
And then we have to return something.

11:23.640 --> 11:25.650
So let's just go return.

11:26.700 --> 11:30.480
And in this case, let's make it a success.

11:30.480 --> 11:37.710
So we will return node dot status, dot success.

11:38.520 --> 11:44.940
Now notice here that the returning of the success here really has nothing to do whether your agent actually

11:44.940 --> 11:47.010
made it to the destination or not.

11:47.010 --> 11:50.520
Because at this moment I'm not really concerned about that.

11:50.910 --> 11:53.190
I just want something that you can see at running.

11:53.190 --> 11:53.460
Okay.

11:53.460 --> 11:59.220
Then up in your go to diamond leaf, we're going to pass through, go to diamond.

11:59.430 --> 12:07.110
And now the van actually needs a method pass through it as well.

12:07.110 --> 12:09.690
Okay, so now it's time for a challenge.

12:09.690 --> 12:20.370
What I'd like you to do is to write a go to Van method and set it up here with your leaf and write it

12:20.370 --> 12:22.440
similar to what we've got for go to Diamond.

12:22.440 --> 12:24.060
Don't make it too complicated.

12:24.060 --> 12:26.310
We will fix all this up a little bit later.

12:26.310 --> 12:27.960
We just want something that's going to run.

12:27.960 --> 12:29.400
So follow this format here.

12:29.400 --> 12:30.120
Pause the video.

12:30.120 --> 12:33.810
Now, when we come back, I will show you how to do that.

12:35.130 --> 12:35.850
Okay.

12:35.880 --> 12:36.810
How did you go?

12:36.810 --> 12:43.470
Well, really, all you needed to do was grab this, copy it and put it down here.

12:43.500 --> 12:47.040
Call it, go to van, then down.

12:47.040 --> 12:48.450
And your set destination.

12:48.450 --> 12:50.610
You just want to put van.

12:50.940 --> 12:56.580
And then up here in your leaf, we will put go to van.

12:58.680 --> 12:59.100
All right.

12:59.100 --> 13:04.050
So before we can play, we actually have to kick the tree off with a process of it because it's not

13:04.050 --> 13:05.430
actually running its process.

13:05.430 --> 13:11.310
And if we go into our behavior tree code, you'll see that it is ready to run the first child here in

13:11.310 --> 13:11.850
process.

13:11.850 --> 13:13.530
But we're not calling it.

13:13.530 --> 13:21.660
So back in our code we actually need to go tree dot process like that to kick the whole thing off.

13:21.660 --> 13:25.650
Now having another look through our tree, here's our tree.

13:25.650 --> 13:31.080
We then have a intermediate node which is just a generic node at the moment.

13:31.080 --> 13:36.750
We will modify that to something else a little bit later, but we're just leaving it at Node four.

13:36.750 --> 13:42.300
Now, if we go into Node and have a look at the nodes process, you can see that it's going to return

13:42.300 --> 13:50.550
success, which is kind of useless for us because we want it to then go and run its children events.

13:50.550 --> 13:56.610
So instead I'm just going to replace this in here with the same code that I've got in the behavior tree,

13:56.610 --> 14:04.410
which is going to be children current child dot.

14:05.290 --> 14:06.130
Process.

14:06.160 --> 14:11.710
Now, this is still not perfect, but it's enough to actually get part of our tree running.

14:11.710 --> 14:12.940
So save that.

14:12.940 --> 14:21.730
Save your rubber behavior, and then let's go and press play just so that you can see something working

14:21.730 --> 14:22.500
at the end here.

14:22.510 --> 14:23.440
Okay, great.

14:23.440 --> 14:24.090
There we go.

14:24.100 --> 14:25.660
So off he goes.

14:25.810 --> 14:33.400
He's gone from the top level root of the tree to steel down to go to diamond.

14:33.520 --> 14:35.650
Now, he's going to stop there.

14:35.650 --> 14:40.630
He's not going to turn around and come back to the van, because if you have a look at the code, we're

14:40.630 --> 14:45.130
only running the first child of any particular node.

14:45.160 --> 14:49.840
The van is a second child of steel, so it's not actually running.

14:50.320 --> 14:56.590
And the way that we currently have our nav mesh code set up to go to the destination isn't actually

14:56.590 --> 15:00.850
correct either, but you at least have something that works that you can test.

15:01.150 --> 15:04.210
Okay, so that is our action.

15:04.210 --> 15:12.730
What are we going to do when we come back in the next lecture is actually fix up the nav mesh code and

15:12.730 --> 15:17.380
put in some sort of proper code for go to Diamond and go to Van.
