Welcome back.
So our goal here is to get our enemies moving around and being active and being controlled by AI.
Now we know that there are currently associated with a behavior tree.
If we go to our blueprints folder, go to AI.
We have a behavior tree.
Now the behavior tree has a root node.
And from the root node we can attach something.
We can attach children to nodes in our behavior tree.
Now one of the fundamental nodes that you can have in the behavior tree is the selector node.
A selector node has the very specific quality that it will execute its children from left to right.
And by children I mean I can drag off of the bottom of my selector and put nodes.
We know what tasks do.
We've seen the play animation task node, and there are a number of other built in tasks such as wait
and other things such as move to.
So these are tasks which perform some kind of action.
Now when we have things connected to the bottom of a selector, we say that these nodes are children
of the selector and the selector will execute them left to right, one at a time.
Now it doesn't just do this all at the same time, but one after the other.
In other words, it's going to use this play animation node and it's going to stop.
It'll be held up here until the play animation node finishes, at which point it will then go to the
next node.
And once that node has finished, it will then go to the next node.
Now, nodes in a behavior tree have the concept of success or failure, and whether a node succeeds
or fails depends on the node and how it defines success or failure.
Now, if a node succeeds or fails, it returns that success or failure information back to its parent,
and the parent will then decide what to do with that information.
A selector specifically has the quality that it will execute its children, one after the other, until
one of its children succeeds, at which point it will then return success up to its parent.
Now, once success returns to the root, then the whole tree starts over.
So a selector with three children like this will execute its first child first.
And if that child succeeds, it'll never get to the other children.
And once it returns success to the root and starts over, if it succeeds again, then once again, we
will never try to execute the other two child nodes.
It will only ever keep executing the first node as long as it keeps succeeding.
As soon as it fails though, then the selector will try to execute the next of its child nodes and continue
the process from there.
So that's the behavior of a selector.
Now selectors can have things attached to them.
We call a selector a composite node, and we can attach certain types of nodes to composite nodes and
tasks.
One of these types of nodes that I'd like to attach to my selector is called a service.
Now I can right click on my selector and notice that we can add decorators and services.
Decorators can be attached to nodes, and services can be attached to nodes.
Notice that there are a number of different types of decorators.
And we'll get to decorators in more detail later.
But I'd like to add a service and a service once attached to a node, such as a selector will execute
at its defined frequency as long as the branch is being executed.
Now this selector is considered a branch, and even if it has multiple children connected to it, we
consider the selector and all of its children to be a branch.
If this branch is being executed, then any services attached to it will be executed and services execute
at a frequency that we specify.
They have their own tick functionality, but generally in behavior trees we don't often have things
tick every single frame of the game.
Like your typical tick function.
We tick every so often, every half second, every quarter second.
We specify how often our services can tick.
Now, if I wanted to attach a service on here, I could or I can create my own.
And that's what I'd like to do, is create my own service that will do exactly what I tell it to do
every time it ticks.
And for that, we need a new class.
Hey there, it's Steven from the future here, and I'm coming back to let you know about a mistake that
I made in the course.
And how.
How we're going to rectify it.
You see, in the original version of this video, we created a new class for our behavior tree service,
and we created that class in C plus.
Plus.
The class we created was based on UT Service Blueprint Base, which is designed to be subclassed in
blueprint only.
We can see the comment here.
Base class for blueprint based service nodes.
Do not use it for creating native C plus plus classes.
This is the no no that I actually did in the course, and it's kind of silly.
The class is called Blueprint Base.
I don't know why I didn't snap there, but I created the class in C plus plus and this class is made
to be Subclassed in blueprint.
That's the appropriate way to subclass this class.
That's how we're actually going to do it.
But the original version of the course created it in C plus.
Plus it still worked, but it just wasn't using the class the way it was designed to be used, which
is as a blueprint rather than a native C plus plus class.
So if you go and look at the GitHub commit for this lecture, you're going to see this behavior tree
service C plus plus class based on UT Service Blueprint base.
You can safely ignore that because we're going to be creating it in blueprint as is intended.
So completely ignore the GitHub commit for this lecture as we're not going to create this class in C
plus plus.
Instead we're going to create it in blueprint.
So we can go into our I folder and create our new behavior tree task directly.
Here we can right click.
We can create a new blueprint class.
And we're going to open all classes and search for BT Service Blueprint Base.
This is what we're going to create a blueprint based on.
So let's make this blueprint.
And we're going to prefix its name with Pts for behavior tree service.
And I'd like to call this Find nearest player because I want this service to do just that.
Find the nearest player to the I pawn.
So let's open pts, find nearest player and take a look at our behavior tree service.
Now a behavior tree service as we mentioned, is designed to perform some kind of action at a regular
interval.
In fact our service has an interval parameter.
By default it's set to 0.5 seconds, which means every half second this service will tick.
We can even add a random deviation to the service.
Now our details panel has a couple other things that we'll briefly mention.
One is the node name which we can customize.
I'm just going to fill this in with find nearest player and a custom description which is optional.
We could say finds nearest player if we like.
And we can attach our custom service in our behavior tree to our selector.
We can right click Add Service and there's our Pts.
Find nearest player and notice that its name is just find nearest player because that's what I set it
to for the node name.
So now we have this attached to our selector.
And it's going to tick according to its interval which by the way we can select find nearest player
and change its interval.
It's exposed to the details panel.
I'm going to leave it at 0.5.
And the way we give this ticking functionality is by overriding one of its overridable functions.
We're interested in receive tick I if we choose that we have access to the owner controller, the controlled
pawn, even delta seconds, and we can do something at this services interval.
All I'd like to do for now is do a print string.
We can print hello.
Or we could even get the owner controller call get object name on it if we'd like to print the name
of the owner controller.
Let's see what happens.
If we compile this and go into our game and press play, we can see that tick happening.
And all three of the I pawns are controlled by the controllers that we have printed to the screen there,
and we also have access to the controlled pawn.
We could print the object name for the controlled pawn as well, and just see that those are the corresponding
pawns that control these behavior trees.
So as you can see, we have created our behavior tree service.
We see that these services are ticking for each of our I pawns.
It's quite easy to attach a service to.
One of our selector nodes as we've done, but all we're doing is printing a message to the screen.
What we'd like to do is find the nearest player.
In other words, where's aura, how close is aura, etc. so for that, we'll start setting things up
in the next video.
Great job and I'll see you soon.