Before we start writing our code, let's just remind ourselves what we're aiming to build.
And that's basically this tree structure here.
So our behavior tree is made up of a whole bunch of nodes, and a behavior tree is just a graph where
each box, I guess is a node which has some action or condition or whatever associated with it.
And the lines show which ones are connected to which.
Now, if you remember, the ones right at the bottom are the actual actions or things that will happen
and that will make your character do it's behavior that you will see on the screen.
The other ones link it all together.
So there's two type of leaf nodes is an action and a condition, and we'll get to those a little bit
later on.
Then we have our sequence and our selectors, which will also write a little later on.
But all of these are basically nodes and so a node will be our base class that we will implement.
And when we start writing this, you will be absolutely surprised at just how simple the code is and
yet how powerful it becomes.
Okay, so let's go ahead and write our Node class.
Now, I will put all of my script into a new folder.
There's already a B tree folder that you'll find when you've brought in that project, but that has
the NAV mission, so we don't want to use that.
So let's just go down outside of everything.
So I don't create it inside something else.
And let's call this behavior tree, or you could call it script if you want to put it in there, let's
open this up.
Okay.
So in here, we're going to create a new C-sharp file and we're going to call it Node.
Then you want to open that up in your editor.
Now, as a very basic class, it's not going to be a mono behavior.
In fact, all of these classes won't work on mono behavior.
So we have to design them in a way that they will update themselves.
But because it's in a tree structure, it's going to run from the top root node of the tree, and then
the whole structure will look down to find the first available leaf that should be run and then run
that.
So let's get rid of Start and also update.
Now any particular leaf or any node actually can have one of three statuses.
It can have that it was successful when you attempted to run it, that it failed when you attempted
to run it, or that it could be in a state of running, which means it's still being executed.
Okay.
So we're going to create a public enum for that and we'll call it status and we've got three so successful
or success running and then failure, and then we need a value to actually store that in so that we
can record which one we achieved.
So public status and then we'll just call that status.
Okay.
Now each node can possibly have children nodes.
Okay.
So if it's the root node, it has the nodes immediately below it as its children, the selector and
the sequence nodes, they have actions and conditions as they're children.
So basically every node can have a set of children.
Of course the leaf nodes are going to have an empty set of children, but that's okay.
We're just going to store them in here.
So I'm going to create a public list to store those in four nodes.
So a list of nodes, children, and we will make that equal new list node like that.
Okay.
Now every time the node has to find out the status of any of its children, it's going to run through
them basically from left to right, as though it's reading one after the other, especially if you're
in a sequence, for example, and you've got three things that you want to do in sequence.
You want to have them put into the list in order that they will run.
And then you need the node to actually keep track of which one it's running at any particular time.
So we'll create a public int and we'll call that the current child and we'll set that to zero.
So right at the beginning it's going to be looking at its very first child notice of one.
It's going to run first.
Okay.
Now, to make your own debugging life a little easier, we'll.
Also give each note a name.
And this is just so that we can print it out and see what our tree looks like.
So public string name like that.
Then we're going to create a constructor.
So public node and we'll put string in and in here we'll put name equals and and we will also make a
constructor without anything in it because C-sharp likes to do that.
Now in this case you'll end up with name is practically empty, but we will use this one here instead.
Now, the other thing I want to do before we just finish up with this for now is add in a public method
for adding our child.
So public void add child, we're going to pass it a node n and then inside of here we will just go children
dot add and we'll add n to it.
So the first one that you put in to the list will be at the top of the list.
So that will be child zero.
And then you'll get child one, child two as you go through that list.
And therefore you have to put them into the list in order that they appear or that you want them to
execute within your behavior.
TREE okay, so we've got that.
Let's now save it.
Let's go back into unity.
We've got nothing to run at this point, but we want to get it to a point where we can actually create
a tree.
We're going to create a script for our behavior tree.
So let's call this behavior tree.
Now I'm using the Australian spelling of behavior.
It's up to you which one you use.
Let's open that up in our editor.
And once again, we'll we'll get rid of mono behavior, but we'll actually say that our behavior tree
is basically a node because it is it's the root node and has children underneath it, which then have
their own children.
They're in their own children.
So public behavior tree.
Now, of course, you don't even really need this particular class because you could just create a node
and call it root and then use that.
But just for some kind of completeness sake here, I'm just going to make sure that I distinguish a
behavior tree or something different.
So let's call this name equals tree.
And of course you could pass it through a particular type of behavior if you wanted a sort of a guard
and PC behavior for what it should do.
It could have a particular behavior tree and then you could have another behavior tree.
So this this class here could be expanded in the future to kind of read in a behavior tree that you've
written up as some kind of JSON file or XML file or even just text to load in the actual behaviors.
We won't be doing that here, but that's the kind of thing you could do with this.
And then let's put in public behavior tree and we will pass through a string in and set up the name
in here.
Okay.
So let's save that.
Now let's go back to Unity and we will create another script.
And this will be the script that we actually put on our thief, our robber object.
So let's create C-sharp script and we will call this robber behavior.
Open that up.
And this time we will keep it as a behavior, because this is actually going to go on to our agent and
it's going to run.
So up here, the first thing we'll do is actually create a behavior tree so we can go behavior tree,
let's call it tree.
And then in the start, we're going to construct a very, very simple tree.
So tree equals new behavior tree, and I'll just let it be called tree at this point.
Then we create a node and the node under that is going to be steel, and that's going to equal new node
steel, something we'll call it.
And then what's entailed in stealing something is, well, we can steal the diamond, for example.
So we'll create a node for go to diamond and we'll equal new node, go to diamond, and then another
node go to Van, which is the little truck that's in the scene there.
So we're going to get our agent to move to where the diamond is and then move back to the van.
So that's going to equal new node.
Go to van.
Now, how do we construct this tree?
We've got to kind of do it in reverse so our steel does not add child first.
We're going to add in, go to diamond and then Steel Dot, add child, go to van and then we get our
tree.
Dot add child for steel.
So at this point, our tree should look very simple and as it does on the screen here.
How do we know that this is correct?
Right.
Well, I'm just going to save this.
I'm going to go back into our behavior tree and create a little print tree method in here so that we
can see something in the console.
Right.
So that'll be a public void print tree.
And then inside of here.
Well, you didn't think I was going to make it that easy for you, did you?
This is going to be a challenge for you to come up with the code that's going to go in this print tree.
Now you'll want to use debug logs because we want this just to print onto the console at any time that
we call it from inside the rubber behavior code or wherever.
So all you've got to do is kind of figure out how are you going to traverse through the tree in the
right order and display it in the console?
Give that a go.
When we come back in the next video, we will complete this code and continue writing the code that
will actually get our little rubber agent moving on the screen.