Course contentsShow
← Unreal Engine Game Development
Lesson 453 of 8806. AI, Navigation, and Decision MakingFree lesson

Handling Interrupted States during Pathfinding

Create logic to safely handle state transitions when the AI's movement is interrupted by external forces or navigation path changes.

In this lesson, you will learn how to detect and gracefully resolve AI state transitions when external forces—like knockbacks, physics collisions, or dynamic navigation changes—interrupt a character’s active pathfinding.

The Problem of Stale Pathing

When your AI is executing a MoveTo task, it relies on a calculated path. If an external force moves the AI, or if the navigation mesh changes (e.g., a door closes or a platform shifts), the AI’s current path may become invalid. If you don't explicitly handle these interruptions, the AI might get "stuck" trying to walk through an obstacle or entering a glitched state where it thinks it is still moving toward a stale destination.

Implementation Strategy

To handle this, we utilize the OnMoveCompleted delegate provided by the AAIController. When a move is interrupted, the EPathFollowingResult will return Interrupted. You must capture this in your C++ controller or your custom Task node to trigger a state machine transition.

void AMyAIController::OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result)
{
    Super::OnMoveCompleted(RequestID, Result);

    if (Result.Code == EPathFollowingResult::Interrupted)
    {
        // 1. Notify our state machine that the current goal is invalid
        StateComponent->SetState(EAIState::Recovering);
        
        // 2. Clear stale Blackboard keys to prevent loop re-entry
        Blackboard->ClearValue(TargetLocationKey);
        
        // 3. Re-evaluate using EQS to find a new viable position
        RunBehaviorTree(BehaviorTreeAsset);
    }
}

Best Practices for Interruption

  1. Immediate State Reset: Always move the AI into a "Recovery" or "Idle" state immediately upon interruption. This prevents the Behavior Tree from fighting against the physical impulse that caused the interruption.
  2. Path Validation: In your Tick or via a Timer, periodically check GetPathFollowingComponent()->HasValidPath(). If it returns false while the AI is in a "Moving" state, force a state transition to "Searching" to re-calculate a path from the new position.
  3. Blackboard Synchronization: Ensure your Blackboard keys are cleared during an interruption; otherwise, the AI may attempt to navigate to the exact same point that caused the obstruction or collision.

Key Takeaway: Always intercept the EPathFollowingResult::Interrupted code to reset your AI state machine, clear Blackboard targets, and force a fresh EQS re-evaluation before resuming movement.