WEBVTT

00:03.770 --> 00:04.760
Welcome back.

00:04.790 --> 00:11.120
In the last lecture, I left you with the task of printing out the simple behavior tree that we've been

00:11.120 --> 00:12.500
constructing.

00:12.920 --> 00:21.590
Unfortunately, this isn't actually a simple task and well done to you if you manage to nut it out.

00:21.740 --> 00:25.850
Even better if you actually went and did a bit of research to find out how to do this.

00:25.900 --> 00:32.810
Okay, so Behavior Tree is basically a graph and there's plenty of algorithms out there that have already

00:32.810 --> 00:36.780
been written for traversing through a graph and printing it out.

00:36.800 --> 00:39.190
Now you can do this one of two ways.

00:39.200 --> 00:44.150
You can use recursion or you can do it without recursion.

00:44.150 --> 00:50.600
Now I'm going to do it without recursion, just so I don't confuse people who aren't all over the way

00:50.600 --> 00:54.680
that recursion works, because it can be a little bit mind bending.

00:55.160 --> 00:55.640
All right.

00:55.640 --> 00:58.950
So let's start out with our print tree.

00:58.970 --> 01:07.130
So in here, I'm, first of all, going to put in a string and that string will be the tree printout.

01:08.180 --> 01:11.810
So I'll store everything in a string and then print it out right at the end.

01:12.050 --> 01:14.600
So that equals nothing at this point.

01:14.840 --> 01:23.660
Now, in order to remember where we are in this tree, I'm going to create a stack and the stack is

01:23.660 --> 01:25.610
going to have nodes in it.

01:26.750 --> 01:33.680
And let's call that the node stack equals new stack of node.

01:33.770 --> 01:37.190
And then we put the current node in there.

01:37.190 --> 01:41.690
So the current node at this point is the basic tree itself.

01:41.960 --> 01:56.810
So we'll go node, current node equals this and then we'll go node stack dot, push current.

01:58.050 --> 01:58.810
Note.

01:59.490 --> 02:04.320
Okay, so we've got the root of the tree in that stack.

02:04.680 --> 02:13.140
Next, we basically start looping around through each node that if it has children, we want to put

02:13.140 --> 02:18.420
them onto the stack and then we want to get off the parents and print them out.

02:18.420 --> 02:31.140
So it's going to look like this while the node stack does not count, does not equal zero, and it shouldn't

02:31.140 --> 02:34.650
the first time through because we've put a node in up here.

02:34.920 --> 02:42.120
So while it's not equal to zero node, next node equals node.

02:44.000 --> 02:45.980
Stick Dot Pop.

02:46.340 --> 02:50.990
So we grab hold of the route at this point because that's the only one in there.

02:51.230 --> 02:52.820
And then we print that out.

02:52.820 --> 02:55.020
So we add it to our tree print out string.

02:55.040 --> 03:04.070
It's going to equal plus equals next node name plus a return.

03:04.760 --> 03:06.910
So we can go down to a new line.

03:07.160 --> 03:15.380
Then we go for int I and we loop around all of the children of the one that we just popped off and put

03:15.380 --> 03:16.940
them into the stack.

03:17.150 --> 03:17.570
Okay.

03:17.570 --> 03:22.410
So in this case, I'm going to loop backwards through the children.

03:22.430 --> 03:27.380
The reason being, if you look forwards through the children, it actually prints them out in reverse

03:27.380 --> 03:30.920
order because of the way that it's popping from the stack.

03:31.190 --> 03:35.300
So I'm going to start at the top end of the children list.

03:35.300 --> 03:43.100
So next node dot children, dot, count minus one.

03:43.220 --> 03:48.050
I is then greater than or equal to zero.

03:48.050 --> 03:56.060
So while we haven't counted down through all the children I minus minus we will add them to our stack.

03:56.060 --> 04:01.220
So node stack dot push next.

04:01.580 --> 04:02.750
Node dot.

04:04.400 --> 04:05.420
Children.

04:06.020 --> 04:06.890
I.

04:09.100 --> 04:09.580
Okay.

04:09.670 --> 04:17.560
Then when we've done that we can go debug log and we're going to print out the tree.

04:18.520 --> 04:23.550
Print out value like that and that will print out our tree.

04:23.560 --> 04:24.190
So.

04:24.220 --> 04:25.690
How did you go?

04:25.930 --> 04:30.040
I guess if you've got something working, you know that it's working so well done.

04:30.610 --> 04:33.280
This is the way that I'm doing it.

04:33.520 --> 04:36.460
If you've got a different method that works for you, then please share it.

04:36.460 --> 04:42.080
If you did it with recursion, then share that too, so that others can learn from your code.

04:42.130 --> 04:43.570
Okay, so I'm going to save that.

04:43.570 --> 04:52.630
Now, back in my rubber behavior, I've actually added in some sort of dummy extra nodes in here just

04:52.630 --> 04:56.340
so they've got something more interesting to print out and I'll delete them a bit later.

04:56.350 --> 04:58.960
So we have our stealing stuff up here.

04:59.200 --> 05:07.120
I've also added in an eight node which will have two children of go to pizza shop and buy pizza, and

05:07.120 --> 05:12.670
therefore I'm just adding them down here the same way that we did for steel at the top.

05:12.670 --> 05:14.410
Of course, you don't need to put these in there.

05:14.410 --> 05:17.260
I just like to see a little bit more data when I print this out.

05:17.440 --> 05:17.860
Okay.

05:17.860 --> 05:25.360
So at the bottom of the start, we can then go tree, dot, print tree to run that method we just created.

05:25.360 --> 05:26.290
Save it.

05:26.290 --> 05:29.140
Now let's go back into unity then.

05:29.140 --> 05:33.270
You want to add the rubber behavior script on to the robber?

05:33.280 --> 05:35.320
As you can see, I've already done here.

05:35.590 --> 05:38.800
And we're going to press play and have a look.

05:39.130 --> 05:42.070
Okay, so play now in the console.

05:42.070 --> 05:43.780
You won't see the entire tree.

05:43.780 --> 05:45.640
You're just going to see the top level of the tree.

05:45.640 --> 05:50.770
But if you click down the bottom here in the actual console, you'll see your tree.

05:50.770 --> 05:52.960
So there's tree still something.

05:52.960 --> 05:56.170
So that's the first child of tree.

05:56.170 --> 06:01.810
And then you've got your go to Diamond, go to Van, then you've got your eat something and go to pizza

06:01.810 --> 06:03.940
shop and buy pizza.

06:04.390 --> 06:04.810
Great.

06:04.810 --> 06:07.240
So it's all in the right order.

06:07.270 --> 06:14.680
Now what would be really nice is if we could indent these to show which ones are children of which.

06:14.830 --> 06:24.100
So just with a little bit of tricky code, if I go back into there, I'm going to add in a very simple,

06:24.100 --> 06:26.380
cheeky little structure just up here.

06:26.380 --> 06:37.330
So let's create a struct or node level and this structure is going to hold on to the node and the level

06:37.330 --> 06:39.310
that the node is at in the tree.

06:39.310 --> 06:41.500
So the root is level zero.

06:41.530 --> 06:43.510
The next node down would be level one.

06:43.510 --> 06:45.550
It's children will be level two.

06:45.940 --> 06:49.720
And then any on the same level as one, they're all going to be ones.

06:49.720 --> 06:52.810
And then their children are two and three all the way through it.

06:52.810 --> 07:04.810
So in here we want to have a public int level and a public node to store the node.

07:04.810 --> 07:05.150
Okay?

07:05.170 --> 07:10.510
So once we've done that, we have to just modify our print tree.

07:10.510 --> 07:15.340
So this stack is now going to have node level in it instead.

07:15.340 --> 07:24.010
So in here I'll put node level over here will be node level.

07:25.090 --> 07:30.430
Then when we push the first thing onto that stack, it's not a node anymore.

07:30.430 --> 07:32.230
It's got to be a node level.

07:32.230 --> 07:34.870
So inside of here will put new.

07:37.460 --> 07:47.630
No level and because it's a struct, we can actually go like this level equals current node and then

07:47.660 --> 07:51.070
know it doesn't need equals the level which will be zero at this point.

07:51.080 --> 07:51.920
Comma.

07:52.580 --> 07:58.490
Node equals current node and then squiggly brackets at the end.

07:58.490 --> 08:05.300
So just take notice of how that struct is being declared and created and pushed through here.

08:05.480 --> 08:06.050
Okay.

08:06.050 --> 08:12.950
So then we have inside of our while loop we want to do the same thing is correct.

08:12.950 --> 08:13.250
Or this.

08:13.250 --> 08:17.090
Now the thing being popped off at this point is going to be a node level.

08:18.470 --> 08:28.460
And then what we're printing out here is the next node, dot node, dot name, and we want to use the

08:28.460 --> 08:29.120
level.

08:29.120 --> 08:31.130
Now we're going to use the level not printed out.

08:31.130 --> 08:35.540
You could print out what the level is would be zero one, two, three, whatever it is, but we'll actually

08:35.540 --> 08:39.470
use the number of the level to put in some dashes.

08:39.740 --> 08:49.460
So we'll put new string and then inside of here a single quote, a dash, single quote, and then a

08:49.460 --> 08:53.660
comma, next node dot level.

08:54.050 --> 08:57.920
And that will be plus.

09:00.020 --> 09:02.510
To the name.

09:02.990 --> 09:08.990
So this whatever this number is here, it will print out this many dashes.

09:08.990 --> 09:15.890
So if you're on level five, for example, there'll be five dashes now in the for loop, it'll be next

09:15.890 --> 09:20.930
node dot, node dot, children dot count.

09:21.050 --> 09:27.200
And then what we're pushing again down here is this basically.

09:27.620 --> 09:36.950
So we want a new node level, bracket level equals.

09:36.950 --> 09:38.750
Now, what's it going to equal at this point?

09:38.750 --> 09:47.600
Well, if we've gone to the children, it's going to equal whatever next node up here was level plus

09:47.600 --> 09:52.220
one, because this is the one that's the parent that we're getting the children from.

09:52.220 --> 10:01.430
So if we just copy that and put that in here and then put plus one and then we'll put a comma and this

10:01.430 --> 10:04.130
will be node equals that.

10:04.130 --> 10:14.330
And don't forget the squiggly bracket on the end and this will be next node dot, node dot children.

10:14.570 --> 10:18.440
Okay, so that's how we can do it with this struct.

10:18.440 --> 10:20.540
And of course you can put that struct up the top of your code.

10:20.540 --> 10:26.030
I'm just putting it there so that you can actually see it in the same screen as the rest of this case.

10:26.030 --> 10:27.380
So save it.

10:27.650 --> 10:30.470
Let's go back into here.

10:30.830 --> 10:39.110
Now, when we press play and we come down and have a look at our tree, have a look at the lovely structure

10:39.110 --> 10:39.440
we've got.

10:39.440 --> 10:39.650
Okay?

10:39.650 --> 10:42.200
So we've got our tree at the top.

10:42.200 --> 10:45.770
Then we have a dash for our first level steel something.

10:45.770 --> 10:50.150
And if you come down to eat something, you can see that that's got a single dash as well.

10:50.150 --> 10:56.750
And the children of each of these go to Diamond, go to Van are now indented by two.

10:56.750 --> 11:01.550
If you want to exaggerate that, you could put more than one dash in there that's being repeated.

11:02.600 --> 11:12.440
Now that we have this, we can use it any time just to check that we know our tree is correct throughout.

11:12.440 --> 11:19.400
So it's just a nice little way to debug that as well as learn an algorithm for traversing through your

11:19.490 --> 11:20.720
behavior tree.

11:20.930 --> 11:21.350
Okay.

11:21.350 --> 11:25.370
So let me just go back in the code here in case you've been following along.

11:25.370 --> 11:26.000
Exactly.

11:26.000 --> 11:34.340
I'm going to go back into my rubber and I'm actually going to get rid of my eating of the pizza because

11:34.340 --> 11:37.400
it's actually not really relevant at this point.

11:37.400 --> 11:41.930
And if you want to leave that print tree in there, well, you might as well, because if you've gone

11:41.930 --> 11:45.890
to the trouble of creating it and it'll come in use.

11:46.070 --> 11:46.550
All right.

11:46.550 --> 11:48.530
So that's printing your tree.

11:48.530 --> 11:55.580
In the next video, we're going to come back and start looking at actions and how to get our character

11:55.580 --> 11:56.810
to move.
