In the previous lecture, we put together our actions and got our character moving now in order to execute
a plan.
As such, we need to add in sequence nodes.
So so far we've got like a go to door and a go to van node, but they don't actually play out in sequence.
And that's what you want as your plan and you want your agent to run them one by one.
Now, if at any point during the sequence one of those actions fails, then there's no point continuing
with the sequence.
Therefore, you have to take that into account when you're actually programming the sequence node.
So basically what it comes down to is that a sequence node is like an logical, and if all of its children
are successful, then the sequence node actually returns successful to its parent.
If any of the children fail one or more of them, then the sequence node itself fails and it reports
a fail back up the tree to its parent.
All right, so let's create a sequence node in our behavior tree code.
We're going to create another C-sharp file.
This one is going to be called sequence and open that up in your editor.
Right.
Once again, this is going to be a node, not a miner behavior.
And we can get rid of the contents of that.
Now, the first thing we'll do is just create a public constructor.
So sequence and we'll have that pass through the name for it.
And now we will write its process.
So it's going to be a public override node dot status process and actually don't need that node in there,
but that doesn't really matter.
Okay.
So this is going to return status dot running as a default.
Now before that, it's going to basically loop through all of its children and run their process method.
But you can't have a loop in here.
Remember, it's like an update in unity, so you've kind of got to set it running and then see if it's
still running next time this process comes around and runs again.
Okay.
So first of all, what we're going to do is grab the status of running the first child.
So status.
Child status equals children.
Current child.
Now, if you remember back when we first created our node, we made that current child integer.
And that's just keeping track of which child is currently running.
So it's initially set to zero.
So we go back into our node and have a look.
It's up here.
So the first time that we run that in there, it's going to pick up that very first child that's in
that array of the processors or the children processors.
Okay.
Now if this child status equals status dot running, that means that that child is still running, in
which case we're just going to return status dot running K, we're not going to go and run anything
else.
We're not going to set the process to do another child because we're currently in the middle of already
doing something.
Okay.
So this might be, for example, go to Diamond.
The agent might still be making its way towards there, which means that that particular node hasn't
returned a value yet, hasn't been successful or failed.
It's still running itself.
Okay.
Now, if it's not running, if child status equals status dot failure, then we're going to return child
status, basically saying that, okay, this sequence node has now reported a failure.
We're finished.
We're not doing any more children because we can't continue.
One of our children states or our children actions has failed.
Okay, if you can't go to the diamond, then there's no point continuing trying to steal the diamond,
etc..
Okay, so once we've passed that, so if we haven't failed, that means we must now be successful with
that particular child because it's not running and it's not failed.
So current child plus plus we want to go the next time we come in here to the next child and actually
process it.
So we're going to say if current child is greater than or equal to the children dot count.
So if we've actually made it to the end of our list, then child current child sorry equals zero.
We're going to reset that for next time.
We might run this sequence and we're going to return status dot success so that we've basically looped
through all of the children and none of them have reported a failure, which means that we're successful.
We've made it to the end of the sequence.
So let's say, okay, we've done it, we're finished.
Great.
That is your sequence node.
Simple, simple, simple.
But it's just getting your head around how this is actually running.
And it's it's looping, but it's not looping in a single for loop within here.
All right.
So now let's get this going, save that and we'll go back into our robber behavior because this is where
we need to set all of this up.
So we've got our go to diamond and our go to Van, which means our agents going to go to the diamond
and then go back to the van, or that's at least what we want.
Now, I know in the illustration at the beginning of this lecture, I had go to door in there as well
and will add that a little bit later.
We just want to keep this simple for now.
Now we're already adding those two things to our steal node, but still is now going to become a sequence.
So we'll change it from our basic node to a sequence and we'll change it here to a sequence.
Now these are being added to it, and that's fine.
They're adding to it in order that you want them to be processed.
Now, when we start, we run treetop process that runs the tree once it doesn't continue running the
tree, but we need to keep running the tree just in the same way that unity keeps running its update,
which is down here and empty at the moment.
So instead of running our tree up here in START, we actually want to run it in update.
And in order to run it in update, we need to know what it's returning each time.
So I'm going to go up the top here and actually put in a another variable so it'll be no dot status,
which will actually be the tree status equals node dot status dot running as a default there.
And then what we're going to do is take this tree process out of start.
So let's just cut that out of there.
Bring it down into our update just here and then just test.
If true status equals no dot status dot running, then we're going to process the tree.
Now, when we do, we also need to make sure that we update what tree status is so that this will stop
it running again.
Otherwise it will loop around and continue running whatever particular action you're trying to achieve.
So we'll put tree status equals trade up process.
Okay, so we'll save that and now we're ready to try it out.
So let's go back into unity and we'll press play and we should see those behaviors take place where
we're going to go to the diamond.
So the nav mesh is now in control as far as working out the path, we're going to the diamond and then
we've reached the diamond.
So that's actually returned success on there.
And now we can go back to the van.
And then because that tree is successful, we will stop there and it won't continue.
If you take out that tree status equals your tree process in the update, you'll actually see that this
will bounce around and loop through that behavior.
All right.
So we've got that running now if you need to debug this at all, because it is a little going by faith,
I guess, with the way that we have actually coded it for you as you go through this, what you might
like to do is in your processes, put some debug logs to see which process is actually running.
Because remember, we went to the trouble of sending every single node a name.
Okay?
So in here you can actually put like a debug log processing children, current child dot name in here.
So that would allow you to keep track.
And you can also then print out what the status is.
So in all your processes, wherever they are in leaf, do some debug logs and then you can watch what's
going on, watch which one is processing and where your issues may be.
All right.
Time for a challenge now.
If you remember right back to the beginning, we had a little behavior tree that had a go to door in
it between starting over here, going to a door, going to the diamond and then coming back to the van.
What I'd like you to do is to put together this little sequence where we now, first of all, go to
the door, go to the diamond, and then go to the van.
Now, when I say go to the door, actually want you to go to this door over here.
So I want your agent to go to that door, go to the diamond, and then go to the van.
Or in fact, you could even say go to door, go to diamond, go to door and go to Van.
So see if you can put that in.
Pause the video now.
Go ahead and do that.
When we come back, I'll show you how to do it.
Welcome back.
How did you go with that challenge?
Now, you might have found a little hiccup with this as you did it, and I'll talk about that when it
happens to me.
First of all, this door over here, if we find it over in the scene, it's called the back door.
So that's it there.
And that's the one we want.
So inside of our robber behavior code, we need to make room to pass that through.
So let's go back up to the top here and we'll put public game object back door like that, and then
we need to create a leaf for that particular thing.
So we want a leaf here.
Let's take that and I'm just going to copy and put it down here.
So go to door and let's call it backdoor.
Just to be specific here, go to Backdoor and then we'll put go to backdoor in here and go to backdoor
as our method which we then need to create.
So let's come down the bottom here and we've got go to Diamond.
So let's just make a copy of that, go to back door and then our go to location will be.
Back door doc transformed position.
The rest of it will be okay.
Let's come back up here and add in the go to back door in our sequence.
So actually want that to be the first one, don't we?
So let's go up here and put steel dot go add child, go to Backdoor, go to Diamond and then we want
to go back to the back door to get out and then go to the van.
So let's save that, go back into unity and we'll press play and have a look at what we get.
Of course, after we go to the rubber code and pass through that backdoor object to our script.
So there it is there press play and now let's have a look at it playing out.
So immediately the first thing you would have noticed is that he looks like he's going to the front
door, but he's not.
He's taking a shortcut to the back door via the front door.
Now that is because of the nav mesh.
It's finding a shorter path through that gap in the front door.
So but at least he went there.
I guess now he's gone to the back door, now he's going back to the diamond.
And once he does that, he's going to go back to the back door.
And then I'll assume here that he's going to make a shortcut via that front door.
Oh, now he's calculated differently and go back to the van.
Obviously.
It sure did to go that way.
So great.
Now, the issue is that he's gone to the back door because apparently the front door is locked and he
can't get through there, but he can in this case.
Now, that's, as I said, a nav mesh issue, and we'll sort that out in coming videos.
Okay.
So that is sequences.
When we come back in the next lecture, we're going to look at selectors.