WEBVTT

00:06.880 --> 00:08.020
Welcome back.

00:08.020 --> 00:14.140
Now our find nearest player algorithm is ready to be created by us.

00:14.140 --> 00:18.550
We have all actors with the target tag for our goblins.

00:18.550 --> 00:20.080
That's going to be the player tag.

00:20.080 --> 00:23.110
And there may be more than one aura in the game.

00:23.110 --> 00:33.220
Or if the owning AI being driven by this behavior tree is perhaps a pet following aura around, well,

00:33.220 --> 00:38.980
then it's going to have the player tag and its array of actors will contain the enemy tag.

00:39.010 --> 00:45.970
Either way, this array could have multiple actors, and we want to find the closest of the actors.

00:45.970 --> 00:53.140
So we're going to implement a pretty standard distance checking algorithm to find the closest actor.

00:53.500 --> 00:58.090
This could be a problem that you're given in a programming interview, for instance.

00:58.090 --> 01:00.880
So let's see how we're going to implement this.

01:00.880 --> 01:06.340
For one, why don't we just go ahead and collapse this to a function right away?

01:06.340 --> 01:08.170
So collapse the function.

01:08.170 --> 01:11.890
We'll call this find nearest player.

01:11.890 --> 01:20.290
And here in find nearest player we can simply loop over the actors with tag.

01:20.410 --> 01:24.730
And I'm going to add a couple of local variables here.

01:24.730 --> 01:30.370
One for the closest actor that's going to be a variable of type actor.

01:30.370 --> 01:33.190
So we'll call this closest actor.

01:33.190 --> 01:36.010
We'll set its type to actor object Reference.

01:36.010 --> 01:42.880
And in the beginning here we're going to set closest actor to an empty reference in C plus.

01:42.880 --> 01:45.010
Plus this would be setting it to null.

01:45.010 --> 01:47.830
So first we clear out closest actor.

01:47.830 --> 01:52.900
And we're also going to have another variable for the closest distance.

01:52.900 --> 01:56.530
So this one will be called closest distance.

01:56.530 --> 01:58.660
And its type can be float.

01:58.660 --> 02:04.630
And we're going to set its value to a very large value from the start.

02:04.780 --> 02:09.760
So we can just enter a whole bunch of nines in here if we want to.

02:09.760 --> 02:11.740
Just a large number.

02:11.740 --> 02:16.630
And of course float doesn't accommodate for that many digits.

02:16.630 --> 02:21.580
So we'll just go ahead and put a large, large number here.

02:21.580 --> 02:27.280
Now the reason we want a large number is because we're going to loop through the array and compare each

02:27.280 --> 02:33.160
element's distance to the owning pawn to this closest distance.

02:33.160 --> 02:39.670
And the first time that distance is going to be less than the value in closest distance.

02:39.670 --> 02:47.080
And then each subsequent actor we check may or may not be closer, but by the end we'll have the closest

02:47.080 --> 02:47.500
one.

02:47.500 --> 02:49.030
So here's how we're going to do it.

02:49.030 --> 02:53.410
First, I'd like to just pass in that controlled pawn.

02:53.440 --> 02:57.820
The one that we have access to here in event receive tick.

02:57.820 --> 03:00.970
I so find nearest player can get an input.

03:00.970 --> 03:03.190
And we'll call this owning pawn.

03:03.340 --> 03:07.780
And we'll set its type to pawn object reference.

03:07.780 --> 03:10.630
And we'll pass that controlled pawn straight in.

03:10.630 --> 03:13.090
In fact I want it to be called controlled pawn.

03:13.090 --> 03:14.350
So I'll change that.

03:16.690 --> 03:21.130
So now find nearest player has controlled pawn which we can access here.

03:21.130 --> 03:24.730
And I'd like to do so here in the for each loop.

03:24.730 --> 03:28.360
So the first thing I'll do is take the array element.

03:28.360 --> 03:30.220
We'll check is valid.

03:30.880 --> 03:35.500
Make sure we're not trying to access something that is not valid.

03:35.500 --> 03:42.580
And we'll also take our owning pawn here the controlled pawn and see if that's valid as well.

03:42.580 --> 03:44.650
So I'm going to search for controlled pawn.

03:44.650 --> 03:49.360
And if you didn't know controlled pawn is an input parameter here to this function.

03:49.360 --> 03:53.170
Therefore I can get a get node by searching for it.

03:53.170 --> 03:56.200
We don't have to create a local variable for it.

03:56.200 --> 04:02.500
I'm going to check if this one is valid as well, and if they're both valid, well then I can calculate

04:02.500 --> 04:04.180
the distance between them.

04:05.160 --> 04:07.980
So I'm going to first take the controlled pawn.

04:09.660 --> 04:12.000
I'm going to call get Distance2.

04:12.550 --> 04:15.010
And pass in the array element.

04:15.040 --> 04:21.880
This will give us the distance between the controlled pawn and the array element, which is an actor

04:21.880 --> 04:22.900
and the array.

04:22.930 --> 04:29.350
Now I'm going to check to see if this distance is less than closest distance, and the first time it

04:29.350 --> 04:30.070
will be.

04:30.070 --> 04:35.950
So we're going to use a less than operator checking against closest distance.

04:35.950 --> 04:38.560
So we'll have a branch like so.

04:38.560 --> 04:46.150
And if this is true then we found an actor that's closer than our closest distance.

04:46.150 --> 04:51.850
And in this case we're going to set closest distance to this distance we've calculated.

04:51.850 --> 04:55.510
So the get distance two we can hook that up here.

04:56.080 --> 04:57.190
It looks a little ugly.

04:57.190 --> 04:59.710
So I'm going to promote it to a local variable.

05:00.190 --> 05:04.120
We'll call this calculated distance.

05:04.120 --> 05:05.920
And we'll set that here.

05:05.920 --> 05:08.950
And we'll use that for closest distance.

05:08.950 --> 05:15.760
And to avoid this I'm going to pass calculated distance the set node in here.

05:15.760 --> 05:18.760
So it looks a little bit more clear what we're doing.

05:20.080 --> 05:24.880
Now, in addition to setting the closest distance, I also want to set the closest actor.

05:24.880 --> 05:26.890
So we're going to get a set node for that.

05:26.890 --> 05:35.230
And we're going to pass in the array element, which I would also like a local variable for.

05:35.470 --> 05:43.570
So I'm going to move these forward just a bit and promote the array element to a local variable called

05:43.570 --> 05:44.620
actor.

05:48.740 --> 05:50.480
This will be what we check in.

05:50.480 --> 05:51.770
The is valid.

05:52.100 --> 05:57.380
It will also be what we get the distance to here already.

05:57.380 --> 06:01.490
That's looking better and it will be what we set.

06:01.490 --> 06:04.070
Closest actor to here.

06:04.730 --> 06:11.990
So by the end of this loop, by the time we hit our completed, we will have the closest actor and the

06:11.990 --> 06:13.190
closest distance.

06:13.190 --> 06:17.210
And we can then set our blackboard key selectors.

06:17.210 --> 06:27.020
If we right click and search for set blackboard value as we can set a blackboard value as object.

06:27.020 --> 06:30.950
If we select that this function expects a key.

06:30.950 --> 06:35.120
And we have a key for target to follow, we can pass that in.

06:35.120 --> 06:37.160
We'll hook this up to completed.

06:37.430 --> 06:44.420
And we have to set this to an object that's going to be the closest actor that we've found.

06:44.420 --> 06:54.470
We'll also use set blackboard value but this time as float because we have another one for the distance

06:54.470 --> 06:55.430
to target.

06:55.880 --> 06:57.980
So we'll pass that in.

06:59.070 --> 07:00.960
Just making room here.

07:01.540 --> 07:04.030
We'll pass in distance to target.

07:05.580 --> 07:13.320
And we'll pass in the closest distance which we've calculated not calculated distance, just closest

07:13.320 --> 07:13.950
distance.

07:13.950 --> 07:18.210
So by the end of the loop these blackboard keys will be set.

07:18.210 --> 07:24.240
Now we need to make the link between these two selectors and our blackboard keys.

07:24.240 --> 07:28.140
To do that we open the I icon for both of them.

07:28.140 --> 07:32.850
Compile and go back to our blueprint and select Find Nearest Player.

07:32.850 --> 07:38.040
And now we can make the link here because our selectors are now instance editable.

07:38.040 --> 07:40.050
They're exposed to the details panel.

07:40.050 --> 07:46.710
And we can link them up by setting them here to our target to follow and our distance to target blackboard

07:46.710 --> 07:47.490
keys.

07:47.490 --> 07:50.040
So we'll do that here with the dropdown target.

07:50.040 --> 07:56.730
To follow selector we'll get target to follow distance to target selector will get distance to target.

07:56.730 --> 08:02.460
And now when we set them in our service they will be set on our blackboard.

08:02.460 --> 08:10.200
So now that we have our target to follow and distance to target blackboard keys being set at this interval,

08:10.200 --> 08:12.900
we can use them in our behavior tree.

08:12.900 --> 08:17.460
For instance, we can drag off of our selector and we can get a task.

08:17.460 --> 08:24.690
Let's try getting a move to and our move two if we select it has a blackboard key for its target.

08:24.870 --> 08:27.930
Let's say we set that to target to follow.

08:28.290 --> 08:31.410
Well what do you expect to happen if we play test.

08:31.410 --> 08:32.820
Let's check it out.

08:32.820 --> 08:34.350
We can press play.

08:34.470 --> 08:37.020
And look they're following aura.

08:38.340 --> 08:44.910
That's because every second, well, every half second, rather they're finding the nearest player and

08:44.910 --> 08:46.800
move to is being executed.

08:46.800 --> 08:52.860
So this is really powerful that we can have a service run at a specific interval.

08:53.250 --> 08:55.710
It's not run every frame of the game.

08:55.710 --> 09:01.500
It's run every 0.4 seconds with a random deviation, which is much more performant.

09:01.500 --> 09:06.420
And this is why behavior tree services are so useful.

09:06.510 --> 09:10.140
Excellent job and I'll see you in the next video.
