‫Hello and welcome.
‫In this lecture we are going to be looking at having some choice in our behaviour trees such that I
‫can have an enemy run towards me and if he loses sight of me, he comes to my last known location doing
‫basically two behaviors a chase and then an investigate behavior.
‫Let's dive in and see how that's achievable.
‫Using behavior trees.
‫So while this patrolling behavior is very instructive, it's not really the kind of behavior that we
‫want to implement.
‫What we'd rather have is something along the following lines.
‫When we can see the player, we go and move as close to them as we possibly can.
‫And when the player goes out of sight, then we try and move to the last known location of the player
‫to find them.
‫And once we have exhausted that option, maybe we go back to a different location, or maybe we just
‫stay there for now.
‫So how can we achieve this?
‫Well, at the moment, we've got no way of achieving this with a sequence of actions.
‫We need to make a choice based on the data that we have available.
‫We need to choose between whether we can see the player or whether we can't, whether we've got a last
‫known player location or not, that sort of thing.
‫So to do this, we need a new type of node in the behaviour tree called a selector node.
‫So if we right click and look at our composites, one of the options there is the selector.
‫Now the selector as opposed to the sequence runs thusly.
‫If you have a bunch of tasks underneath the selector, it is going to run until the first one succeeds.
‫And basically it's expecting all of them to fail until one of them succeeds.
‫Now the sequence did the opposite.
‫It runs all of them until one of them fails.
‫So why is that useful?
‫Why is it useful that the selector runs one of them and still one of them succeeds?
‫Because basically we want it to choose the first option that is viable.
‫Now, the way we're going to do this is not by actually running tasks underneath the selector, but
‫running sequences underneath the selector.
‫So at the root level, we're going to be doing a selector node.
‫And then beneath that, let's just move this sequence aside and create other sequences.
‫We're going to have one sequence over here, and what we can do is we can actually rename the sequence
‫from being sequence to something like Chase, and then we can add in another sequence here, which is
‫going to be along of the lines of investigate.
‫And these are basically our two behaviors that we want to be able to choose between.
‫And in order to make them fail, we can add things to nodes called decorators.
‫So if you right click on a node and go add decorator, you can see here there's a bunch of options here
‫and they basically change the way this node works.
‫Then they can do that in a number of ways.
‫For example, we've got this conditional loop which allows you to loop through all the actions again
‫in the sequence as many times as this conditional is correct.
‫We've got other things here, like a cone check, which is about visibility and other such lovely things.
‫We got just a pure loop.
‫We've got a time limit, so it's a can only we can only stay inside this sequence for a certain amount
‫of time.
‫Basically lots of things that modify the behavior of this sequence.
‫But the one that we are interested in is the blackboard, because this is a blackboard condition.
‫And as you can see from the writing underneath of it, the default is something along the lines of is
‫this self actor set?
‫And if this is not set, then automatically this node and everything underneath it will fail, which
‫means that our selector will go on to option two, which is investigate.
‫So in this case, we actually want to change this up.
‫We want to change how this works.
‫So our player location, we want this to be such that it is only set when the player is visible.
‫And then we want to add in a new variable here again, a vector which is going to be the last known
‫player location and the last known player location when the player is visible is going to be the same.
‫But once the player becomes invisible, the player location gets unset and the last known player location
‫is where we're going to want to go.
‫So we can do this by going to our Blackboard Base condition and just changing it to being the query
‫to be Yes is set, but the thing that we want to be is set is the player location.
‫So if the player location is set, then we're going to give Chase and we're going to give Chase by doing
‫basically just a move to and we're going to move to the player location like.
‫So now we're not doing much else under this sequence.
‫So in actual fact, you could put this blackboard condition directly on the move too, but because we
‫might want to do other things such as shooting, I'm going to leave it as a sequence for now.
‫And then we've got this investigate.
‫Investigate is basically also going to do a move, too, but it's going to move to instead of moving
‫to the player location, it's going to move to the last known player location.
‫And you can see we don't have a condition on that because this falls through if this condition fails.
‫One important thing to note about these blackboard conditions is that by default, once we have made
‫the decision to start executing this node, we're not going to reevaluate that.
‫So if for some reason we lose sight of the player while we're moving to that player's location, we're
‫going to keep trying to get there.
‫So if we want it to be able to abort the execution of this node, then there's an option for that.
‫If you go to the flow control, there's notified observers and there is underneath it.
‫Observer aborts at the moment set to none.
‫But you can select from a couple of options.
‫You can set it to self.
‫So this is saying if this condition suddenly becomes false, then stop whatever you are doing and reevaluate
‫your selector and see.
‫Maybe we now need to do the investigate.
‫Note The other option here is lower priority and you can see that it's changed what's highlighted here.
‫It's saying if suddenly this blackboard condition became true, stop whatever else we were doing and
‫start executing this chase condition.
‫And then the final option is both.
‫And this is the one that we want.
‫Basically, it's saying that this blackboard based condition, if it becomes true, we start executing
‫this sequence immediately.
‫If it becomes false, we stop executing it immediately or stop executing whatever else was going on.
‫We just basically want to always go and do whatever path is dictated by this blackboard condition.
‫So hopefully that makes sense that the observer aborts stop things in action, otherwise it will wait
‫until the action is finished before re-evaluating.
‫So really, now we want to go ahead and implement our last known play location and player location,
‫because at the moment, if we go over to our C++ and give ourselves a little bit more room, then you
‫can see that we're only setting this player location in Begin Play.
‫Ideally, we'd be doing this every time we have line of sight to our player pawn, so we can actually
‫uncomment our line of sight code here.
‫And we probably want to uncomment the else code as well.
‫But we're not in this case going to be clearing focus and stopping movement directly.
‫I can go ahead and remove these entirely because.
‫That's not the way we're doing things at the moment.
‫Instead, what we want to be doing, if I remove the code, is we want to be in here setting the player
‫location.
‫We also want to be setting the last known location there and in else we want to clear the player location.
‫Now to do that, we have got a function available to us which is instead of the set value as vector,
‫if we go ahead and open our blackboard component, there is a clear value and this just takes the f
‫name, doesn't need to take a type or anything or because we're just completely erasing whatever was
‫in that variable.
‫So here I've got the challenge for you to fill out our tick set.
‫The two variables when line of sight, basically fill out this first bit of the if statement and then
‫clear the player location when we have the alt.
‫So when we lose line of sight, we don't have a player location anymore.
‫Reduce the speed of the enemy that's in the world so that you can get away from him more easily.
‫And then check the functionality, check that you're able to hide behind a corner and that he goes to
‫the last known location to try and find you.
‫Pause the video and have a go at this.
‫Okay.
‫Welcome back.
‫So let's give this a shot.
‫First of all, we're setting the play location.
‫I'm going to move that out of begin play and move it here instead.
‫So we're going to set the player location to we need to be getting the player pawn here and tick again.
‫So I'll uncomment that line and then we're doing exactly the same thing, exactly the same line of code
‫for our last known player location here.
‫So let's go to our blackboard and just make sure we copy it so we get exactly the same name and then
‫we just want to clear that's the final thing is to clear our player location here.
‫So it's going to be clear value and it's going to take this text player location as the value that we're
‫trying to clear at the end.
‫And we can go ahead and compile and see if this works.
‫Well compile was a successful.
‫The other thing I said to do to make our debugging easier was to select the enemy and we can go and
‫change their speed by going down to their movement component and finding the walking and finding their
‫speed.
‫So Max Walk speed is 600 here.
‫Let's set it down to 400 so we can easily get away from them.
‫They're going to walk a bit slow and go ahead and play.
‫They are walking towards us slowly.
‫We can run away.
‫We hide behind a corner and here they come looking for us each time.
‫But hopefully if we hide behind two corners, they won't be able to find us.
‫So I hide there, hide here, hide here.
‫And he's going to go and investigate at the last known location, you see, he's just poked his head
‫around the corner, but then he's stuck there because he doesn't know what to do.
‫And if we were to go and have a look at our behavior tree and just put this over to one side and the
‫game to the other and give myself a little bit of room.
‫Then if we have a look at the behavior tree, we can see it's kind of jazzing around here because it
‫can't do either of them.
‫It's trying to move to the last known location that's succeeding.
‫So it's getting a bit stuck here.
‫But if I come into view, you can see he's moving towards me.
‫If he loses sight of me, then he goes over to the investigate condition.
‫So lose a sight.
‫He's going to investigate.
‫And then he comes and sees me and then he goes back to chasing me.
‫So this behavior is actually working.
‫Now we're able to do a choice between trying to chase and trying to investigate based on this blackboard
‫behavior.
‫Let's just change the name of this blackboard to behavior here to can see player question mark.
‫So that's going to be much easier to read.
‫Now we've got this selector can see the player, then we should chase, otherwise we should investigate
‫and we can delete this sequence over here because that is no longer in use.
‫So hopefully now you've got a bit of a better understanding of how behavior trees can be linked together
‫to create choice and sequence and put lots of tasks together to make more complex behaviors really easily.
‫In the next lecture, we're going to be looking at how we can make our own custom tasks in C++ so that
‫we can get some shooting action.
‫I'll see you in the next video.