Odd issue in pathfinding, comparing PathNode objects always returns true

I’m getting an odd issue in the path finding that
if (currentNode == endNode) always returns true.
if I use : if (currentNode.GetGridPosition() == endNode.GetGridPosition()) I get the expected result that say the node at grid pos 0,0 <> 7,5
I’m also finding that “closedList.Contains(neighbourNode)” also always says the item contains the node.

What I have tried to do is add an equals operator (and the respective != etc) and compare the grid positions. This seems to show that I can compare GridPositions of path nodes but of course fails later in the code in Calculate Path where “currentNode.GetCameFromPathNode() == null” as this will fail to get a grid position for object null (trying to check for null in the override operator goes into an infinite recursion as I’m comparing it to equal and hence stack dumps, but I think that is a side issue).

I’ll look later on at writing a test case to add to this thread but wondered if someone had encountered this before?
In FindPath, I have as per the tutorial:
PathNode endNode = gridSystem.GetGridObject(endGridPosition);
PathNode currentNode = GetLowestFCostPathNode(openList);
Debug.Log(“Pre If check curr” + currentNode + " ==? " + endNode); << this returns currentNode as 0,0 and end node as say 7,5
if (currentNode == endNode) < this always returns true.

I’m wondering if the comparison is checking the object type in c# rather than the object instance ids/HashCodes?

I do not have this issue. What does you PathNode code look like? Perhaps it’s something small in there that could be rectified

I think that comment may have just given me the answer. Pasted the code into notepad, then realized the only oddity was that it was using MonoBehaviour as a base class. Removed that and all of a sudden comparisons started to work. I guess MonoBehaviour must have some override for comparison in there :slight_smile:

1 Like

All good. As long as it’s sorted. Glad I could ‘help’ :grin:

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

MonoBehaviours cannot exist without being attached to a GameObject. They also come with a lot of overhead that we don’t want in our Pathnodes.

1 Like

Privacy & Terms