Confused about ExploreNeighbors() & Dictionary

Was going to respond to Michael Flores post because his solution was similar to the one I came up with for this challenge.

Unless it’s needed for something else in a later lecture, what is the purpose of using the dictionary in ExploreNeighbors(), when we have already created a GetNode() method that checks to see if the coordinates are within the dictionary bounds? It returns null if it gets coordinates for which it doesn’t have a key, so it seems much simpler to just check if GetNode() returned a node or a null.

My method ended up looking like:

 void ExploreNeighbors()
        List<Node> neighbors = new List<Node>();

        foreach (Vector2Int direction in directions)
            Vector2Int neighborCoord = currentSearchNode.coordinates + direction;
            Node neighbor = gridManager.GetNode(neighborCoord);

            if (neighbor != null)

                //TODO Remove after testing
                neighbor.isExplored = true;
                Node currentNode = gridManager.GetNode(currentSearchNode.coordinates);
                currentNode.isPath = true;

That seems to work, because my nodes are being colored correctly.


In many cases, there are multiple ways to make something work in Unity, and Gary cannot show all of them. If your code works, it’s a solution by definition. :slight_smile:

See also:

Thanks. I was mostly curious if the method Gary used was due to a performance difference or just personal preference.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms