Checking Path Validity on Specific Grid Graphs with Tags and Penalties

  • A* version: 5.3.0
  • Unity version: 2022.3.49f1
    I need help with checking if a path is still valid within a specific grid graph setup. Here’s what I’m working with:
  • I have stored a Path object (Path path).

  • I’m using three different grid graphs.

  • I need to ensure the path check is performed on the correct grid graph.

  • The check must consider tags and penalties associated with that particular graph.

How can I verify if the path (path) is still valid considering these factors?

So I’m not fully sure what’s the best way to take a Path and see how it would be affected by penalty, per agent. I’ll have to defer that one to @aron_granberg (so I can also find out what’s the best way here) :slight_smile:

However in the intermediary I can give you a few useful resources:

First, CanTraverse will check to simply see if you can go from point A to point B. As for, per agent, I’m not 100% sure. This does check if the tags allow it to be walkable, however I’m not sure about the interaction between it and penalties.

Second, you may also get some mileage from ITraversalProvider. There are examples of how this can be used in the Agent-Specific Pathfinding documentation. I have a strong feeling this may be more in line of what you’re looking for?

What do you define as “valid” in this context? Considering you also want to account for penalties and similar.

Thank you very much for your reply! I truly appreciate the support.

To clarify my use case: In my game, I have NPCs that repeatedly walk between two points (A and B). To optimize performance, I cache the path once and reuse it for subsequent trips.

However, since this is a city builder game, the graph can change dynamically. For instance:

  • If a building is placed, the graph is updated with unwalkable nodes.
  • Some areas might also be marked with a specific tag that makes them non-traversable for certain agents, depending on their Seeker settings.

Because of this, I need to ensure that the cached path is still valid before reusing it. The key is that this validation should be based on the Seeker’s specific settings (e.g., whether it can traverse certain tags). I don’t want penalties to be considered here since they don’t affect walkability in this context.

Ideally, it would be great if there was a Seeker.IsPathValid(Path path) function that takes a Path object and validates it. However, since this function doesn’t exist, I’m wondering:

  1. What is the next best solution to achieve this?
  2. Is it more efficient to validate a path for reuse, or is it generally faster to simply recalculate the path from scratch?

Looking forward to your thoughts! Thank you for all your hard work on this amazing pathfinding system.

Hi

I think you can do something like:

bool IsValid(Path path) {
    for (int i = 0; i < path.path.Count; i++) {
        if (path.path[i].Destroyed) return false;
        if (!path.CanTraverse(path.path[i])) return false;
    }
    return true;
}

Recalculating a path from scratch is typically not that expensive, though. So unless pathfinding actually shows up in your profiler, I wouldn’t worry too much about optimizing it.

Also. Keep in mind that even if a path stays valid, it may not be the shortest path anymore.

Thanks again for the reply.
Does the Path object contains information about which graph it was calculated on or does the path.CanTraverse just take the first graph it find.

path.path is a list of nodes that the path traversed. So yes, it knows which graph it is checking.