WEBVTT

00:06.850 --> 00:07.900
Welcome back.

00:07.990 --> 00:11.610
So one of our next steps is to create an AI controller.

00:11.620 --> 00:20.500
And if we go into our character folder and open up enemy base, we can search for AI controller as the

00:20.500 --> 00:24.190
character class has an AI controller class.

00:24.190 --> 00:32.140
So if it has an AI controller set for its AI controller class, then it's going to use that for AI.

00:32.170 --> 00:39.430
But we're going to want our own custom AI controller class so we can add behavior tree and blackboard

00:39.430 --> 00:40.750
components to it.

00:41.360 --> 00:48.560
So for this reason, we're going to go into C plus plus classes or public, and I'd like a new folder

00:48.560 --> 00:51.620
specifically for AI related classes.

00:51.620 --> 00:57.140
So I'm going to make a new class here, choose all classes and search for AI controller.

00:57.140 --> 00:59.210
So we have the AI controller class.

00:59.210 --> 01:08.420
We're going to choose that and put this in a new folder called AI, and this is going to be Ora AI controller.

01:08.450 --> 01:13.580
Let's create the class I'm going to click on No and close the editor.

01:13.970 --> 01:19.910
I'm going to go ahead and close all tabs here and go into my new AI folder.

01:20.610 --> 01:22.090
And there it is.

01:22.110 --> 01:27.630
Here's my aura controller and I can open the CPP file as well.

01:27.840 --> 01:31.890
Now, aura controller is going to be relatively simple.

01:31.890 --> 01:33.990
It just needs a couple of components.

01:34.020 --> 01:37.520
A blackboard component and a behavior tree component.

01:37.530 --> 01:40.290
And we'll learn what these things are and how they work.

01:40.290 --> 01:43.500
But first we're going to add these to our controller.

01:43.500 --> 01:51.000
And before we do, we're about to use some classes that exist in an AI module in Unreal Engine.

01:51.000 --> 01:57.960
So we have to go to Aura Build X and we need to add something to our dependencies here.

01:57.960 --> 02:01.740
So I'm going to add two private dependency module names.

02:04.600 --> 02:07.720
I module, that's what it's called.

02:07.720 --> 02:09.160
It needs to go into quotes.

02:09.160 --> 02:16.780
So I module is going to contain those I related classes that we're going to want to use.

02:16.930 --> 02:23.950
Now that I have that, I'm going to go ahead and close out of the build file and we're going to add

02:23.950 --> 02:25.320
these components here.

02:25.330 --> 02:33.880
So I need a public section and I'm going to construct these components in the I controllers constructor.

02:33.880 --> 02:40.690
So we're going to have a constructor here and the blackboard and behavior tree components can be protected.

02:42.090 --> 02:44.650
So we'll have a couple of T object pointers.

02:44.670 --> 02:50.160
Now, the types for these two components I'm going to forward declare up at the top.

02:50.340 --> 02:58.140
So the first one is going to be you blackboard component and the next one will be you behavior tree

02:58.170 --> 02:59.130
component.

02:59.400 --> 03:01.770
These are the types of components we need.

03:01.860 --> 03:06.960
So first we'll make a T object pointer of type u blackboard component.

03:06.990 --> 03:09.900
This will be called blackboard component.

03:11.250 --> 03:13.050
We'll give this a property.

03:14.410 --> 03:20.770
And we can add a t object pointer of type you behavior tree component.

03:20.770 --> 03:24.100
And this will be behavior tree.

03:24.930 --> 03:28.950
Component and this also gets a new property.

03:30.040 --> 03:35.500
So now that we have these, we just need to construct them in the constructor, which means we should

03:35.500 --> 03:37.450
generate a definition for it.

03:37.570 --> 03:39.350
The constructor, that is.

03:39.370 --> 03:42.740
So we need to construct these components.

03:42.760 --> 03:46.930
We need to construct blackboard component and behavior tree component.

03:46.960 --> 03:49.330
So we'll start with Blackboard component.

03:49.360 --> 03:55.900
We're going to use Create Default Subobject with you Blackboard component.

03:56.800 --> 04:03.700
Thanks to writer for including my header file and we'll call this blackboard component.

04:05.310 --> 04:08.250
Next we can create the behavior tree component.

04:10.110 --> 04:18.030
So we'll use create default subobject with you behavior tree components and we'll call this behavior

04:18.030 --> 04:20.060
tree component.

04:20.070 --> 04:24.120
And again, thanks to writer I have my includes.

04:24.480 --> 04:25.710
Now we can check these.

04:25.710 --> 04:28.250
These should definitely be valid by this point.

04:28.260 --> 04:30.300
So we're going to make sure of that.

04:30.450 --> 04:36.660
We'll check Blackboard component and we'll check behavior tree component.

04:36.900 --> 04:38.040
And there we go.

04:38.220 --> 04:46.200
So now that we have our components on the AI controller class, our enemy needs to have a behavior tree

04:46.230 --> 04:49.920
that it can use by our behavior tree component.

04:49.950 --> 04:56.190
So we're going to go to Ora Enemy, and here in the protected section, we're going to add a behavior

04:56.190 --> 04:56.550
tree.

04:56.550 --> 04:59.190
But I'd like to forward declare this type.

04:59.850 --> 05:03.000
So I'm going to forward declare you behavior tree.

05:05.110 --> 05:10.230
I'd also like a pointer to our AI controller, which I'm also going to forward declare.

05:10.240 --> 05:13.370
So I'm going to forward declare ora AI controller.

05:13.390 --> 05:14.110
Be careful.

05:14.110 --> 05:15.640
There's an A at the beginning.

05:15.640 --> 05:17.200
It's an actor after all.

05:17.200 --> 05:19.720
So we have a ora AI controller.

05:19.750 --> 05:25.120
We're going to add a couple of pointers, one for the behavior tree and one for the AI controller.

05:25.240 --> 05:26.920
So first, the behavior tree.

05:26.920 --> 05:33.880
We're going to have a T object pointer of type U behavior tree and we'll call this behavior tree.

05:34.090 --> 05:38.200
And this gets a U property and we're going to be setting this from the blueprint.

05:38.200 --> 05:44.350
So it's going to be edit anywhere or edit defaults only and we'll give it a category of AI.

05:46.510 --> 05:50.200
Now we'll also have a pointer to our AI controller.

05:51.230 --> 05:55.100
And this will be of type A ora i controller.

05:56.410 --> 05:59.290
And we'll call this aura AI controller.

06:01.450 --> 06:03.280
And this simply gets a new property.

06:03.280 --> 06:05.200
We're not going to expose it to blueprint.

06:06.550 --> 06:08.110
At least for the time being.

06:08.140 --> 06:08.530
Okay.

06:08.530 --> 06:16.630
So we can set that from our blueprint or AI controller, however, needs to be set at some point and

06:16.630 --> 06:23.440
the soonest we know we have a valid controller that we can cast to this and set this pointer is in possessed

06:23.440 --> 06:26.500
by so we can override possessed by.

06:26.500 --> 06:28.270
We've seen this function before.

06:28.420 --> 06:32.860
We're going to have a virtual void possessed by that's an override.

06:32.860 --> 06:35.740
We're going to go ahead and generate a definition for this.

06:35.740 --> 06:45.520
We'll let this call super, but then we're going to set our AI controller equal to a cast to or AI controller

06:45.550 --> 06:48.130
A or AI controller to be exact.

06:48.130 --> 06:53.710
And we're going to cast the new controller that we have access to here in possessed by.

06:53.860 --> 06:56.920
So at this point we'll have our AI controller.

06:56.950 --> 07:03.600
Now notice at the top we now have or AI controller here, so that is included.

07:03.610 --> 07:08.060
So now that we have some variables, it's time to go back into blueprint.

07:08.090 --> 07:14.720
We need to make sure our enemy is using our custom AI controller as well as the behavior tree that we

07:14.720 --> 07:16.160
are going to create.

07:16.190 --> 07:19.820
So let's go ahead and compile and launch the editor.

07:21.310 --> 07:26.110
So with the editor open, we know that we have a new AI controller class.

07:26.200 --> 07:33.280
I'd like to go to Blueprints and make a new folder here for all AI related blueprints.

07:33.490 --> 07:38.590
And here in AI, I'm going to make my new AI controller blueprint.

07:38.620 --> 07:45.190
I'm going to create a new Blueprint class and search for Aura AI and get my aura AI controller select

07:45.190 --> 07:49.570
that and make a BP aura AI controller.

07:49.660 --> 07:52.000
So at least we have a blueprint here.

07:52.090 --> 07:53.830
Here's what it looks like.

07:53.860 --> 08:00.070
Nothing really that we need to set on it, but we know that it's an AI controller and this is our AI

08:00.100 --> 08:04.030
controller class that we want to use on our enemies.

08:04.030 --> 08:09.790
So we're going to go to blueprints and character and here's BP enemy base.

08:09.820 --> 08:17.110
We need to search for AI controller class and this is not our aura AI controller.

08:17.110 --> 08:19.120
We didn't expose that to blueprint.

08:19.150 --> 08:21.170
We're setting that in C plus plus.

08:21.170 --> 08:25.760
This is the character inherited AI controller class.

08:25.790 --> 08:28.370
It sets an AI controller by default.

08:28.400 --> 08:34.910
We'd like to use our BP aura AI controller, and that way it'll be controlled by that.

08:35.030 --> 08:42.560
Now at the same time, we also have under our category AI a behavior tree.

08:42.590 --> 08:44.810
Now there's nothing to set it to.

08:44.840 --> 08:48.380
We haven't created a behavior tree or a blackboard.

08:48.380 --> 08:54.020
And again, we'll get into exactly what these classes are and what they can do for us once it's time

08:54.020 --> 08:55.190
to get into that.

08:55.280 --> 09:00.950
But for now, we're just going to create these classes a behavior tree and a blackboard, and those

09:00.950 --> 09:02.300
can go in blueprints.

09:02.330 --> 09:11.120
AI So for a blackboard, we're going to right click go to Artificial Intelligence and select Blackboard.

09:11.120 --> 09:17.390
And this can be prefixed with B for Blackboard, and we're going to call this enemy Blackboard.

09:17.780 --> 09:20.660
We're also going to create an enemy behavior tree.

09:20.660 --> 09:24.560
So right click artificial intelligence and Behavior Tree.

09:24.560 --> 09:31.670
We're going to call this BT for Behavior tree and call it enemy behavior tree.

09:32.090 --> 09:34.370
Now we can double click these to open them up.

09:34.370 --> 09:35.990
Here's the behavior tree.

09:35.990 --> 09:37.550
It has a root node.

09:37.550 --> 09:43.460
We're going to learn about how to use this, but it also has a behavior tree and a blackboard tab.

09:43.460 --> 09:47.720
So here's the blackboard associated with this behavior tree.

09:47.810 --> 09:54.470
So now that we have a behavior tree asset called enemy Behavior tree, we can go back to enemy base.

09:54.470 --> 09:57.110
Here's our enemy behavior tree variable.

09:57.110 --> 10:01.040
We can now set it to BT Enemy behavior tree.

10:01.040 --> 10:04.460
And now that behavior tree variable has been set.

10:04.610 --> 10:11.630
So after doing all this, now that these classes actually exist, we have a behavior tree in a blackboard.

10:11.630 --> 10:13.760
We have to run the behavior tree.

10:13.760 --> 10:18.320
We have to set it in motion so that it's actively controlling our character.

10:18.320 --> 10:21.650
And to do that, we go back into C plus plus.

10:21.650 --> 10:25.280
So we're going to close and save everything that we've changed here.

10:25.520 --> 10:29.210
So here in C plus plus, we need to run the behavior tree.

10:29.240 --> 10:33.140
Now, a behavior tree is always associated with a given blackboard.

10:33.140 --> 10:37.670
As we saw, there was a blackboard tab in the behavior tree asset.

10:37.700 --> 10:40.130
We're going to make that association as well.

10:40.130 --> 10:45.350
We're going to do all this from the aura enemy here in possessed by because this is the perfect place

10:45.350 --> 10:45.920
to do it.

10:45.920 --> 10:48.620
Now we're going to check if we have authority.

10:48.620 --> 10:52.790
In fact, we're going to say if not has authority, we're going to return.

10:52.790 --> 10:53.270
Why?

10:53.300 --> 10:56.240
Because I only matters on the server.

10:56.480 --> 10:58.790
AI characters are controlled on the server.

10:58.790 --> 11:02.510
Anything that clients see is the result of replication.

11:02.510 --> 11:05.780
So we're only setting AI controller on the server.

11:05.780 --> 11:13.040
And if we check this pointer for validity and it's null, we know that we're not on the server and so

11:13.040 --> 11:13.510
on.

11:13.520 --> 11:16.070
Now we want to run the behavior tree.

11:16.070 --> 11:17.030
That's pretty easy.

11:17.030 --> 11:25.610
We take our aura AI controller and AI controllers and it would help if I spelled it without typos.

11:25.610 --> 11:26.390
There we go.

11:26.690 --> 11:33.320
And AI controllers have run behavior tree, which takes a pointer to a behavior tree.

11:33.470 --> 11:36.650
Now we have one, so we're going to pass that in.

11:37.370 --> 11:39.920
Now, that's not the only thing we should do.

11:39.950 --> 11:45.860
We should also initialize the blackboard on the blackboard component.

11:45.890 --> 11:52.610
So whatever blackboard is associated with our behavior tree asset, our AI controller needs to take

11:52.610 --> 11:57.770
its blackboard component and initialize it with that blackboard asset.

11:58.520 --> 12:05.990
So the AI controller class has a getter for its blackboard component so we can take our aura AI controller

12:06.110 --> 12:09.650
and we can call get blackboard component.

12:09.650 --> 12:13.100
And on this we can call initialize.

12:14.110 --> 12:14.960
Blackboard.

12:14.980 --> 12:17.680
Now, this requires a new blackboard.

12:17.680 --> 12:18.520
Data.

12:18.550 --> 12:20.470
Our behavior tree has one of these.

12:20.470 --> 12:25.660
If we take our behavior tree, we can access the blackboard asset.

12:25.690 --> 12:28.210
Now, this is a new blackboard data.

12:28.210 --> 12:31.660
It's a T object pointer as we see.

12:31.660 --> 12:36.810
So we're going to have to dereference this pointer and pass it in as a non pointer.

12:36.820 --> 12:40.900
Now the AI controller class has this git blackboard component, right?

12:40.900 --> 12:47.260
And if we go to its definition, we see that it simply returns Blackboard.

12:47.290 --> 12:53.710
This is a U blackboard component pointer that exists on the AI controller.

12:54.380 --> 12:57.590
So really this means that we could just set this.

12:57.620 --> 12:58.670
It's protected.

12:58.700 --> 13:03.200
We don't really need to create a blackboard component variable.

13:03.200 --> 13:07.970
When we create our blackboard component, we can really just set blackboard, right?

13:07.970 --> 13:16.030
So we could just go back to our AI controller and remove blackboard component altogether and in or AI

13:16.070 --> 13:24.620
controller cpp, we could really just set the blackboard to the result of create default Subobject here

13:24.800 --> 13:26.830
and check blackboard.

13:26.840 --> 13:32.620
So really no need to create blackboard component if Blackboard is already a variable.

13:32.630 --> 13:38.360
Now in Ora Enemy, we know that we're accessing the blackboard component that we've created and we're

13:38.360 --> 13:39.710
initializing it.

13:40.310 --> 13:47.810
So now after all this, we know that we'll be running our behavior tree as long as our enemies have

13:47.810 --> 13:52.670
their behavior tree variable set and we set that on the base enemy class.

13:52.670 --> 13:56.970
So let's just go ahead and compile and launch the editor.

13:57.820 --> 14:00.690
And I'm going to go ahead and open up my behavior tree.

14:00.700 --> 14:07.150
And the last thing we can do is just make sure that our enemies have their behavior trees running.

14:07.180 --> 14:11.210
So we'll get into what the nodes in the behavior tree can do.

14:11.230 --> 14:16.990
But for now, we're just going to do a quick test to make sure it's working by dragging off of the root

14:16.990 --> 14:18.800
and adding a selector.

14:18.820 --> 14:26.920
And from the selector, we're going to drag off, expand tasks and we're going to choose a play animation

14:26.920 --> 14:27.820
task.

14:27.850 --> 14:32.600
Now, with the play animation task selected, here's animation to play.

14:32.620 --> 14:35.200
We're going to choose to play an animation.

14:35.800 --> 14:37.890
I'm going to choose Attack Spear.

14:37.900 --> 14:44.000
So this is just a quick test to see if our behavior tree is running on our enemies.

14:44.020 --> 14:45.400
We're going to press play.

14:46.170 --> 14:51.840
We're going to run up to our enemies and they're playing the attack spear animation.

14:51.840 --> 14:54.690
So that proves that our behavior tree is running.

14:54.690 --> 15:01.860
And if we undock it and take a look at those nodes in the tree and press play, we'll see that suddenly

15:01.860 --> 15:03.210
it looks a little bit different, right?

15:03.210 --> 15:05.220
We're seeing this flow here.

15:05.400 --> 15:10.200
We are able to see where in the tree we are.

15:11.290 --> 15:12.690
So that's pretty nice.

15:12.700 --> 15:16.780
So we know that the tree is working, Everything is tied together.

15:17.620 --> 15:19.060
This is a great start.

15:19.090 --> 15:21.520
We're ready to start fleshing out our tree.

15:21.550 --> 15:23.320
We can go ahead and remove those nodes.

15:23.320 --> 15:28.200
We'll get into the details of what to do in this tree soon enough.

15:28.210 --> 15:36.460
But now that everything is working, we're going to save all and we're ready to start giving our enemies

15:36.460 --> 15:38.260
some AI behavior.

15:38.470 --> 15:41.320
Great job and I'll see you in the next video.
