# I need help with A* pathfinding searching the neighbours for lowest GCost

This topic is 1265 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need help with A* pathfinding searching the neighbours for lowest GCost

Guys I got the a* pathfinder almost down but my search neighbors for the lowest GCost code isn't working.

That code below should compare the last closeList index closeListNum with the the openList -7 index. It searches 8 moves back for the lowest GCost. I hope someone can help me out with this. :)

// search neighbours
lowestGCost = closeList[i-7].GCost;
for (int i = openListNum-6; i < openListNum; i++)
{
if (openList(i).GCost < lowestGCost)
{
lowestGCost = openList.GCost;
if (lowestGCost < closeList[closeListNum].GCost)
{

closeList[closeListNum].Num = openList.Num;
closeList[closeListNum].GridX = openList.GridX;
closeList[closeListNum].GridY = openList.GridY;
closeList[closeListNum].Heuristic = openList.Heuristic;
closeList[closeListNum].Parent = openList.Parent;
closeList[closeListNum].GCost = openList.GCost;
closeList[closeListNum].FCost = openList.FCost;

}

}
}

##### Share on other sites

oops the code was wrong a little bit here is the new code update.

// search neighbours
lowestGCost = openList[i-7].GCost;
for (int i = openListNum-6; i < openListNum; i++)
{
if (openList(i).GCost < lowestGCost)
{
lowestGCost = openList.GCost;
if (lowestGCost < closeList[closeListNum].GCost)
{

closeList[closeListNum].Num = openList.Num;
closeList[closeListNum].GridX = openList.GridX;
closeList[closeListNum].GridY = openList.GridY;
closeList[closeListNum].Heuristic = openList.Heuristic;
closeList[closeListNum].Parent = openList.Parent;
closeList[closeListNum].GCost = openList.GCost;
closeList[closeListNum].FCost = openList.FCost;

}

}
}

##### Share on other sites

Is openListNum guaranteed to be greater than or equal to 7 every single time you reach this code?  Strange things would likely happen if openListNum were 3, for example, since you'd be reading incorrect memory.  But that's just a guess; you never mentioned what actually happens, other than that presumably you don't get the path you were hoping for.

Have you tried stepping through the code with a debugger?  If not, now is the perfect to learn how.  The end-result behavior of a misbehaving pathfinding algorithm can be difficult to interpret.  Watching the code execute line by line and observing the values it produces can make the error very obvious in many cases, though.

##### Share on other sites

Yeah, that -6 and -7 both look really weird to me, but maybe fine depending on implementation?  I just am thinking of say, the corner of a 2d grid, it only has 3 neighbors.