GetRemainingPath not updated fast enough

I am using a Grid graph, and after the path is calculated OnPathCalculated callback is executed. (from _seeker.pathCallback that I subscribed to).

When I iterate the original path that came back with

foreach (GraphNode node in calculatedPath.path)
        {
            Debug.Log($"Node: {(Vector3)node.position}");
        }

I can see the entire path calculated correctly.

However, in that same method I then call GetRemainingPath. It always seems to at the beginning return some outdated version of the path (just first 3 subnodes), nothing to do with the new path.

This GetRemainingPath is crucial for me to do some checks immediately. Is there any way I can guarantee (or wait for) GetRemainingPath to return the actual LATEST information about the path so that I can then do something with it?

Hi

Sorry for the late reply.
Yeah, the GetRemainingPath for the RichAI (which I assume you are using), is only updated when its movement calculations run. You could run ai.MovementUpdate(0.0f) before you get the remaining path. I think that should do all calculations to make sure it is up to date.

Hi. I am using AIPath, so I could only do something similar to:

_aiPath.MovementUpdate(0.0f, out Vector3 _, out Quaternion _);

However, this didn’t work. The GetRemainingPath method still returned a stale path and didn’t include the very early subnodes of the path that was just calculated.

Any idea why this is acting like this?

Hi

The AIPath updates the remaining path immediately when it receives the new path. In that case, it might be that you are subscribed to receive the seeker.pathCallback before the AIPath receives it (it also subscribes to the same event). So the AIPath will not even be aware that a new path has been calculated yet. If you are currently registering your pathCallback during Awake, try regestering it during Start instead.

Hi

I’m doing this in a less conventional way. I have a custom C# script which handles pathfinding, but it isn’t a MonoBehavior. Instead, it places the AIPath on another monobehavior. Here’s approximately what it looks like:

public Pather(PatherDef def, Creature creature)
    {
        ...
        AttachPatherGameObject();
    }

 private void AttachPatherGameObject()
    {
        GameObject worldGo = _creature.Visualization.WorldGameObject;

        _seeker = worldGo.AddComponent<Seeker>();
        _seeker.pathCallback += OnPathCalculated;

        _aiPath = worldGo.AddComponent<AIPath>();
        // ... other aiPath settings
   }

In this case, how would I get the solution to work? I’m not sure how registering the pathCallback during Start instead of Awake would help, but I can’t do it anyway because of this approach.

I call the GetRemainingPath inside of the OnPathCalculated method which subscribes to the seeker.PathCallback. However, it still didn’t return the new result in time it seems.

Thank you!

In this case you register to the pathCallback before you add the AIPath component. That means you’ll get the callback first. If you instead register to the callback after you run AddComponent, then the order will be reversed.

1 Like