So far we've built all the fundamental nodes for a behavior tree.
So isn't it amazing just how simple it really is underneath and how you can build up a very complex
structure of behavior with them?
So we've looked at the sequence nodes now they are denoted by an arrow which I've put in here, and
the children of the sequence each get run until one of them fails.
So it's kind of like ending them together in an end statement, always starting with the first child
that goes into that list of children and working through them in order.
Now we also have created a selector node and the selector allows you to have multiple children again
and it will work through them, but only one of them has to be successful.
So if the first one is successful, it will return the value back up to its parent.
If not, it continues on to the next one in the list and performs that instead.
So that's sort of when you're making a selection between multiple actions that kind of all result in
the same kind of thing that we've done with these doors.
So you might have like ten doors into that gallery and the agent might be able to actually go through
and check all of those ten doors, if it had to, to get in.
And you might even include windows.
You might climb through a window if you can't get in through a door sort of thing.
But we only need one of those to be successful in order for him to continue trying to steal the diamond.
Now, what we're going to move on to is having a look at another type of node, which is a condition,
and the condition is really just another leaf like an action.
But it's it's actually much, much simpler because rather than doing any particular process whatsoever,
it's just testing a certain value.
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
of the steel sequence to make the agent go and steal something if he doesn't have enough money.
But if he does have enough money, he's not going to do anything.
So the condition in this case is to check if the robber has enough money.
So we're going to begin in the code by adding in a property to store the agents money.
So if we come back into the robber behavior in these property, I'm going to add a property for money.
It's almost like an inventory system, I guess.
Very simple, of course.
So I'm going to make it so that we can slide it in the inspector.
So the range for the amount of money, let's go from 0 to 1000 and then this will be a public int and
we'll call it money.
And let's say to begin with there's 800.
So as long as he's got more than let's say $500, then he won't go and perform the stealing.
But if it drops below that, he will go and do that.
All right.
So there's our inventory now.
Our condition is that there's more than $500 in there.
So we need to create this leaf node now, a leaf.
If we just go down a little bit, a leaf is a node with a name, but also a method attached to it.
So we have to create a method that tests for the money.
Now that's really, really straightforward because we're already creating all of these methods here.
And essentially they're kind of a test to see if a task is finished running, but we're just doing a
simple check.
So in here, I'm just going to grab maybe this one, the simplest one we've got, and I might just put
this up above here and let's call it has money.
And inside of here, we're going to return.
First of all, if money is greater than or equal to 500, then we will return.
Node dot.
Status dot.
Success.
Or in our programming speak that would be a true.
Otherwise we're going to return node status dot value.
So this is a node that doesn't have a running status because the logic just gets performed and then
returns success or failure.
Now, because it's in a sequence, if he has money that returns success and that allows that sequence
to keep running.
If he doesn't, it is a failure and therefore it won't.
And I've just realized I've got these around the wrong way.
If has money greater than 500, then we actually want to fail because this is a condition where he's
got less than 500 in order to trigger it.
So we want it to be successful when it's less than that.
So put this in.
Okay.
Right.
I've got my logic right now.
So there's my has money, so let's save that.
So let's go up here and create that leaf for having money.
So I'm going to put that doesn't really matter where we put it in here.
Leaf.
Has got money equals new leaf has got money and then in here has money.
Then with that leaf we can put it into our steel sequence as the first thing because it's what's going
to trigger it off to begin with, or it's not even going to trigger it off.
It's just going to stop that sequence from running if the money is above 500.
So let's put steel dot add child.
And we're going to add in has got money in there.
Now, the final thing we need to do is to come down into our update where we're actually running our
tree, because our tree here is going to say if it's running, then process it again, process it again
and process again until it stops.
But let's just have a look at what happens if I save this and go back into my back into unity.
Now we find our robber over in the inspector.
You can see there's a slider for money, so let's press play.
And he's not going to do anything to start with now because he's been stopped by that new condition.
What we would expect is if we slide this down below 500, he should start doing something, but he doesn't.
So what's actually happened here is that in that update for the tree status of running the tree, the
tree runs through once because he's got enough money.
That whole sequence fails and it never comes back and tries again because this is only like one run
through of the tree.
It's going to keep processing the tree while it's running.
And then once it gets a fail or succeed in this case actually getting a fail, then it's not going to
bother doing it again.
So our condition for running our tree over and over is now changed.
Instead, we actually want to say if the tree status does not equal success, then we're going to run
it again.
Okay?
And whatever you want with your particular behaviors, with your trees and how you want them to work,
some trees will run over and over again.
Some will run just once.
You have to put the logic in here to make sure that is so and then you can trigger them elsewhere.
I mean, you could load up a tree during runtime to put into your agent when some other condition or
some other scene loads or something.
It might be completely different behavior tree.
Right, so we'll save that, go back into unity and now we're going to press play.
And so he's now got 800.
I'm going to slide that money down in the inspector to below 500 and look what happens.
As soon as it gets below, he's going to go and act out what was happening before.
So he'll go around and he's going to go on execute that, and then he'll go get back to the vehicle,
which is where everything ends and.
So essentially he's stolen the diamond because he wants his money to go up.
When he gets back to the van, currently, it's not going to go up because we haven't actually coded
that in.
So I'm going to leave that as a challenge for you.
What I'd like you to do is have the money over here.
So the money thing we've created in our script to increase in value when he gets the diamond back to
the van and at that point he can leave the diamond in the van and to signal that you can just destroy
the diamond object because it's not needed any more.
Okay.
So pause the video now.
Go do that.
When we come back, I'll show you how to do it.
Okay.
Welcome back.
How did you go?
Well, let's go back into our code so we know that is picking up the diamond.
We know he's taking it back to the van and he's taking it back to the van up here.
Go to van.
So when go to Van is successful, which means he's completed it, that's when you want to sort of sell
off the diamond and add the money.
Now, in order to do that, we need some logic very similar to what we've done with Go to Diamond.
We actually running this, go to location and then waiting for it to become successful and then doing
our logic down in here.
So I'm basically just going to copy this out of here.
Let's put this down in our go to van and we've got to go to location, which will be Van here.
And we're not going to parent the van to the agent at this point.
So we're going to the van.
What we want to do here is put money plus equals.
How much is that diamond going to be worth?
Let's say $300.
And then we can actually get rid of the diamond altogether so we could go diamond dot set active to
false.
So it's still there, but we've made it sort of invisible and taking it kind of out of the equation.
Okay, so we'll save that.
Let's go back into unity, try that out.
So we press play.
He's got still he's got $800.
And I'm going to just put that down just below 500 to trigger that behavior.
And then off he goes, he'll go and get the diamond.
And once he does take that back to the van, we should see the money going up.
So as soon as he got that diamond, we can start watching over in the inspector for that money value
to go up.
And at the same time, you'll notice that the diamond is going to disappear altogether out of your hierarchy
of game objects.
So actually, I'm going to watch him first.
It's going to come back here to the van.
Diamond disappears and let's go over to the inspector and his money has gone up.
Fantastic.
Okay, so you've now done that.
And believe it or not, you've now covered all the fundamental nodes that make up behavior trees.
You've got enough knowledge and skills now that you can actually take this and do a bit of Googling
on behavior trees to have a look and get some more ideas how other people have actually expanded on
the nodes.
So don't be afraid to even add more nodes to your system that do other things.
Just make sure that you always stick to that control flow of returning success, failure or running.