And without delay.
Let's get back into programming part five of this code.
Let's go back in to find paths.
So we've got our new path block here.
And so I'm going to get the text messages that are attached to that object.
So let's just call it values equals path block.
Get components in children and you want the one with the S in it so that you're getting multiple ones.
And we want to get our text.
Mesh.
Children.
And then you'll get them in the order that they come in.
So the first one is G, the next one is H, and the following one is F, as they are attached to that
particular prefab.
So we can go values at zero dot text equals and then just put some text in there.
I'm going to put a G like that.
Plus G to string with a little bit of formatting so we don't get something ridiculous with a lot of
decimal places like that.
And then we just need these for the H and the F, so let's just paste that twice more.
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
F, almost finished the interior of this fall.
The last thing we need to do is to add the marker to our open list.
But before we do that, we have to make sure that it doesn't already exist because of it does exist.
We just want to update its values.
So it might already be in the open list.
And then we're looking at it from a different point of view.
So from a different node it becomes that node's neighbor, we still need to update the g, h and F values.
So I'm going to create a new method to do that.
And once again, we'll put it down under search.
Come down here, pull.
Update marker.
Map location pose float g.
Float h.
Float if and then path.
Marco.
And then inside of here for each.
Path marker p in open if p dot location dot equals pause, then we're talking about the same path marker.
And then inside of here we can just update its value.
So p g equals g, p h equals h and p dot f equals f.
Okay.
So this just needs to be fixed up.
P dot.
H and then F and then we also need to set the parent of that marker to the node that we're currently
working on.
So P dot parent equals l parent variable that we've passed through, then we can return true saying
yes, we've dealt with it.
Otherwise we looked all the way through here.
We haven't found it so we can return false and then deal with it back in the other code.
So let's go back up to our search where we will now use this update marker.
And in here we're going to put if.
Not update marker.
Passing through the neighbor, the g value, the h value and the F value and then the node.
So this node which will then become its parent otherwise open add new path marker.
Neighbour g h if.
Puff block and this node.
Okay.
Phew.
A lot of code.
We're almost done.
We're almost where we can actually test it, but not quite yet.
So we've looped through all of these neighbours and we've calculated their FG and F values and we have
essentially put them into the open list.
What do we do next?
Well, we have to pick one that's going to become the next node that we're focusing on.
So the position that's going to generate the new lot of neighbors, what determines the one that's chosen?
So which one in the open list do we get?
Well, it's the one with the lowest F value.
So the easiest way to do that is to reorder your open list from the smallest to the largest F values.
And we can use the link library to do this.
So we set our open list to equal our open list dot ordered or order by and we're ordering by a value
that is from p f, that's basically what that says.
And then dot make it back into a list to list of path markers.
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
while down the track for me and I just want to make you aware of what's going on with this order by
so while this will work, imagine that you've got all of these open nodes and you've ordered them by
the F value and there's several of them sitting up at the top and they all have the same f value.
Then which one of those do you pick?
Well, it makes more sense if we then pick the one that has the highest h value.
So what we're going to do in this case is do another order inside of this ordering and we do that by
using a dot.
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
greater than n dot h in there and that will then order by F and then within all of the F of the same,
it'll order them secondly by the H and that will improve this algorithm.
Now, I'm going to go back to the previous version of the code that you'll see just in a moment, because
I continued typing from then on.
But just remember, whenever you see this line in the code from now on, then we need to have this,
then buy in there instead of my old version of it.
Right?
That's done.
Next we can select the one at the top of that list because it's going to be the one with the smallest
f k.
So path marker.
PM.
Equals path marker.
Open list dot.
Element at position zero.
So the one on the very top, let's make that in to a bracket.
Okay, then we can close that path marker.
So we're taking it out of the open list and we're putting it into the closed list.
So closed don't add path marker like that.
Right?
So we've done that, we've closed it off and therefore we need to remove it out of the open list.
Now remember, it's sitting on the top of the open list so we can go open remove at zero.
So taking it out of one and putting it into the other.
And at the same time, we're also going to set its material to that red color that we selected for closed.
So pm dot marker dot get component.
Renderer material equals the closed material.
Last but not least, we now need to set our last position, which was initially start.
If you remember, last pause.
The node that will get run next equals our path marker with the lowest F, so that next time we come
commence loop through into here, this node will be set down here to this last node.
Okay, I think we're actually ready to run this.
So let's go down into our update.
What we're going to do down in here is use a different key to step through our a star.
So if input dot get key down is I'll use C not for any particular reason except for s is usually used
for navigation.
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
pause, which at the start will be the first node.
Now, if you don't press P before pressing C, then you're going to end up with a bit of an error because
last pause will actually be null.
So just to get over that issue, if you come up into your search, what you want to do is just put this
line of code in here to make sure that pressing C will do absolutely nothing.
If you haven't pressed P first and we'll come back and try this out in the next video.