WEBVTT

00:03.840 --> 00:08.910
So far we've built all the fundamental nodes for a behavior tree.

00:08.910 --> 00:15.330
So isn't it amazing just how simple it really is underneath and how you can build up a very complex

00:15.330 --> 00:17.310
structure of behavior with them?

00:17.310 --> 00:24.720
So we've looked at the sequence nodes now they are denoted by an arrow which I've put in here, and

00:24.720 --> 00:31.740
the children of the sequence each get run until one of them fails.

00:31.740 --> 00:39.150
So it's kind of like ending them together in an end statement, always starting with the first child

00:39.150 --> 00:44.160
that goes into that list of children and working through them in order.

00:44.340 --> 00:53.310
Now we also have created a selector node and the selector allows you to have multiple children again

00:53.310 --> 00:57.540
and it will work through them, but only one of them has to be successful.

00:57.540 --> 01:03.750
So if the first one is successful, it will return the value back up to its parent.

01:04.050 --> 01:09.870
If not, it continues on to the next one in the list and performs that instead.

01:10.110 --> 01:18.960
So that's sort of when you're making a selection between multiple actions that kind of all result in

01:18.960 --> 01:21.930
the same kind of thing that we've done with these doors.

01:22.170 --> 01:27.960
So you might have like ten doors into that gallery and the agent might be able to actually go through

01:27.960 --> 01:32.220
and check all of those ten doors, if it had to, to get in.

01:32.220 --> 01:33.840
And you might even include windows.

01:33.840 --> 01:37.200
You might climb through a window if you can't get in through a door sort of thing.

01:37.200 --> 01:44.010
But we only need one of those to be successful in order for him to continue trying to steal the diamond.

01:44.340 --> 01:50.280
Now, what we're going to move on to is having a look at another type of node, which is a condition,

01:50.280 --> 01:55.920
and the condition is really just another leaf like an action.

01:55.920 --> 02:02.700
But it's it's actually much, much simpler because rather than doing any particular process whatsoever,

02:02.700 --> 02:05.250
it's just testing a certain value.

02:05.250 --> 02:11.730
So what we are going to do is you can see that I've added to this tree is add a condition at the beginning

02:11.730 --> 02:19.920
of the steel sequence to make the agent go and steal something if he doesn't have enough money.

02:19.920 --> 02:22.530
But if he does have enough money, he's not going to do anything.

02:22.530 --> 02:28.200
So the condition in this case is to check if the robber has enough money.

02:30.000 --> 02:37.170
So we're going to begin in the code by adding in a property to store the agents money.

02:37.170 --> 02:44.220
So if we come back into the robber behavior in these property, I'm going to add a property for money.

02:44.220 --> 02:47.040
It's almost like an inventory system, I guess.

02:47.040 --> 02:48.180
Very simple, of course.

02:48.180 --> 02:52.740
So I'm going to make it so that we can slide it in the inspector.

02:52.740 --> 03:05.040
So the range for the amount of money, let's go from 0 to 1000 and then this will be a public int and

03:05.040 --> 03:06.180
we'll call it money.

03:06.780 --> 03:09.720
And let's say to begin with there's 800.

03:10.500 --> 03:19.320
So as long as he's got more than let's say $500, then he won't go and perform the stealing.

03:19.320 --> 03:21.930
But if it drops below that, he will go and do that.

03:22.080 --> 03:22.560
All right.

03:22.560 --> 03:26.220
So there's our inventory now.

03:26.220 --> 03:30.540
Our condition is that there's more than $500 in there.

03:30.540 --> 03:33.780
So we need to create this leaf node now, a leaf.

03:33.780 --> 03:41.760
If we just go down a little bit, a leaf is a node with a name, but also a method attached to it.

03:41.760 --> 03:47.280
So we have to create a method that tests for the money.

03:47.280 --> 03:52.230
Now that's really, really straightforward because we're already creating all of these methods here.

03:52.230 --> 03:58.440
And essentially they're kind of a test to see if a task is finished running, but we're just doing a

03:58.440 --> 03:59.250
simple check.

03:59.250 --> 04:07.530
So in here, I'm just going to grab maybe this one, the simplest one we've got, and I might just put

04:07.530 --> 04:14.550
this up above here and let's call it has money.

04:14.880 --> 04:18.630
And inside of here, we're going to return.

04:20.260 --> 04:30.010
First of all, if money is greater than or equal to 500, then we will return.

04:31.570 --> 04:34.050
Node dot.

04:34.150 --> 04:36.010
Status dot.

04:37.590 --> 04:38.580
Success.

04:38.760 --> 04:42.240
Or in our programming speak that would be a true.

04:42.360 --> 04:48.720
Otherwise we're going to return node status dot value.

04:48.990 --> 04:56.820
So this is a node that doesn't have a running status because the logic just gets performed and then

04:56.820 --> 04:58.970
returns success or failure.

04:58.980 --> 05:06.600
Now, because it's in a sequence, if he has money that returns success and that allows that sequence

05:06.600 --> 05:07.310
to keep running.

05:07.320 --> 05:11.820
If he doesn't, it is a failure and therefore it won't.

05:12.090 --> 05:14.970
And I've just realized I've got these around the wrong way.

05:15.900 --> 05:23.640
If has money greater than 500, then we actually want to fail because this is a condition where he's

05:23.640 --> 05:26.340
got less than 500 in order to trigger it.

05:26.340 --> 05:30.660
So we want it to be successful when it's less than that.

05:30.660 --> 05:32.250
So put this in.

05:33.210 --> 05:33.740
Okay.

05:33.990 --> 05:34.590
Right.

05:34.860 --> 05:35.910
I've got my logic right now.

05:35.910 --> 05:38.790
So there's my has money, so let's save that.

05:39.060 --> 05:44.610
So let's go up here and create that leaf for having money.

05:44.610 --> 05:47.940
So I'm going to put that doesn't really matter where we put it in here.

05:48.180 --> 05:49.020
Leaf.

05:50.670 --> 06:05.130
Has got money equals new leaf has got money and then in here has money.

06:07.550 --> 06:15.680
Then with that leaf we can put it into our steel sequence as the first thing because it's what's going

06:15.680 --> 06:18.530
to trigger it off to begin with, or it's not even going to trigger it off.

06:18.530 --> 06:24.680
It's just going to stop that sequence from running if the money is above 500.

06:24.680 --> 06:28.100
So let's put steel dot add child.

06:32.050 --> 06:37.360
And we're going to add in has got money in there.

06:37.390 --> 06:44.890
Now, the final thing we need to do is to come down into our update where we're actually running our

06:44.890 --> 06:53.010
tree, because our tree here is going to say if it's running, then process it again, process it again

06:53.010 --> 06:54.460
and process again until it stops.

06:54.460 --> 07:02.360
But let's just have a look at what happens if I save this and go back into my back into unity.

07:02.380 --> 07:05.140
Now we find our robber over in the inspector.

07:05.140 --> 07:08.920
You can see there's a slider for money, so let's press play.

07:09.490 --> 07:15.310
And he's not going to do anything to start with now because he's been stopped by that new condition.

07:15.640 --> 07:23.740
What we would expect is if we slide this down below 500, he should start doing something, but he doesn't.

07:24.250 --> 07:33.220
So what's actually happened here is that in that update for the tree status of running the tree, the

07:33.220 --> 07:39.430
tree runs through once because he's got enough money.

07:39.430 --> 07:46.540
That whole sequence fails and it never comes back and tries again because this is only like one run

07:46.540 --> 07:47.740
through of the tree.

07:47.740 --> 07:50.890
It's going to keep processing the tree while it's running.

07:50.890 --> 07:57.340
And then once it gets a fail or succeed in this case actually getting a fail, then it's not going to

07:57.340 --> 07:58.570
bother doing it again.

07:58.570 --> 08:04.180
So our condition for running our tree over and over is now changed.

08:04.480 --> 08:15.610
Instead, we actually want to say if the tree status does not equal success, then we're going to run

08:15.610 --> 08:16.900
it again.

08:16.900 --> 08:17.420
Okay?

08:17.440 --> 08:24.820
And whatever you want with your particular behaviors, with your trees and how you want them to work,

08:24.820 --> 08:28.060
some trees will run over and over again.

08:28.480 --> 08:30.340
Some will run just once.

08:30.820 --> 08:36.040
You have to put the logic in here to make sure that is so and then you can trigger them elsewhere.

08:36.040 --> 08:43.330
I mean, you could load up a tree during runtime to put into your agent when some other condition or

08:43.330 --> 08:44.860
some other scene loads or something.

08:44.860 --> 08:47.020
It might be completely different behavior tree.

08:47.020 --> 08:53.680
Right, so we'll save that, go back into unity and now we're going to press play.

08:54.820 --> 08:59.520
And so he's now got 800.

08:59.530 --> 09:04.540
I'm going to slide that money down in the inspector to below 500 and look what happens.

09:04.540 --> 09:10.810
As soon as it gets below, he's going to go and act out what was happening before.

09:10.810 --> 09:21.640
So he'll go around and he's going to go on execute that, and then he'll go get back to the vehicle,

09:22.510 --> 09:26.920
which is where everything ends and.

09:27.630 --> 09:34.140
So essentially he's stolen the diamond because he wants his money to go up.

09:34.560 --> 09:38.520
When he gets back to the van, currently, it's not going to go up because we haven't actually coded

09:38.520 --> 09:39.270
that in.

09:39.270 --> 09:41.730
So I'm going to leave that as a challenge for you.

09:42.270 --> 09:46.080
What I'd like you to do is have the money over here.

09:46.320 --> 09:54.570
So the money thing we've created in our script to increase in value when he gets the diamond back to

09:54.570 --> 10:02.490
the van and at that point he can leave the diamond in the van and to signal that you can just destroy

10:02.520 --> 10:05.460
the diamond object because it's not needed any more.

10:05.610 --> 10:05.910
Okay.

10:05.910 --> 10:07.170
So pause the video now.

10:07.560 --> 10:08.220
Go do that.

10:08.250 --> 10:10.740
When we come back, I'll show you how to do it.

10:11.760 --> 10:12.180
Okay.

10:12.180 --> 10:13.260
Welcome back.

10:13.260 --> 10:14.670
How did you go?

10:14.670 --> 10:19.110
Well, let's go back into our code so we know that is picking up the diamond.

10:19.110 --> 10:25.080
We know he's taking it back to the van and he's taking it back to the van up here.

10:25.110 --> 10:26.100
Go to van.

10:26.100 --> 10:34.740
So when go to Van is successful, which means he's completed it, that's when you want to sort of sell

10:34.740 --> 10:36.810
off the diamond and add the money.

10:36.840 --> 10:43.710
Now, in order to do that, we need some logic very similar to what we've done with Go to Diamond.

10:43.710 --> 10:49.140
We actually running this, go to location and then waiting for it to become successful and then doing

10:49.140 --> 10:50.910
our logic down in here.

10:51.060 --> 10:54.420
So I'm basically just going to copy this out of here.

10:54.510 --> 11:04.740
Let's put this down in our go to van and we've got to go to location, which will be Van here.

11:05.160 --> 11:12.000
And we're not going to parent the van to the agent at this point.

11:12.000 --> 11:13.680
So we're going to the van.

11:13.680 --> 11:19.980
What we want to do here is put money plus equals.

11:20.250 --> 11:22.530
How much is that diamond going to be worth?

11:22.530 --> 11:25.170
Let's say $300.

11:25.170 --> 11:33.450
And then we can actually get rid of the diamond altogether so we could go diamond dot set active to

11:33.450 --> 11:33.870
false.

11:33.870 --> 11:39.030
So it's still there, but we've made it sort of invisible and taking it kind of out of the equation.

11:39.030 --> 11:41.040
Okay, so we'll save that.

11:41.040 --> 11:44.730
Let's go back into unity, try that out.

11:44.910 --> 11:46.350
So we press play.

11:46.350 --> 11:49.230
He's got still he's got $800.

11:49.230 --> 11:54.990
And I'm going to just put that down just below 500 to trigger that behavior.

11:55.290 --> 11:59.190
And then off he goes, he'll go and get the diamond.

11:59.880 --> 12:06.090
And once he does take that back to the van, we should see the money going up.

12:06.090 --> 12:12.000
So as soon as he got that diamond, we can start watching over in the inspector for that money value

12:12.000 --> 12:12.810
to go up.

12:13.440 --> 12:22.890
And at the same time, you'll notice that the diamond is going to disappear altogether out of your hierarchy

12:23.100 --> 12:24.600
of game objects.

12:25.320 --> 12:26.890
So actually, I'm going to watch him first.

12:26.890 --> 12:29.370
It's going to come back here to the van.

12:29.610 --> 12:35.160
Diamond disappears and let's go over to the inspector and his money has gone up.

12:35.700 --> 12:37.140
Fantastic.

12:37.770 --> 12:40.470
Okay, so you've now done that.

12:40.620 --> 12:46.920
And believe it or not, you've now covered all the fundamental nodes that make up behavior trees.

12:46.920 --> 12:51.570
You've got enough knowledge and skills now that you can actually take this and do a bit of Googling

12:51.570 --> 12:57.360
on behavior trees to have a look and get some more ideas how other people have actually expanded on

12:57.360 --> 12:57.810
the nodes.

12:57.810 --> 13:03.120
So don't be afraid to even add more nodes to your system that do other things.

13:03.330 --> 13:12.210
Just make sure that you always stick to that control flow of returning success, failure or running.
