WEBVTT

00:02.520 --> 00:10.890
In the previous lecture, we put together our actions and got our character moving now in order to execute

00:10.890 --> 00:12.180
a plan.

00:12.210 --> 00:16.800
As such, we need to add in sequence nodes.

00:17.010 --> 00:24.570
So so far we've got like a go to door and a go to van node, but they don't actually play out in sequence.

00:24.570 --> 00:30.780
And that's what you want as your plan and you want your agent to run them one by one.

00:30.780 --> 00:39.150
Now, if at any point during the sequence one of those actions fails, then there's no point continuing

00:39.150 --> 00:40.440
with the sequence.

00:40.680 --> 00:45.660
Therefore, you have to take that into account when you're actually programming the sequence node.

00:45.660 --> 00:57.030
So basically what it comes down to is that a sequence node is like an logical, and if all of its children

00:57.090 --> 01:03.510
are successful, then the sequence node actually returns successful to its parent.

01:03.810 --> 01:11.250
If any of the children fail one or more of them, then the sequence node itself fails and it reports

01:11.250 --> 01:14.280
a fail back up the tree to its parent.

01:15.540 --> 01:20.940
All right, so let's create a sequence node in our behavior tree code.

01:20.940 --> 01:23.430
We're going to create another C-sharp file.

01:23.430 --> 01:28.920
This one is going to be called sequence and open that up in your editor.

01:28.920 --> 01:29.250
Right.

01:29.250 --> 01:32.160
Once again, this is going to be a node, not a miner behavior.

01:32.160 --> 01:35.580
And we can get rid of the contents of that.

01:36.090 --> 01:39.540
Now, the first thing we'll do is just create a public constructor.

01:39.540 --> 01:45.030
So sequence and we'll have that pass through the name for it.

01:45.030 --> 01:48.090
And now we will write its process.

01:48.360 --> 02:00.600
So it's going to be a public override node dot status process and actually don't need that node in there,

02:00.600 --> 02:02.160
but that doesn't really matter.

02:02.160 --> 02:02.640
Okay.

02:02.640 --> 02:12.480
So this is going to return status dot running as a default.

02:12.900 --> 02:24.240
Now before that, it's going to basically loop through all of its children and run their process method.

02:24.450 --> 02:26.940
But you can't have a loop in here.

02:27.180 --> 02:35.460
Remember, it's like an update in unity, so you've kind of got to set it running and then see if it's

02:35.460 --> 02:39.720
still running next time this process comes around and runs again.

02:39.750 --> 02:40.020
Okay.

02:40.050 --> 02:46.860
So first of all, what we're going to do is grab the status of running the first child.

02:46.860 --> 02:47.820
So status.

02:48.750 --> 02:53.640
Child status equals children.

02:55.240 --> 02:56.680
Current child.

02:56.680 --> 03:02.800
Now, if you remember back when we first created our node, we made that current child integer.

03:03.670 --> 03:06.820
And that's just keeping track of which child is currently running.

03:06.820 --> 03:08.620
So it's initially set to zero.

03:08.620 --> 03:10.780
So we go back into our node and have a look.

03:10.960 --> 03:12.310
It's up here.

03:12.310 --> 03:18.040
So the first time that we run that in there, it's going to pick up that very first child that's in

03:18.040 --> 03:22.960
that array of the processors or the children processors.

03:23.110 --> 03:23.440
Okay.

03:23.440 --> 03:34.390
Now if this child status equals status dot running, that means that that child is still running, in

03:34.390 --> 03:41.350
which case we're just going to return status dot running K, we're not going to go and run anything

03:41.350 --> 03:41.530
else.

03:41.530 --> 03:46.840
We're not going to set the process to do another child because we're currently in the middle of already

03:46.840 --> 03:48.420
doing something.

03:48.430 --> 03:48.590
Okay.

03:48.640 --> 03:52.030
So this might be, for example, go to Diamond.

03:52.210 --> 03:58.450
The agent might still be making its way towards there, which means that that particular node hasn't

03:58.450 --> 04:02.290
returned a value yet, hasn't been successful or failed.

04:02.290 --> 04:04.300
It's still running itself.

04:04.750 --> 04:05.000
Okay.

04:05.050 --> 04:18.700
Now, if it's not running, if child status equals status dot failure, then we're going to return child

04:18.700 --> 04:25.120
status, basically saying that, okay, this sequence node has now reported a failure.

04:25.120 --> 04:25.780
We're finished.

04:25.780 --> 04:28.810
We're not doing any more children because we can't continue.

04:28.810 --> 04:34.990
One of our children states or our children actions has failed.

04:35.200 --> 04:40.150
Okay, if you can't go to the diamond, then there's no point continuing trying to steal the diamond,

04:40.150 --> 04:40.840
etc..

04:41.050 --> 04:47.470
Okay, so once we've passed that, so if we haven't failed, that means we must now be successful with

04:47.470 --> 04:50.140
that particular child because it's not running and it's not failed.

04:50.140 --> 04:58.990
So current child plus plus we want to go the next time we come in here to the next child and actually

04:58.990 --> 04:59.650
process it.

04:59.650 --> 05:08.560
So we're going to say if current child is greater than or equal to the children dot count.

05:08.560 --> 05:17.590
So if we've actually made it to the end of our list, then child current child sorry equals zero.

05:17.590 --> 05:19.780
We're going to reset that for next time.

05:19.780 --> 05:29.650
We might run this sequence and we're going to return status dot success so that we've basically looped

05:29.650 --> 05:37.210
through all of the children and none of them have reported a failure, which means that we're successful.

05:37.210 --> 05:38.740
We've made it to the end of the sequence.

05:38.740 --> 05:41.770
So let's say, okay, we've done it, we're finished.

05:41.860 --> 05:42.520
Great.

05:42.790 --> 05:45.070
That is your sequence node.

05:45.070 --> 05:46.930
Simple, simple, simple.

05:46.930 --> 05:50.410
But it's just getting your head around how this is actually running.

05:50.410 --> 05:55.510
And it's it's looping, but it's not looping in a single for loop within here.

05:55.930 --> 05:56.590
All right.

05:56.590 --> 06:05.500
So now let's get this going, save that and we'll go back into our robber behavior because this is where

06:05.500 --> 06:07.330
we need to set all of this up.

06:07.330 --> 06:13.300
So we've got our go to diamond and our go to Van, which means our agents going to go to the diamond

06:13.300 --> 06:16.210
and then go back to the van, or that's at least what we want.

06:16.240 --> 06:22.360
Now, I know in the illustration at the beginning of this lecture, I had go to door in there as well

06:22.360 --> 06:23.920
and will add that a little bit later.

06:23.920 --> 06:26.050
We just want to keep this simple for now.

06:26.110 --> 06:35.530
Now we're already adding those two things to our steal node, but still is now going to become a sequence.

06:35.530 --> 06:45.430
So we'll change it from our basic node to a sequence and we'll change it here to a sequence.

06:46.900 --> 06:49.210
Now these are being added to it, and that's fine.

06:49.210 --> 06:53.590
They're adding to it in order that you want them to be processed.

06:54.160 --> 07:02.290
Now, when we start, we run treetop process that runs the tree once it doesn't continue running the

07:02.290 --> 07:09.640
tree, but we need to keep running the tree just in the same way that unity keeps running its update,

07:09.640 --> 07:12.760
which is down here and empty at the moment.

07:12.760 --> 07:20.620
So instead of running our tree up here in START, we actually want to run it in update.

07:21.010 --> 07:25.810
And in order to run it in update, we need to know what it's returning each time.

07:25.810 --> 07:36.280
So I'm going to go up the top here and actually put in a another variable so it'll be no dot status,

07:37.720 --> 07:49.840
which will actually be the tree status equals node dot status dot running as a default there.

07:49.840 --> 07:54.460
And then what we're going to do is take this tree process out of start.

07:54.620 --> 07:56.480
So let's just cut that out of there.

07:56.510 --> 08:03.650
Bring it down into our update just here and then just test.

08:03.650 --> 08:17.240
If true status equals no dot status dot running, then we're going to process the tree.

08:17.930 --> 08:24.050
Now, when we do, we also need to make sure that we update what tree status is so that this will stop

08:24.050 --> 08:25.040
it running again.

08:25.040 --> 08:31.840
Otherwise it will loop around and continue running whatever particular action you're trying to achieve.

08:31.850 --> 08:36.470
So we'll put tree status equals trade up process.

08:36.470 --> 08:40.970
Okay, so we'll save that and now we're ready to try it out.

08:40.970 --> 08:47.990
So let's go back into unity and we'll press play and we should see those behaviors take place where

08:47.990 --> 08:50.780
we're going to go to the diamond.

08:51.170 --> 09:00.170
So the nav mesh is now in control as far as working out the path, we're going to the diamond and then

09:00.440 --> 09:01.640
we've reached the diamond.

09:01.640 --> 09:04.310
So that's actually returned success on there.

09:04.310 --> 09:09.650
And now we can go back to the van.

09:09.650 --> 09:16.550
And then because that tree is successful, we will stop there and it won't continue.

09:16.550 --> 09:22.730
If you take out that tree status equals your tree process in the update, you'll actually see that this

09:22.730 --> 09:25.790
will bounce around and loop through that behavior.

09:26.360 --> 09:26.960
All right.

09:26.960 --> 09:35.270
So we've got that running now if you need to debug this at all, because it is a little going by faith,

09:35.270 --> 09:41.450
I guess, with the way that we have actually coded it for you as you go through this, what you might

09:41.450 --> 09:50.300
like to do is in your processes, put some debug logs to see which process is actually running.

09:50.300 --> 09:56.240
Because remember, we went to the trouble of sending every single node a name.

09:56.330 --> 09:56.690
Okay?

09:56.690 --> 10:06.080
So in here you can actually put like a debug log processing children, current child dot name in here.

10:06.380 --> 10:08.150
So that would allow you to keep track.

10:08.150 --> 10:10.850
And you can also then print out what the status is.

10:10.970 --> 10:18.380
So in all your processes, wherever they are in leaf, do some debug logs and then you can watch what's

10:18.380 --> 10:24.650
going on, watch which one is processing and where your issues may be.

10:24.800 --> 10:25.460
All right.

10:25.460 --> 10:27.680
Time for a challenge now.

10:27.680 --> 10:33.380
If you remember right back to the beginning, we had a little behavior tree that had a go to door in

10:33.380 --> 10:40.970
it between starting over here, going to a door, going to the diamond and then coming back to the van.

10:41.810 --> 10:51.230
What I'd like you to do is to put together this little sequence where we now, first of all, go to

10:51.230 --> 10:55.550
the door, go to the diamond, and then go to the van.

10:55.550 --> 11:01.970
Now, when I say go to the door, actually want you to go to this door over here.

11:02.270 --> 11:09.020
So I want your agent to go to that door, go to the diamond, and then go to the van.

11:09.020 --> 11:15.230
Or in fact, you could even say go to door, go to diamond, go to door and go to Van.

11:15.230 --> 11:16.670
So see if you can put that in.

11:16.670 --> 11:17.870
Pause the video now.

11:18.170 --> 11:19.370
Go ahead and do that.

11:19.370 --> 11:22.010
When we come back, I'll show you how to do it.

11:22.640 --> 11:23.660
Welcome back.

11:23.660 --> 11:26.000
How did you go with that challenge?

11:26.000 --> 11:31.700
Now, you might have found a little hiccup with this as you did it, and I'll talk about that when it

11:31.700 --> 11:32.870
happens to me.

11:32.900 --> 11:41.870
First of all, this door over here, if we find it over in the scene, it's called the back door.

11:42.140 --> 11:43.280
So that's it there.

11:43.280 --> 11:44.690
And that's the one we want.

11:44.690 --> 11:50.870
So inside of our robber behavior code, we need to make room to pass that through.

11:50.870 --> 12:03.500
So let's go back up to the top here and we'll put public game object back door like that, and then

12:03.500 --> 12:07.490
we need to create a leaf for that particular thing.

12:07.730 --> 12:09.410
So we want a leaf here.

12:09.410 --> 12:14.360
Let's take that and I'm just going to copy and put it down here.

12:14.360 --> 12:19.910
So go to door and let's call it backdoor.

12:19.910 --> 12:34.700
Just to be specific here, go to Backdoor and then we'll put go to backdoor in here and go to backdoor

12:34.700 --> 12:39.770
as our method which we then need to create.

12:39.770 --> 12:43.130
So let's come down the bottom here and we've got go to Diamond.

12:43.140 --> 12:54.410
So let's just make a copy of that, go to back door and then our go to location will be.

12:54.630 --> 12:57.540
Back door doc transformed position.

12:57.840 --> 12:59.540
The rest of it will be okay.

12:59.550 --> 13:04.770
Let's come back up here and add in the go to back door in our sequence.

13:04.770 --> 13:07.110
So actually want that to be the first one, don't we?

13:07.110 --> 13:23.670
So let's go up here and put steel dot go add child, go to Backdoor, go to Diamond and then we want

13:23.670 --> 13:28.410
to go back to the back door to get out and then go to the van.

13:28.800 --> 13:36.270
So let's save that, go back into unity and we'll press play and have a look at what we get.

13:36.300 --> 13:42.540
Of course, after we go to the rubber code and pass through that backdoor object to our script.

13:42.840 --> 13:47.550
So there it is there press play and now let's have a look at it playing out.

13:47.550 --> 13:53.130
So immediately the first thing you would have noticed is that he looks like he's going to the front

13:53.130 --> 13:55.170
door, but he's not.

13:55.170 --> 13:59.430
He's taking a shortcut to the back door via the front door.

13:59.460 --> 14:03.060
Now that is because of the nav mesh.

14:03.270 --> 14:06.900
It's finding a shorter path through that gap in the front door.

14:06.900 --> 14:08.310
So but at least he went there.

14:08.310 --> 14:13.710
I guess now he's gone to the back door, now he's going back to the diamond.

14:14.010 --> 14:18.990
And once he does that, he's going to go back to the back door.

14:19.740 --> 14:26.880
And then I'll assume here that he's going to make a shortcut via that front door.

14:27.270 --> 14:30.570
Oh, now he's calculated differently and go back to the van.

14:31.860 --> 14:32.370
Obviously.

14:32.370 --> 14:33.360
It sure did to go that way.

14:33.360 --> 14:34.650
So great.

14:34.650 --> 14:41.520
Now, the issue is that he's gone to the back door because apparently the front door is locked and he

14:41.520 --> 14:43.890
can't get through there, but he can in this case.

14:43.890 --> 14:50.370
Now, that's, as I said, a nav mesh issue, and we'll sort that out in coming videos.

14:50.550 --> 14:51.270
Okay.

14:51.270 --> 14:53.250
So that is sequences.

14:53.250 --> 14:59.730
When we come back in the next lecture, we're going to look at selectors.
