WEBVTT

00:01.590 --> 00:02.610
And without delay.

00:02.610 --> 00:06.450
Let's get back into programming part five of this code.

00:06.570 --> 00:08.760
Let's go back in to find paths.

00:08.760 --> 00:13.650
So we've got our new path block here.

00:13.830 --> 00:19.050
And so I'm going to get the text messages that are attached to that object.

00:19.590 --> 00:25.140
So let's just call it values equals path block.

00:25.410 --> 00:33.180
Get components in children and you want the one with the S in it so that you're getting multiple ones.

00:33.240 --> 00:36.780
And we want to get our text.

00:37.630 --> 00:38.590
Mesh.

00:39.530 --> 00:40.550
Children.

00:41.120 --> 00:44.670
And then you'll get them in the order that they come in.

00:44.690 --> 00:50.630
So the first one is G, the next one is H, and the following one is F, as they are attached to that

00:50.630 --> 00:51.710
particular prefab.

00:51.710 --> 00:59.330
So we can go values at zero dot text equals and then just put some text in there.

00:59.870 --> 01:02.960
I'm going to put a G like that.

01:04.290 --> 01:12.360
Plus G to string with a little bit of formatting so we don't get something ridiculous with a lot of

01:12.360 --> 01:14.670
decimal places like that.

01:14.670 --> 01:21.240
And then we just need these for the H and the F, so let's just paste that twice more.

01:21.660 --> 01:36.540
Go to value one for the H and two for the F and this is H, this is F and then this is H and this is

01:37.320 --> 01:41.710
F, almost finished the interior of this fall.

01:41.880 --> 01:48.900
The last thing we need to do is to add the marker to our open list.

01:48.900 --> 01:54.900
But before we do that, we have to make sure that it doesn't already exist because of it does exist.

01:54.900 --> 01:57.360
We just want to update its values.

01:57.600 --> 01:59.520
So it might already be in the open list.

01:59.520 --> 02:01.620
And then we're looking at it from a different point of view.

02:01.620 --> 02:08.310
So from a different node it becomes that node's neighbor, we still need to update the g, h and F values.

02:08.640 --> 02:13.260
So I'm going to create a new method to do that.

02:13.260 --> 02:15.420
And once again, we'll put it down under search.

02:15.420 --> 02:17.520
Come down here, pull.

02:18.630 --> 02:21.090
Update marker.

02:22.480 --> 02:27.370
Map location pose float g.

02:27.370 --> 02:29.230
Float h.

02:29.230 --> 02:33.520
Float if and then path.

02:34.290 --> 02:35.050
Marco.

02:35.430 --> 02:38.640
And then inside of here for each.

02:39.780 --> 02:55.470
Path marker p in open if p dot location dot equals pause, then we're talking about the same path marker.

02:55.830 --> 02:58.920
And then inside of here we can just update its value.

02:58.920 --> 03:12.450
So p g equals g, p h equals h and p dot f equals f.

03:13.020 --> 03:13.440
Okay.

03:13.560 --> 03:15.630
So this just needs to be fixed up.

03:15.870 --> 03:16.840
P dot.

03:17.630 --> 03:25.970
H and then F and then we also need to set the parent of that marker to the node that we're currently

03:25.970 --> 03:26.600
working on.

03:26.600 --> 03:36.650
So P dot parent equals l parent variable that we've passed through, then we can return true saying

03:36.650 --> 03:38.060
yes, we've dealt with it.

03:38.180 --> 03:40.040
Otherwise we looked all the way through here.

03:40.040 --> 03:45.350
We haven't found it so we can return false and then deal with it back in the other code.

03:45.770 --> 03:50.480
So let's go back up to our search where we will now use this update marker.

03:50.480 --> 03:52.310
And in here we're going to put if.

03:53.440 --> 03:56.020
Not update marker.

03:56.200 --> 04:05.920
Passing through the neighbor, the g value, the h value and the F value and then the node.

04:05.920 --> 04:13.240
So this node which will then become its parent otherwise open add new path marker.

04:14.180 --> 04:18.290
Neighbour g h if.

04:19.230 --> 04:22.650
Puff block and this node.

04:23.040 --> 04:23.630
Okay.

04:23.640 --> 04:24.150
Phew.

04:24.180 --> 04:24.910
A lot of code.

04:24.930 --> 04:26.630
We're almost done.

04:26.640 --> 04:31.470
We're almost where we can actually test it, but not quite yet.

04:31.470 --> 04:40.920
So we've looped through all of these neighbours and we've calculated their FG and F values and we have

04:40.920 --> 04:43.860
essentially put them into the open list.

04:44.220 --> 04:46.030
What do we do next?

04:46.050 --> 04:51.060
Well, we have to pick one that's going to become the next node that we're focusing on.

04:51.060 --> 04:58.260
So the position that's going to generate the new lot of neighbors, what determines the one that's chosen?

04:58.260 --> 05:00.350
So which one in the open list do we get?

05:00.360 --> 05:03.450
Well, it's the one with the lowest F value.

05:03.450 --> 05:11.880
So the easiest way to do that is to reorder your open list from the smallest to the largest F values.

05:11.880 --> 05:14.130
And we can use the link library to do this.

05:14.250 --> 05:25.500
So we set our open list to equal our open list dot ordered or order by and we're ordering by a value

05:25.500 --> 05:29.670
that is from p f, that's basically what that says.

05:29.670 --> 05:34.980
And then dot make it back into a list to list of path markers.

05:35.370 --> 05:41.010
Now I'm just coming back to this line in the code that I know you've just typed in, but it's like a

05:41.010 --> 05:46.710
while down the track for me and I just want to make you aware of what's going on with this order by

05:46.710 --> 05:52.950
so while this will work, imagine that you've got all of these open nodes and you've ordered them by

05:52.950 --> 05:59.100
the F value and there's several of them sitting up at the top and they all have the same f value.

05:59.100 --> 06:01.610
Then which one of those do you pick?

06:01.620 --> 06:09.810
Well, it makes more sense if we then pick the one that has the highest h value.

06:10.080 --> 06:17.520
So what we're going to do in this case is do another order inside of this ordering and we do that by

06:17.520 --> 06:19.280
using a dot.

06:19.890 --> 06:29.280
Then by now this is just part of our link library to be able to do this and we're going to put n equals

06:30.090 --> 06:41.640
greater than n dot h in there and that will then order by F and then within all of the F of the same,

06:41.640 --> 06:47.340
it'll order them secondly by the H and that will improve this algorithm.

06:47.340 --> 06:53.070
Now, I'm going to go back to the previous version of the code that you'll see just in a moment, because

06:53.070 --> 06:55.770
I continued typing from then on.

06:55.770 --> 07:01.050
But just remember, whenever you see this line in the code from now on, then we need to have this,

07:01.050 --> 07:05.310
then buy in there instead of my old version of it.

07:05.850 --> 07:06.300
Right?

07:06.300 --> 07:07.050
That's done.

07:07.050 --> 07:14.010
Next we can select the one at the top of that list because it's going to be the one with the smallest

07:14.010 --> 07:14.660
f k.

07:14.730 --> 07:16.740
So path marker.

07:17.620 --> 07:18.490
PM.

07:19.910 --> 07:22.940
Equals path marker.

07:23.810 --> 07:25.760
Open list dot.

07:27.390 --> 07:29.910
Element at position zero.

07:29.910 --> 07:34.200
So the one on the very top, let's make that in to a bracket.

07:34.680 --> 07:39.780
Okay, then we can close that path marker.

07:39.780 --> 07:44.220
So we're taking it out of the open list and we're putting it into the closed list.

07:44.220 --> 07:49.350
So closed don't add path marker like that.

07:49.350 --> 07:49.830
Right?

07:49.830 --> 07:54.360
So we've done that, we've closed it off and therefore we need to remove it out of the open list.

07:54.360 --> 08:00.810
Now remember, it's sitting on the top of the open list so we can go open remove at zero.

08:01.170 --> 08:05.280
So taking it out of one and putting it into the other.

08:05.280 --> 08:10.260
And at the same time, we're also going to set its material to that red color that we selected for closed.

08:10.260 --> 08:17.490
So pm dot marker dot get component.

08:18.600 --> 08:23.310
Renderer material equals the closed material.

08:23.670 --> 08:30.450
Last but not least, we now need to set our last position, which was initially start.

08:30.450 --> 08:32.520
If you remember, last pause.

08:32.520 --> 08:41.010
The node that will get run next equals our path marker with the lowest F, so that next time we come

08:41.010 --> 08:50.820
commence loop through into here, this node will be set down here to this last node.

08:51.570 --> 08:55.410
Okay, I think we're actually ready to run this.

08:55.410 --> 08:58.800
So let's go down into our update.

08:59.070 --> 09:03.810
What we're going to do down in here is use a different key to step through our a star.

09:03.810 --> 09:13.560
So if input dot get key down is I'll use C not for any particular reason except for s is usually used

09:13.560 --> 09:14.370
for navigation.

09:14.370 --> 09:21.450
So I'll just pick a key and then we're going to run a search and we're going to pass through the last

09:21.750 --> 09:26.520
pause, which at the start will be the first node.

09:26.670 --> 09:34.070
Now, if you don't press P before pressing C, then you're going to end up with a bit of an error because

09:34.090 --> 09:36.030
last pause will actually be null.

09:36.030 --> 09:42.690
So just to get over that issue, if you come up into your search, what you want to do is just put this

09:42.690 --> 09:47.850
line of code in here to make sure that pressing C will do absolutely nothing.

09:47.850 --> 09:53.640
If you haven't pressed P first and we'll come back and try this out in the next video.
