Game hangs when placing tower after the Broadcast message

So I’ve gotten as far as adding the broadcast message to fire the recalculate path method once placing down a tower.

When I pass true into the NotifyReceivers parameter it works but obviously the enemies will reset back to the start coordinate

    {
        BroadcastMessage("RecalculatePath", true, SendMessageOptions.DontRequireReceiver);
    }
}

When i use …


    {

        BroadcastMessage("RecalculatePath", false, SendMessageOptions.DontRequireReceiver);

    }
}

When placing my tower the game will hang and I have to close it and reopen which looks to me like an infinite loop.

I’ve debugged enough to find that once the path is recalculated, the path.Count keeps going up and never stops which led me to believe there was an issue with this code block

    {
        List<Node> path = new List<Node>();
        Node currentNode = destinationNode;

        path.Add(currentNode);
        currentNode.isPath = true;
        //go back through the nodes and add them to the path
        while (currentNode.connectedTo != null)
        {
            currentNode = currentNode.connectedTo;
            path.Add(currentNode);
            currentNode.isPath = true;
            Debug.Log(path.Count);

        }
        path.Reverse();
        return path;
    }

But to me, everything here matches with the rest of the lecture changes?

Once I remove the Broadcast message all is fine but the enemies won’t recalculate their path on tower placement.

Here is my Repo & branch im working on for reference https://github.com/cozrodgers/TowerDefenseV2/tree/feature/improving-the-path

I would very much appreciate the help!

Thanks :slight_smile:

Hi Corey,

Have you checked all your classes?

What you described sounds indeed like an endless loop. And we have this condition in the while-loop: (currentNode.connectedTo != null).

Maybe you could log the instance id (GetInstanceID) of the Node objects assigned to currentNode into your console to see if currentNode.connectedTo references itself or something like that. Since we have a limited number of Waypoint objects, the only reason that (currentNode.connectedTo != null) gets never evaluated to false is that the “last” Node object references an object that has already been referenced by another object. Only then, path.Count will increment “forever”.

You’ll probably have to debug your other classes, too.

1 Like

Hey Nina thanks for the fast reply.

After spending more time I finally fixed the issue,

Firstly in the BreadthFirstSearch method in pathfinder.cs I was adding the startCoords to the reached dictionary instead of adding the coordinates. This fixed the hanging,

There was also a nested foreach loop in my explore neighbors due to not adding the correct amount of braces slowing down calculations.

Good job on fixing the problem. :slight_smile:

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

Privacy & Terms