StartPath() do not update Astar Path in same frame?

I find out Astar still use the old path information by the below code:

void DrawRobotMoveLine()
    {
        walkLine.enabled = true;
        walkLine.startColor = Color.green;
        walkLine.endColor = Color.green;
        selectRBCursor.gameObject.SetActive(true);

        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag(Data.TagTerrain))
        {        
            currentSelectedRobotSeeker.StartPath(currentSelectedRobot.position, hit.transform.position, OnPathComplete);
        }
        
        // StartPath() cause the robot moving, but it should not
        void OnPathComplete(Pathfinding.Path p)
        {                        
            currentSelectedRobotAStar.canMove = false;

            // Bug here, Astar on object still use old path even after StartPath called
            Debug.Log(Time.time + " " + currentSelectedRobotAStar.remainingDistance + " " + hit.transform.name);
            if (TileManagement.MoveStepCheck(currentSelectedRobotAStar.remainingDistance, currentSelectedRobotAI.MaxMoveStep))
            {
                currentRobotDestination = hit.transform.position;

                // shift up, so not overlap with map
                walkLine.SetPosition(0, currentSelectedRobot.position + 0.2f * Vector3.up);
                walkLine.SetPosition(1, currentRobotDestination + 0.2f * Vector3.up);
                selectRBCursor.position = currentRobotDestination + 0.2f * Vector3.up;
            }
        }
    }

Related to: The seeker start to move when call StartPath()?

You can start/stop the agent by modifying the isStopped value

Hi

OnPathCompleted is called when the path calculation is finished. This may take more than one frame.
You can force the path to be calculated immediately by calling path.BlockUntilCalculated().

var path = currentSelectedRobotSeeker.StartPath(currentSelectedRobot.position, hit.transform.position, OnPathComplete);
path.BlockUntilCalculated();
// The OnPathComplete callback will have been called now

I still get the path null error, but it happens once only. The null error pop many times if I don’t add the path block calculation function. I don’t know why, anyway I put a null check to solve the problem.

But I find another similar problem on the AstarPath.Scan(). The log shows the scan only take 5ms to complete. But what i see on the editor is the grid graph do not update until robot finish move, which take few secs.

Before robot move, it will disable its own obstacle, so the obstacle will not block itself.

currentSelectedRobotAI.AstarScanMask.SetActive(false);
                        AStar.Scan();

and then it start to move

                                path = _rbSeeker.StartPath(transform.position, destination);                                
                                path.BlockUntilCalculated();                                    
                                _rbAI.Move(destination);

when the robot reach destination, turn on its obstacle again.

 if (IAStarAI.reachedDestination)
                    {                  
                    IAStarAI.canMove = false;
                    AstarScanMask.SetActive(true);   }

From what I see in editor, when robots move one by one, robot(i) use the grid graph robot(i-1) generated ( why the delay?)

And the grid graph only update right after a robot reach destination. I wonder any moving object will forbid grid graphic update?

Shit, I find the problem. the robot start to move too early before scan() execute.

1 Like