Challenge in writing ExploreNeighbors() ourselves

This is what I came up with in the challenge, minus the debug code that Gary added in later to test.

Is it in some way problematic down the line? Seems to work just as well so far.

void ExploreNeighbors()
{
List neighbors = new List();

    foreach (Vector2Int direction in directions)
    { 
        Vector2Int neighbor = new Vector2Int((currentSearchNode.coordinates.x+direction.x),(currentSearchNode.coordinates.y+direction.y));
        Node node = gridmanager.getNode(neighbor);
        if (node == null) { return; }
        neighbors.Add(neighbor);
        
        //TODO Remove after Testing
        grid[neighbor].isExplored = true;
        grid[currentSearchNode.coordinates].isPath = true;
    }
}
1 Like

Welcome to the community!

Is a little hard to predict if your code will cause issues on future lectures, so you have two options: 1) Stay with your code and deal with possible future consequences, which is great practice, there’s no such thing as perfect code, those kind of things tend to happen a lot, or 2) copy Gary’s code, which is fine, it will prevent issues if any and it’ll keep you on a great track to finish the course. Entirely up to you.

1 Like

Thank you for your answer.

I’ll go with 1 and see how far I get with it as practice and keep Gary’s Code as a backup, commented out underneath, in case it’s needed.

I am just immensely happy I actually found a solution on my own, because so far, I tend to sometimes get lost within the different references and converting/ juggeling of values to get what we want.

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);
        }

    }
}

As for the List type going astray when posting, it could be that the web page doesn’t like the angle brackets so well since they’re used for writing HTML tags as well, so it could be that the page just filters them (and the string inbetween) out when it saves the posting.

As for issues with the code… I would really recommend making use of the vector addition instead of adding the x and y values of the coordinates and the direction manually. Being able to use that kind of notation (as well of all the other vector operations like Distance() ) will make your life much easier (and is the point of having vectors readily available as datatypes in the first place).
How much of a performance difference there is between grabbing a reference for the grid and accessing it directly inside the Pathmanager versus always calling a method in the GridManager would probably require some benchmarking with a sufficiently large map, and how much can be optimized away for a standalone build (as opposed to the Unity editor). The advantage of using the GridManager would be that there could be some extra logic in the GetNode() method that gets lost when directly operating on the grid.
(One possible use that comes to mind would be a grid where access to some areas depends on conditions that go beyond the simple “isWalkable” flag and which might be under control of a more sophisticated GridManager.)

Privacy & Terms