Welcome back.
So one of our next steps is to create an AI controller.
And if we go into our character folder and open up enemy base, we can search for AI controller as the
character class has an AI controller class.
So if it has an AI controller set for its AI controller class, then it's going to use that for AI.
But we're going to want our own custom AI controller class so we can add behavior tree and blackboard
components to it.
So for this reason, we're going to go into C plus plus classes or public, and I'd like a new folder
specifically for AI related classes.
So I'm going to make a new class here, choose all classes and search for AI controller.
So we have the AI controller class.
We're going to choose that and put this in a new folder called AI, and this is going to be Ora AI controller.
Let's create the class I'm going to click on No and close the editor.
I'm going to go ahead and close all tabs here and go into my new AI folder.
And there it is.
Here's my aura controller and I can open the CPP file as well.
Now, aura controller is going to be relatively simple.
It just needs a couple of components.
A blackboard component and a behavior tree component.
And we'll learn what these things are and how they work.
But first we're going to add these to our controller.
And before we do, we're about to use some classes that exist in an AI module in Unreal Engine.
So we have to go to Aura Build X and we need to add something to our dependencies here.
So I'm going to add two private dependency module names.
I module, that's what it's called.
It needs to go into quotes.
So I module is going to contain those I related classes that we're going to want to use.
Now that I have that, I'm going to go ahead and close out of the build file and we're going to add
these components here.
So I need a public section and I'm going to construct these components in the I controllers constructor.
So we're going to have a constructor here and the blackboard and behavior tree components can be protected.
So we'll have a couple of T object pointers.
Now, the types for these two components I'm going to forward declare up at the top.
So the first one is going to be you blackboard component and the next one will be you behavior tree
component.
These are the types of components we need.
So first we'll make a T object pointer of type u blackboard component.
This will be called blackboard component.
We'll give this a property.
And we can add a t object pointer of type you behavior tree component.
And this will be behavior tree.
Component and this also gets a new property.
So now that we have these, we just need to construct them in the constructor, which means we should
generate a definition for it.
The constructor, that is.
So we need to construct these components.
We need to construct blackboard component and behavior tree component.
So we'll start with Blackboard component.
We're going to use Create Default Subobject with you Blackboard component.
Thanks to writer for including my header file and we'll call this blackboard component.
Next we can create the behavior tree component.
So we'll use create default subobject with you behavior tree components and we'll call this behavior
tree component.
And again, thanks to writer I have my includes.
Now we can check these.
These should definitely be valid by this point.
So we're going to make sure of that.
We'll check Blackboard component and we'll check behavior tree component.
And there we go.
So now that we have our components on the AI controller class, our enemy needs to have a behavior tree
that it can use by our behavior tree component.
So we're going to go to Ora Enemy, and here in the protected section, we're going to add a behavior
tree.
But I'd like to forward declare this type.
So I'm going to forward declare you behavior tree.
I'd also like a pointer to our AI controller, which I'm also going to forward declare.
So I'm going to forward declare ora AI controller.
Be careful.
There's an A at the beginning.
It's an actor after all.
So we have a ora AI controller.
We're going to add a couple of pointers, one for the behavior tree and one for the AI controller.
So first, the behavior tree.
We're going to have a T object pointer of type U behavior tree and we'll call this behavior tree.
And this gets a U property and we're going to be setting this from the blueprint.
So it's going to be edit anywhere or edit defaults only and we'll give it a category of AI.
Now we'll also have a pointer to our AI controller.
And this will be of type A ora i controller.
And we'll call this aura AI controller.
And this simply gets a new property.
We're not going to expose it to blueprint.
At least for the time being.
Okay.
So we can set that from our blueprint or AI controller, however, needs to be set at some point and
the soonest we know we have a valid controller that we can cast to this and set this pointer is in possessed
by so we can override possessed by.
We've seen this function before.
We're going to have a virtual void possessed by that's an override.
We're going to go ahead and generate a definition for this.
We'll let this call super, but then we're going to set our AI controller equal to a cast to or AI controller
A or AI controller to be exact.
And we're going to cast the new controller that we have access to here in possessed by.
So at this point we'll have our AI controller.
Now notice at the top we now have or AI controller here, so that is included.
So now that we have some variables, it's time to go back into blueprint.
We need to make sure our enemy is using our custom AI controller as well as the behavior tree that we
are going to create.
So let's go ahead and compile and launch the editor.
So with the editor open, we know that we have a new AI controller class.
I'd like to go to Blueprints and make a new folder here for all AI related blueprints.
And here in AI, I'm going to make my new AI controller blueprint.
I'm going to create a new Blueprint class and search for Aura AI and get my aura AI controller select
that and make a BP aura AI controller.
So at least we have a blueprint here.
Here's what it looks like.
Nothing really that we need to set on it, but we know that it's an AI controller and this is our AI
controller class that we want to use on our enemies.
So we're going to go to blueprints and character and here's BP enemy base.
We need to search for AI controller class and this is not our aura AI controller.
We didn't expose that to blueprint.
We're setting that in C plus plus.
This is the character inherited AI controller class.
It sets an AI controller by default.
We'd like to use our BP aura AI controller, and that way it'll be controlled by that.
Now at the same time, we also have under our category AI a behavior tree.
Now there's nothing to set it to.
We haven't created a behavior tree or a blackboard.
And again, we'll get into exactly what these classes are and what they can do for us once it's time
to get into that.
But for now, we're just going to create these classes a behavior tree and a blackboard, and those
can go in blueprints.
AI So for a blackboard, we're going to right click go to Artificial Intelligence and select Blackboard.
And this can be prefixed with B for Blackboard, and we're going to call this enemy Blackboard.
We're also going to create an enemy behavior tree.
So right click artificial intelligence and Behavior Tree.
We're going to call this BT for Behavior tree and call it enemy behavior tree.
Now we can double click these to open them up.
Here's the behavior tree.
It has a root node.
We're going to learn about how to use this, but it also has a behavior tree and a blackboard tab.
So here's the blackboard associated with this behavior tree.
So now that we have a behavior tree asset called enemy Behavior tree, we can go back to enemy base.
Here's our enemy behavior tree variable.
We can now set it to BT Enemy behavior tree.
And now that behavior tree variable has been set.
So after doing all this, now that these classes actually exist, we have a behavior tree in a blackboard.
We have to run the behavior tree.
We have to set it in motion so that it's actively controlling our character.
And to do that, we go back into C plus plus.
So we're going to close and save everything that we've changed here.
So here in C plus plus, we need to run the behavior tree.
Now, a behavior tree is always associated with a given blackboard.
As we saw, there was a blackboard tab in the behavior tree asset.
We're going to make that association as well.
We're going to do all this from the aura enemy here in possessed by because this is the perfect place
to do it.
Now we're going to check if we have authority.
In fact, we're going to say if not has authority, we're going to return.
Why?
Because I only matters on the server.
AI characters are controlled on the server.
Anything that clients see is the result of replication.
So we're only setting AI controller on the server.
And if we check this pointer for validity and it's null, we know that we're not on the server and so
on.
Now we want to run the behavior tree.
That's pretty easy.
We take our aura AI controller and AI controllers and it would help if I spelled it without typos.
There we go.
And AI controllers have run behavior tree, which takes a pointer to a behavior tree.
Now we have one, so we're going to pass that in.
Now, that's not the only thing we should do.
We should also initialize the blackboard on the blackboard component.
So whatever blackboard is associated with our behavior tree asset, our AI controller needs to take
its blackboard component and initialize it with that blackboard asset.
So the AI controller class has a getter for its blackboard component so we can take our aura AI controller
and we can call get blackboard component.
And on this we can call initialize.
Blackboard.
Now, this requires a new blackboard.
Data.
Our behavior tree has one of these.
If we take our behavior tree, we can access the blackboard asset.
Now, this is a new blackboard data.
It's a T object pointer as we see.
So we're going to have to dereference this pointer and pass it in as a non pointer.
Now the AI controller class has this git blackboard component, right?
And if we go to its definition, we see that it simply returns Blackboard.
This is a U blackboard component pointer that exists on the AI controller.
So really this means that we could just set this.
It's protected.
We don't really need to create a blackboard component variable.
When we create our blackboard component, we can really just set blackboard, right?
So we could just go back to our AI controller and remove blackboard component altogether and in or AI
controller cpp, we could really just set the blackboard to the result of create default Subobject here
and check blackboard.
So really no need to create blackboard component if Blackboard is already a variable.
Now in Ora Enemy, we know that we're accessing the blackboard component that we've created and we're
initializing it.
So now after all this, we know that we'll be running our behavior tree as long as our enemies have
their behavior tree variable set and we set that on the base enemy class.
So let's just go ahead and compile and launch the editor.
And I'm going to go ahead and open up my behavior tree.
And the last thing we can do is just make sure that our enemies have their behavior trees running.
So we'll get into what the nodes in the behavior tree can do.
But for now, we're just going to do a quick test to make sure it's working by dragging off of the root
and adding a selector.
And from the selector, we're going to drag off, expand tasks and we're going to choose a play animation
task.
Now, with the play animation task selected, here's animation to play.
We're going to choose to play an animation.
I'm going to choose Attack Spear.
So this is just a quick test to see if our behavior tree is running on our enemies.
We're going to press play.
We're going to run up to our enemies and they're playing the attack spear animation.
So that proves that our behavior tree is running.
And if we undock it and take a look at those nodes in the tree and press play, we'll see that suddenly
it looks a little bit different, right?
We're seeing this flow here.
We are able to see where in the tree we are.
So that's pretty nice.
So we know that the tree is working, Everything is tied together.
This is a great start.
We're ready to start fleshing out our tree.
We can go ahead and remove those nodes.
We'll get into the details of what to do in this tree soon enough.
But now that everything is working, we're going to save all and we're ready to start giving our enemies
some AI behavior.
Great job and I'll see you in the next video.