We encountered some issues while adapting the turn-based example on this page:
https://arongranberg.com/astar/docs/turnbased.php
We ultimately got the code to work, but not without several edits/workarounds. This A* Pathfinding Project is quite well-used, so we wonder if perhaps we are doing something wrong. We are using version 4.0.10.
-
We copied the code in BlockerPathTest.cs, but changed line 14 to use BlockManager.BlockMode.AllExceptSelector instead of BlockManager.BlockMode.OnlySelector . After doing so, we passed in 0 arguments to the “obstacles” list, which we figured would treat everything as an obstruction. It did not–the path was drawn straight through the sphere. Our workaround was to pass 1 argument (not the sphere) to the “obstacles” list, which resulted in the path being drawn around the sphere as we believe is proper.
-
We tried applying the example code to objects in our 2-D game. There are about 20 objects on our 17x15 grid. To each object were attached SingleNodeBlocker.cs and BlockerTest.cs. Then the BlockerPathTest script (using BlockManager.BlockMode.AllExceptSelector on line 14) was added to one of the objects and given another object as the target. The object itself was passed into the obstacles list so as to prevent pathing around itself. (This also suffices as the workaround for Issue 1.) We observed that the path was being drawn straight through objects that should have been obstructions. Investigation with a debugger revealed that line 102 of BlockManager.cs wasn’t finding anything because the hashcode of the node being looked for was different than the hashcode of the corresponding node in the “blocked” dictionary. Workaround: in GridNodeBase.cs, we overrode methods GetHashCode() and Equals(object obj) to ensure that a node at a particular X-Z position always returns a consistent hashcode.
-
After the above changes, the pathing algorithm was properly finding nodes in the “blocked” dictionary, but misevaluating whether a node was an obstruction. We determined that the logic BlockManager.NodeContainsAnyExcept() was returning incorrect results. Workaround: we replaced lines 108-114 (a FOR loop) with this condition:
if (selector.Contains(inNode) == false) { return true; }
As mentioned, after making the above changes, the pathing in our game now seems to be working correctly. But it seems unlikely that we’d be the first to encounter issues of this significance on such a well-used project. Are we perhaps doing something incorrectly?