Welcome back.
In the last lecture, I left you with the task of printing out the simple behavior tree that we've been
constructing.
Unfortunately, this isn't actually a simple task and well done to you if you manage to nut it out.
Even better if you actually went and did a bit of research to find out how to do this.
Okay, so Behavior Tree is basically a graph and there's plenty of algorithms out there that have already
been written for traversing through a graph and printing it out.
Now you can do this one of two ways.
You can use recursion or you can do it without recursion.
Now I'm going to do it without recursion, just so I don't confuse people who aren't all over the way
that recursion works, because it can be a little bit mind bending.
All right.
So let's start out with our print tree.
So in here, I'm, first of all, going to put in a string and that string will be the tree printout.
So I'll store everything in a string and then print it out right at the end.
So that equals nothing at this point.
Now, in order to remember where we are in this tree, I'm going to create a stack and the stack is
going to have nodes in it.
And let's call that the node stack equals new stack of node.
And then we put the current node in there.
So the current node at this point is the basic tree itself.
So we'll go node, current node equals this and then we'll go node stack dot, push current.
Note.
Okay, so we've got the root of the tree in that stack.
Next, we basically start looping around through each node that if it has children, we want to put
them onto the stack and then we want to get off the parents and print them out.
So it's going to look like this while the node stack does not count, does not equal zero, and it shouldn't
the first time through because we've put a node in up here.
So while it's not equal to zero node, next node equals node.
Stick Dot Pop.
So we grab hold of the route at this point because that's the only one in there.
And then we print that out.
So we add it to our tree print out string.
It's going to equal plus equals next node name plus a return.
So we can go down to a new line.
Then we go for int I and we loop around all of the children of the one that we just popped off and put
them into the stack.
Okay.
So in this case, I'm going to loop backwards through the children.
The reason being, if you look forwards through the children, it actually prints them out in reverse
order because of the way that it's popping from the stack.
So I'm going to start at the top end of the children list.
So next node dot children, dot, count minus one.
I is then greater than or equal to zero.
So while we haven't counted down through all the children I minus minus we will add them to our stack.
So node stack dot push next.
Node dot.
Children.
I.
Okay.
Then when we've done that we can go debug log and we're going to print out the tree.
Print out value like that and that will print out our tree.
So.
How did you go?
I guess if you've got something working, you know that it's working so well done.
This is the way that I'm doing it.
If you've got a different method that works for you, then please share it.
If you did it with recursion, then share that too, so that others can learn from your code.
Okay, so I'm going to save that.
Now, back in my rubber behavior, I've actually added in some sort of dummy extra nodes in here just
so they've got something more interesting to print out and I'll delete them a bit later.
So we have our stealing stuff up here.
I've also added in an eight node which will have two children of go to pizza shop and buy pizza, and
therefore I'm just adding them down here the same way that we did for steel at the top.
Of course, you don't need to put these in there.
I just like to see a little bit more data when I print this out.
Okay.
So at the bottom of the start, we can then go tree, dot, print tree to run that method we just created.
Save it.
Now let's go back into unity then.
You want to add the rubber behavior script on to the robber?
As you can see, I've already done here.
And we're going to press play and have a look.
Okay, so play now in the console.
You won't see the entire tree.
You're just going to see the top level of the tree.
But if you click down the bottom here in the actual console, you'll see your tree.
So there's tree still something.
So that's the first child of tree.
And then you've got your go to Diamond, go to Van, then you've got your eat something and go to pizza
shop and buy pizza.
Great.
So it's all in the right order.
Now what would be really nice is if we could indent these to show which ones are children of which.
So just with a little bit of tricky code, if I go back into there, I'm going to add in a very simple,
cheeky little structure just up here.
So let's create a struct or node level and this structure is going to hold on to the node and the level
that the node is at in the tree.
So the root is level zero.
The next node down would be level one.
It's children will be level two.
And then any on the same level as one, they're all going to be ones.
And then their children are two and three all the way through it.
So in here we want to have a public int level and a public node to store the node.
Okay?
So once we've done that, we have to just modify our print tree.
So this stack is now going to have node level in it instead.
So in here I'll put node level over here will be node level.
Then when we push the first thing onto that stack, it's not a node anymore.
It's got to be a node level.
So inside of here will put new.
No level and because it's a struct, we can actually go like this level equals current node and then
know it doesn't need equals the level which will be zero at this point.
Comma.
Node equals current node and then squiggly brackets at the end.
So just take notice of how that struct is being declared and created and pushed through here.
Okay.
So then we have inside of our while loop we want to do the same thing is correct.
Or this.
Now the thing being popped off at this point is going to be a node level.
And then what we're printing out here is the next node, dot node, dot name, and we want to use the
level.
Now we're going to use the level not printed out.
You could print out what the level is would be zero one, two, three, whatever it is, but we'll actually
use the number of the level to put in some dashes.
So we'll put new string and then inside of here a single quote, a dash, single quote, and then a
comma, next node dot level.
And that will be plus.
To the name.
So this whatever this number is here, it will print out this many dashes.
So if you're on level five, for example, there'll be five dashes now in the for loop, it'll be next
node dot, node dot, children dot count.
And then what we're pushing again down here is this basically.
So we want a new node level, bracket level equals.
Now, what's it going to equal at this point?
Well, if we've gone to the children, it's going to equal whatever next node up here was level plus
one, because this is the one that's the parent that we're getting the children from.
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
will be node equals that.
And don't forget the squiggly bracket on the end and this will be next node dot, node dot children.
Okay, so that's how we can do it with this struct.
And of course you can put that struct up the top of your code.
I'm just putting it there so that you can actually see it in the same screen as the rest of this case.
So save it.
Let's go back into here.
Now, when we press play and we come down and have a look at our tree, have a look at the lovely structure
we've got.
Okay?
So we've got our tree at the top.
Then we have a dash for our first level steel something.
And if you come down to eat something, you can see that that's got a single dash as well.
And the children of each of these go to Diamond, go to Van are now indented by two.
If you want to exaggerate that, you could put more than one dash in there that's being repeated.
Now that we have this, we can use it any time just to check that we know our tree is correct throughout.
So it's just a nice little way to debug that as well as learn an algorithm for traversing through your
behavior tree.
Okay.
So let me just go back in the code here in case you've been following along.
Exactly.
I'm going to go back into my rubber and I'm actually going to get rid of my eating of the pizza because
it's actually not really relevant at this point.
And if you want to leave that print tree in there, well, you might as well, because if you've gone
to the trouble of creating it and it'll come in use.
All right.
So that's printing your tree.
In the next video, we're going to come back and start looking at actions and how to get our character
to move.