Behavior.
Trees are an er technique used to control the behavior of NPCs.
They became popular after use for the NPCs in Halo.
They are a special kind of graph called A Tree.
As the name would suggest, its structure begins with a single root and extends out from that adding
branches, nodes and finally end nodes.
There are three types of nodes composite, decorator and leaf.
A composite node is one that can have one or more children nodes.
A decorator node can have only one child.
A leaf node is a node at the end of a branch and corresponds to an actual action in a game.
A group of leaf nodes at the end of a branch may represent a sequence of actions or a set of actions
to select from how they operate.
Depends on the type of composite node of which there are two types sequences and selectors.
When processed, a sequence node will ensure that each child is visited in order.
A selector node, however, indicates that only one of its children need to be visited and that child
can be picked at random.
Let's take a look at an example.
Imagine you or your NPC are hungry.
You have a decision to make about what to eat.
This could be shown using a selector node in which you can select from just one of the children.
You don't need all of them.
So this is when a selector comes into play, when just one leaf node needs to be executed to perform
the behavior of eat.
However, if your NPC's actions were at a lower level than this, such as in The Sims, then you could
associate the eating of each item with a well-defined process.
In this case, you'd need a sequence node for simplicity.
Let's say such a sequence includes gather ingredients, make pizza, cook pizza, and eat pizza.
Notice each leaf node corresponds to an actual action.
It's important that the leaf nodes can be performed with a logically or with animation because these
are the actual things you will see the NPC doing on the screen.
They are also self contained.
This means they can be performed and then the sequence abandoned.
Nodes can also represent decisions that when in a sequence can cause a sequence to continue or fail.
In this case, the behavior tree will execute completely only if my hungry and do I have food is satisfied
at which the decision to eat, burger or eat pizza will be satisfied.
Behavior trees can be as simple or as complex as you need, including actions at all levels, joined
by different composite nodes.
So how does all this work in code?
Well, each leaf node must have a matching method that can be executed.
The behavior tree itself is a kind of scripting language.
Sitting above your code base, you must write a bunch of self-contained modules in a C-sharp file that
can be called when a leaf node is being run.
The method itself will have to report whether or not it has failed or succeeded.
This gives the leaf node a fail or success state, which is reported back to the running of the behaviour
tree.
In the case of a sequence, this is essential.
As if one leaf fails, the entire sequence fails.
In this section we're going to build the basic skeleton of a behavior tree system so that you can get
to see what all the major code is going to look like and put together your own little simulation.
So just in the same way that I did in the Hope series, the goal orientated action planning, I've created
another little scenario here, and this scenario has an agent on a nav mesh again that I've built.
The agent in this case is a robber, and he's going to go and rob something out of the little gallery
that I've created over here.
Now, if you bring in this data project that's attached to this lecture and open it up in unity, you're
going to find this in the bee tree scene.
The nav mesh will already be built.
Now there's two door objects in here.
They're not actually part of the nav mesh, but we will make them nav mesh obstacles a little bit later.
So this is already set up and ready to go for you to use.
So when we come back in the next lecture, we'll actually start writing some code to get our robber
moving through the gallery.