Hello, at first - awesome project, thank you!
I would like to get advice on how to proceed with the following scenario (I thought it easy at first, but I am unable to fix it for days).
I’ve been struggling with a path distance limitation. I use hex node graph and turn-based approach, the units can cross nodes until they have no stamina, and the logic is implemented outside pathfinding (e.g. each node has associated script that handles the logic because many things were an obstacle using nodes for this, for example, BFS is cut on non-walkable nodes).
The problem is, I can’t get the unit to stop if the path is being too long to complete with the stamina available. I have a variable that knows whether next movement is possible (if stamina <= 0) kill, but can’t kill the path properly. I also modified the AIPath script to provide me a callback on each new node crossed so I can perform custom functionality on each path node (it is not called once the unit is exactly in the middle of the new node – I could not find out how to do that – but rather once a new node is closer than the previous node).
GraphNode lastVisited = null;
private void OnNewNodeEnteredInternal(Vector3 currentSimulatedPosition) {
NNInfo nn = AstarPath.active.GetNearest(currentSimulatedPosition, NNConstraint.Default);
if (nn.node != null && lastVisited != nn.node) {
lastVisited = nn.node;
OnNewNodeEntered(nn.node);
}
}
Here’s what I’ve tried:
Custom ITraversalProvider implementation:
public new bool CanTraverse(Path path, GraphNode node)
{
MapTile tile = Map.FromNode(node);
//stamina variable was defined here and read through property alias in the unit
return tile.IsWalkable() && stamina > 0 && base.CanTraverse(path, node);
}
But nothing has changed.
I also tried to kill the path manually, using the custom new node callback:
if (stamina <= 0)
{
currentPath = null; //currently traversed path
path.isStopped = true; //associated AIPath script
path.OnPathFinished((Vector3)newNode.position);
}
else
{
stamina -= Map.FromNode(node).NodePrice;
}
but this approach kills the path earlier, that is the unit is not anymore on the node but stopped before reaching the node. I would like the unit between nodes C and D with stamina = 0 on a path from A to G to continue to the node D and stop once there, not on the halfway.
Thank you for any suggestions!