‫Hello and welcome.
‫In this lecture, we've got our eye that is following us around the world.
‫It will go and investigate when he loses sight of me.
‫So it's quite hard to lose.
‫But if I go too many corners away, he's not going to be able to keep up and eventually he's going to
‫lose interest and go back to his start location over there.
‫That's what we're going to be implementing in this lecture by finishing off our behavior tree task.
‫Let's dive in and see how it's done.
‫So we want to get our task to execute, to actually do something.
‫And particularly we want to be able to clear that blackboard value, do what it says on the tin.
‫So how do we go about doing that?
‫Well, for that, we need to go up to our base class and have a look at what's implemented there.
‫In fact, we need to go up one level higher, which, as you can see from the inheritance of the task
‫blackboard base.
‫Above that we have the BT task node.
‫So let's use F 12 to navigate to the implementation there.
‫And if you scroll up to the top of the BT task node header file, you can see there's the information
‫about this class.
‫And the most important thing is that these are the virtual functions that we're most likely to want
‫to implement.
‫So the first one is execute task.
‫This is called when the task starts to execute, we then have a board task, which is when it's decided
‫that we need to stop executing, for example, because a condition has become false and we've got that
‫signalling set up.
‫So it aborts this task.
‫Tick task happens all the time that the task is executing, but not the first tick.
‫So the first tick will call execute task thereafter.
‫We'll keep calling tick task every time.
‫And that's for behaviors that might run for longer than one tick, such as moving to an enemy might
‫take multiple ticks.
‫So tick task allows us to do something.
‫Every tick and on message is for other things other than aborting that might send a message to this
‫task.
‫We don't really have to worry about this particular function.
‫So the first three are really the ones we're more interested in, and seeing as our task is simply just
‫going to clear the blackboard value as soon as it is executed and then it's going to finish straightaway.
‫It doesn't need to do things across multiple ticks.
‫We're interested in the execute task, so if we search in this header file for execute task, you can
‫see the full signature is here and the return type here is this boot node result.
‫And it takes in two things the owner component and the node memory.
‫Now the reason it takes in node memory is a little bit complicated, but the important thing to know
‫here is that a task class or a task class instance, there's only one of them per all of the blackboards.
‫Essentially, they get shared between everything, which is why if you want to save some information
‫that's related to a node in one particular behavior tree, then what you need to do is store it in this
‫memory pointer.
‫But we're not going to be doing anything related to that.
‫It's a little bit more advanced to need to do that.
‫And there are other ways to get around this, such as allowing Unreal to create multiple instances for
‫every behavior tree, which might be a little bit neater or not, but this is a slightly more efficient
‫way and we don't have to worry about it.
‫Ultimately, what we're interested in is the fact that we can get to our own a component, the behavior
‫tree component, and from there we can get to other things such as the pawn, etc. But that's also the
‫subject of another lecture.
‫So what's interesting and important now is just to implement our execute task.
‫So we're going to copy the signature, go over to our clear blackboard value H and let's just see as
‫this public note, I think this is going to be protected because it's in the default section.
‫So we're going to create ourselves a protected section and paste it in there and then we're going to
‫go ahead and create the or put an override just to make sure be safe.
‫And then we're going to create an implementation over in the C++, remembering that we always want to
‫be calling to super when we're overriding so super execute task with the owner component and the node
‫memory being called up to our parent here.
‫And then what we want to do is get hold of our blackboard so that we can clear the key.
‫And there's two things that are important here.
‫First of all, how do we know what Key was selected?
‫Well, if we go to our Blackboard Base H, you can see that one of the methods there is get selected
‫Blackboard Key.
‫So you can easily just call this in your C++, get selected Blackboard Key, and that's going to return.
‫If we hover over it, it returns an F name, the F name of the blackboard key that was selected in the
‫behavior tree.
‫Now, the other thing we need to do is to be actually actually able to get hold of the blackboard related
‫to this behavior tree.
‫So for that, we've got our own a comp.
‫Now notice here that the type of owner comp being passed in here is actually a reference, not a pointer.
‫So it can't be null for a start.
‫That's great.
‫But it also means that we need to use the dot operator rather than the the arrow operator because we're
‫not using a pointer and from here we can simply get blackboard component.
‫So that just gets the blackboard component if you hover over it, this one is a pointer to a blackboard
‫component, so you will need to use arrow operators.
‫So we already know how to clear keys on blackboard.
‫Opponents, that's going to be your challenge.
‫So hopefully the challenge is not too complicated.
‫You want to clear the selected key.
‫We you can check where we've done this before.
‫Do you need any includes in order to do this?
‫Maybe the blackboard needs to include use the key selected in your behaviour tree as the key that we
‫want to clear.
‫So pause the video and have a go.
‫Okay, welcome back.
‫So let's give this a shot.
‫So the first thing is we've got our blackboard component.
‫Let's use the error operator here.
‫Nothing is coming up.
‫So I'm assuming that I need and indeed there's some red squiggles I need a hash include here to do the
‫behavior tree and then the blackboard component here and now.
‫Hopefully my autocomplete should be working, which will allow me to have the clear.
‫Let's just try and refresh our memories.
‫Where have we done this before?
‫If we go over to Shooter I, Controller CP and have a look at what we've got here, we've got clear
‫value.
‫That's the one we're looking for.
‫So let's try that clear value.
‫And what does it take in?
‫It takes in either an F key or an F name, great f name we can get.
‫And where did we say we could get that with the get selected blackboard key that is on this class because
‫it's in the parent class.
‫So get selected Blackboard Key and it's as simple as that.
‫We're clearing the value of the selected Blackboard Key.
‫Now, one final thing we need to do.
‫This wasn't part of your challenge, so don't worry.
‫If you didn't know this is that we need to return because there is a return value for our clear blackboard
‫value execute task.
‫And the return value is quite interesting.
‫Let's have a look.
‫Let's follow it with an F 12 and see what different status is we can return from our execute test.
‫Now the first one is succeeded.
‫Great.
‫That basically signals to the behaviour tree that this node is finished executing and we can move on
‫to the next one.
‫Failed again.
‫This is a finished so we can move on to the next one, but this time it's saying that it failed.
‫So that gives the different indications to, for example, as sequence nodes or our selected nodes as
‫to what those nodes should actually be doing.
‫Then we have got aborted, which is basically the same as failure and we have in progress.
‫If you return in progress from an execute task, it's basically going to say you've got to keep calling
‫tick until I tell you otherwise, until I say that I have finished now we're not going to be using this.
‫But it's good to keep in mind that if you need to create a task that is long running, we'll run across
‫multiple ticks.
‫Then we're going to have to return in progress.
‫But as I said, we want succeeded.
‫So let's go in and do that.
‫Return EB Beat node result colon, colon.
‫And it is going to be succeeded because we succeeded to clear that value basically no matter what.
‫So we can go ahead and save all of this, go over into the editor and hit compile.
‫And when that's complete, let's go ahead and open up our AI and the behavior tree.
‫So where's that one beat?
‫And I dock it to the main window and let's have a look.
‫We've got a clear blackboard value which should now be able to allow us to move to the last known location,
‫clear the blackboard value, wait for five, and then we should see that investigating this last known
‫player location should be unset, so we should move back to our start location.
‫So let's see if this behavior is now working now that the task is fully implement.
‫So he's coming over to me, he's investigating me.
‫I'm going to go and hide.
‫So let's go and hide.
‫He's coming to investigate.
‫Let's go and hide again.
‫So go and hide.
‫Let's see if we can spot him over there.
‫He should be waiting at the moment and then he's going back to his start location, which unfortunately
‫makes him intersect with me.
‫So let's see if this is actually working as we expect, by bringing out our enemy a window and docking
‫it over to the right and putting our main window over on the left.
‫It's a little bit cramped, but we can we can make do and then we're going to try and escape this guy
‫one more time and see what happens with his behavior.
‫You can see he is moving to the last known player location.
‫And now he is waiting, having cleared the value.
‫And then you can see he's going to move to and you can see these red bars on the decorators are telling
‫us that neither of these two conditions are true.
‫So he can't do either of those.
‫Finally, he's moved to the start location.
‫So now it's flickering because basically it can't do anything.
‫So that basically is that this lecture we've seen how we can get the tasks to execute code understood
‫the different statuses that you can return from execute task how to get the blackboard from the task
‫and also how to get the currently selected key on a blackboard task.
‫In the next lecture, we're going to see how we can get hold of the world outside of the blackboard
‫behavior tree, how we can get hold of the pawn from within a behavior tree task.
‫I'll see you there.