Just in case someone ever wonders if this alternative code was still viable through to video “31_RR_UY3”.
At first I had a nasty bug with the next lecture that I couldn’t find for more than a day. It just stopped working. I poured over it and stared at it for hours and couldn’t figure out what I did wrong after we altered the code. It drove me mad.
in the end it was a wrong definition of a node variable. Also the new node in my first code snippet was superfluous, not sure what I thought, adding it in, in the first place.
Code works like a charm again.
Annotation: for some reason, my list in the code always gets emptied out of it’s type, when I post the code. Not sure why. It’s a vector2int one, naturally.
void ExploreNeighbors() //my version with vector2int
{
List neighbors = new List();
foreach (Vector2Int direction in directions)
{
Vector2Int neighbor = new Vector2Int((currentSearchNode.coordinates.x+direction.x),(currentSearchNode.coordinates.y+direction.y));
if (grid.ContainsKey(neighbor))
{
neighbors.Add(neighbor);
Debug.Log(grid[neighbor].coordinates + " - " + grid[neighbor].isWalkable);
}
}
foreach (Vector2Int neighbor in neighbors)
{
Node node = gridmanager.getNode(neighbor);
if (!reached.ContainsKey(neighbor) && node.isWalkable)
{
node.connectedTo = currentSearchNode;
reached.Add(neighbor, node);
frontier.Enqueue(node);
}
}
}