‫Hello and welcome.
‫In this lecture, we're introducing two new types of asset the behavior tree and the blackboard, which
‫are two assets that allow us to do very interesting things with A.I..
‫Allow us to make much more complicated.
‫I very easily let's dive in and see how we use them and set them up.
‫Okay.
‫So we want to create some sophisticated AI behavior.
‫Now, the way we typically do this in Unreal is using a system called behavior trees.
‫Now, what are these?
‫Well, the best way for us to find out is to create one.
‫So let's go ahead and right click and go.
‫We're looking here under the artificial intelligence.
‫And there's two options here behavior, tree and blackboard.
‫We are going to want both of these.
‫So let's go and start with the behavior tree.
‫The prefix for this is BHT and we're going to call this the enemy.
‫So behavior tree enemy.
‫I just finish off with that.
‫There you go.
‫And we also need to create the other thing in there, which was a blackboard.
‫Now, what on earth is a blackboard?
‫We'll get to that in just a second.
‫But we're just going to call it the b b underscore enemy.
‫I said before, blackboard beat for behavior tree.
‫And while we're at it, we've got a few things relating to I here.
‫I'm going to create a new folder called A.I. and we are going to move the blackboard, the behavior
‫tree.
‫And somewhere here we have got our shooter, AI controller.
‫All of that goes into I move here and then we can go into our AI folder to deal with everything AI related.
‫Let's open up our behavior tree first of all and dock it to the main window.
‫So what on earth is this?
‫So this allows us to create trees of behaviors where you might say we've got part of the behavior here
‫is for tracing.
‫Part of it is for trolling.
‫Part of it is for something else, opening doors, all these kinds of things.
‫They typically structure behaviors as lots and lots of trees of behaviors and sub trees and so on.
‫We will see that all going on in detail, but suffice to say it is a great way of putting together natural
‫behaviors in your games.
‫Now it's not the be all and end all.
‫There's lots of AI techniques.
‫This might not be the one that you want to go with for your game, but it's a great start and you can
‫achieve quite a lot with it.
‫Now the next thing we can see over in the top, right, these two assets are linked.
‫The behavior tree and the blackboard.
‫What on earth is a blackboard?
‫Well, the blackboard is kind of like the memory of your A.I. It's essentially where you store a bunch
‫of variables.
‫You input them from the gameplay very much like we were doing with the animation system where you had
‫these these different properties that are being set by the gameplay.
‫We do the same thing with the AI.
‫We set lots of properties that give it information about its game world and then the behavior tree draws
‫on those properties and uses them to decide what it's going to do.
‫It can set those properties, it can use them as a memory, it can read from those properties and all
‫that lovely stuff.
‫So with that in mind, how can we set up our behavior tree in blackboard so that they are linked to
‫one another?
‫Well, we're going to expand out in our details pane.
‫You can see here that there is this blackboard asset and automatically it seems to have filled it out
‫for us such that the blackboard asset is linked to this b b b enemy I blackboard asset.
‫Now we are going to be departing from C++ for a little bit to use these assets because obviously behavior
‫trees and blackboards are entirely within the editor.
‫They're not blueprints as such.
‫They are their own editor tools and we can interface with them in C++.
‫But it does mean we're going to be using the editor for the next couple of lectures as well as C++.
‫Not so much blueprint though.
‫So it's great that we've got ourselves this behavior tree and this blackboard, but how on earth do
‫we actually use it?
‫Well, for that, we go back to our AI controller.
‫I if you remember, I controller is entirely responsible for running our behaviors and that is no exception
‫here.
‫The behavior tree is just another thing that the AI controller can use or run.
‫So the way we're going to do this is we're going to go over to our AI control and we need to have a
‫way of referencing in the blueprint what behavior tree we want it to run.
‫So the first thing I'm going to do is I'm actually going to go over to the control CP and comment out
‫the stuff in TTIC because we're going to be replacing this functionality with behavior tree functionality
‫instead.
‫And then that means that I can remove this acceptance radius here because we no longer need it.
‫And instead we're going to be using a pointer to the new behavior.
‫And I have to spell this The American Way Tree and pointer, that's the behavior tree we want there.
‫And I need to forward declare this with a class in front of it.
‫And then basically here, what I want to say is that this is the AI behavior and.
‫I'm going to keep the American spelling here just for consistency.
‫So this is the AI behavior that we want here now to actually run the behavior.
‫It couldn't be simpler.
‫Again, the eye control has a built in function for this.
‫We've got this run behavior.
‫Tree function turns a boolean.
‫Whether you are able to successfully execute it or not, start executing it, that is.
‫And it takes in the beat asset which is appointed to the new behavior tree.
‫So that's what we're going to want to use and we can do this over in begin play.
‫So what we're going to do is, first of all, we're going to check that we indeed have a behavior tree
‫here.
‫So we're going to say I behavior.
‫Is not equal to null pointer because we do not want to be using it otherwise.
‫And then what we can do is we can run the behavior tree called our AI behavior.
‫It really is that simple to get an AI behavior running and we can go ahead and compile.
‫And one more thing we can do with behavior trees is we can kind of see them running at runtime, but
‫to do that, you need to have some sort of node in here.
‫So we're just going to add in a node without fully understanding what it does.
‫I'm going to add in a sequence node and I'm going to hook it up by dragging down from the root node,
‫the bit underneath the root node that highlights when I mouse over it down to the bit above the sequence
‫node.
‫And I'm going to save that.
‫I'm going to go over and hit play and go into my behavior tree asset and you should be able to see a
‫dropdown here, but we're not because it's not currently running.
‫So let's stop playing.
‫And why is it not running?
‫Because we haven't hooked it up in our shooter controller.
‫So let's do that mini challenge for you to hook that up.
‫Hopefully it shouldn't be too hard.
‫Okay, welcome back.
‫So if we go and have a look in here, we're looking for our AI behavior.
‫For some reason, it required a recompile for me to show up for me.
‫But here it is.
‫AI behavior is what we want.
‫We click the drop down.
‫There's only one option at the moment because we've only got one behavior tree.
‫So let's select that one and compile and save.
‫And if we now go ahead and play the game.
‫And go and have a look at our beat enemy.
‫You can see that it's highlighting the node that's currently running and you've got this little drop
‫down next to the pause stop buttons, which tells you which I controller you're currently observing
‫the behavior tree of.
‫So this is a great functionality for you to be able to jump in and go, okay, why is that?
‫I just standing still.
‫Why is he not chasing the player?
‫You can see which part of the behavior he's currently running and go, Ah, that c you know, that thing's
‫gone wrong.
‫It's really helpful for debugging.
‫You can go and have a look at what all your AI in the world are up to by using this dropdown.
‫Great stuff.
‫That's all for this lecture.
‫In the next lecture, we're going to be looking at how we can set information in the blackboard memory
‫of our AI from C++.
‫I'll see you there.