Welcome back.
Now our find nearest player algorithm is ready to be created by us.
We have all actors with the target tag for our goblins.
That's going to be the player tag.
And there may be more than one aura in the game.
Or if the owning AI being driven by this behavior tree is perhaps a pet following aura around, well,
then it's going to have the player tag and its array of actors will contain the enemy tag.
Either way, this array could have multiple actors, and we want to find the closest of the actors.
So we're going to implement a pretty standard distance checking algorithm to find the closest actor.
This could be a problem that you're given in a programming interview, for instance.
So let's see how we're going to implement this.
For one, why don't we just go ahead and collapse this to a function right away?
So collapse the function.
We'll call this find nearest player.
And here in find nearest player we can simply loop over the actors with tag.
And I'm going to add a couple of local variables here.
One for the closest actor that's going to be a variable of type actor.
So we'll call this closest actor.
We'll set its type to actor object Reference.
And in the beginning here we're going to set closest actor to an empty reference in C plus.
Plus this would be setting it to null.
So first we clear out closest actor.
And we're also going to have another variable for the closest distance.
So this one will be called closest distance.
And its type can be float.
And we're going to set its value to a very large value from the start.
So we can just enter a whole bunch of nines in here if we want to.
Just a large number.
And of course float doesn't accommodate for that many digits.
So we'll just go ahead and put a large, large number here.
Now the reason we want a large number is because we're going to loop through the array and compare each
element's distance to the owning pawn to this closest distance.
And the first time that distance is going to be less than the value in closest distance.
And then each subsequent actor we check may or may not be closer, but by the end we'll have the closest
one.
So here's how we're going to do it.
First, I'd like to just pass in that controlled pawn.
The one that we have access to here in event receive tick.
I so find nearest player can get an input.
And we'll call this owning pawn.
And we'll set its type to pawn object reference.
And we'll pass that controlled pawn straight in.
In fact I want it to be called controlled pawn.
So I'll change that.
So now find nearest player has controlled pawn which we can access here.
And I'd like to do so here in the for each loop.
So the first thing I'll do is take the array element.
We'll check is valid.
Make sure we're not trying to access something that is not valid.
And we'll also take our owning pawn here the controlled pawn and see if that's valid as well.
So I'm going to search for controlled pawn.
And if you didn't know controlled pawn is an input parameter here to this function.
Therefore I can get a get node by searching for it.
We don't have to create a local variable for it.
I'm going to check if this one is valid as well, and if they're both valid, well then I can calculate
the distance between them.
So I'm going to first take the controlled pawn.
I'm going to call get Distance2.
And pass in the array element.
This will give us the distance between the controlled pawn and the array element, which is an actor
and the array.
Now I'm going to check to see if this distance is less than closest distance, and the first time it
will be.
So we're going to use a less than operator checking against closest distance.
So we'll have a branch like so.
And if this is true then we found an actor that's closer than our closest distance.
And in this case we're going to set closest distance to this distance we've calculated.
So the get distance two we can hook that up here.
It looks a little ugly.
So I'm going to promote it to a local variable.
We'll call this calculated distance.
And we'll set that here.
And we'll use that for closest distance.
And to avoid this I'm going to pass calculated distance the set node in here.
So it looks a little bit more clear what we're doing.
Now, in addition to setting the closest distance, I also want to set the closest actor.
So we're going to get a set node for that.
And we're going to pass in the array element, which I would also like a local variable for.
So I'm going to move these forward just a bit and promote the array element to a local variable called
actor.
This will be what we check in.
The is valid.
It will also be what we get the distance to here already.
That's looking better and it will be what we set.
Closest actor to here.
So by the end of this loop, by the time we hit our completed, we will have the closest actor and the
closest distance.
And we can then set our blackboard key selectors.
If we right click and search for set blackboard value as we can set a blackboard value as object.
If we select that this function expects a key.
And we have a key for target to follow, we can pass that in.
We'll hook this up to completed.
And we have to set this to an object that's going to be the closest actor that we've found.
We'll also use set blackboard value but this time as float because we have another one for the distance
to target.
So we'll pass that in.
Just making room here.
We'll pass in distance to target.
And we'll pass in the closest distance which we've calculated not calculated distance, just closest
distance.
So by the end of the loop these blackboard keys will be set.
Now we need to make the link between these two selectors and our blackboard keys.
To do that we open the I icon for both of them.
Compile and go back to our blueprint and select Find Nearest Player.
And now we can make the link here because our selectors are now instance editable.
They're exposed to the details panel.
And we can link them up by setting them here to our target to follow and our distance to target blackboard
keys.
So we'll do that here with the dropdown target.
To follow selector we'll get target to follow distance to target selector will get distance to target.
And now when we set them in our service they will be set on our blackboard.
So now that we have our target to follow and distance to target blackboard keys being set at this interval,
we can use them in our behavior tree.
For instance, we can drag off of our selector and we can get a task.
Let's try getting a move to and our move two if we select it has a blackboard key for its target.
Let's say we set that to target to follow.
Well what do you expect to happen if we play test.
Let's check it out.
We can press play.
And look they're following aura.
That's because every second, well, every half second, rather they're finding the nearest player and
move to is being executed.
So this is really powerful that we can have a service run at a specific interval.
It's not run every frame of the game.
It's run every 0.4 seconds with a random deviation, which is much more performant.
And this is why behavior tree services are so useful.
Excellent job and I'll see you in the next video.