SHARE
TWEET

Untitled

a guest Mar 19th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void UAIBlueprintHelperLibrary::SimpleMoveToLocation(AController* Controller, const FVector& GoalLocation)
  2. {
  3.     UNavigationSystemV1* NavSys = Controller ? FNavigationSystem::GetCurrent<UNavigationSystemV1>(Controller->GetWorld()) : nullptr;
  4.     if (NavSys == nullptr || Controller == nullptr || Controller->GetPawn() == nullptr)
  5.     {
  6.         UE_LOG(LogNavigation, Warning, TEXT("UNavigationSystemV1::SimpleMoveToActor called for NavSys:%s Controller:%s controlling Pawn:%s (if any of these is None then there's your problem"),
  7.             *GetNameSafe(NavSys), *GetNameSafe(Controller), Controller ? *GetNameSafe(Controller->GetPawn()) : TEXT("NULL"));
  8.         return;
  9.     }
  10.  
  11.     UPathFollowingComponent* PFollowComp = InitNavigationControl(*Controller);
  12.  
  13.     if (PFollowComp == nullptr)
  14.     {
  15.         FMessageLog("PIE").Warning(FText::Format(
  16.             LOCTEXT("SimpleMoveErrorNoComp", "SimpleMove failed for {0}: missing components"),
  17.             FText::FromName(Controller->GetFName())
  18.         ));
  19.         return;
  20.     }
  21.  
  22.     if (!PFollowComp->IsPathFollowingAllowed())
  23.     {
  24.         FMessageLog("PIE").Warning(FText::Format(
  25.             LOCTEXT("SimpleMoveErrorMovement", "SimpleMove failed for {0}: movement not allowed"),
  26.             FText::FromName(Controller->GetFName())
  27.         ));
  28.         return;
  29.     }
  30.  
  31.     const bool bAlreadyAtGoal = PFollowComp->HasReached(GoalLocation, EPathFollowingReachMode::OverlapAgent);
  32.  
  33.     // script source, keep only one move request at time
  34.     if (PFollowComp->GetStatus() != EPathFollowingStatus::Idle)
  35.     {
  36.         PFollowComp->AbortMove(*NavSys, FPathFollowingResultFlags::ForcedScript | FPathFollowingResultFlags::NewRequest
  37.             , FAIRequestID::AnyRequest, bAlreadyAtGoal ? EPathFollowingVelocityMode::Reset : EPathFollowingVelocityMode::Keep);
  38.     }
  39.  
  40.     // script source, keep only one move request at time
  41.     if (PFollowComp->GetStatus() != EPathFollowingStatus::Idle)
  42.     {
  43.         PFollowComp->AbortMove(*NavSys, FPathFollowingResultFlags::ForcedScript | FPathFollowingResultFlags::NewRequest);
  44.     }
  45.  
  46.     if (bAlreadyAtGoal)
  47.     {
  48.         PFollowComp->RequestMoveWithImmediateFinish(EPathFollowingResult::Success);
  49.     }
  50.     else
  51.     {
  52.         const ANavigationData* NavData = NavSys->GetNavDataForProps(Controller->GetNavAgentPropertiesRef());
  53.         if (NavData)
  54.         {
  55.             FPathFindingQuery Query(Controller, *NavData, Controller->GetNavAgentLocation(), GoalLocation);
  56.             FPathFindingResult Result = NavSys->FindPathSync(Query);
  57.             if (Result.IsSuccessful())
  58.             {
  59.                 PFollowComp->RequestMove(FAIMoveRequest(GoalLocation), Result.Path);
  60.             }
  61.             else if (PFollowComp->GetStatus() != EPathFollowingStatus::Idle)
  62.             {
  63.                 PFollowComp->RequestMoveWithImmediateFinish(EPathFollowingResult::Invalid);
  64.             }
  65.         }
  66.     }
  67. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top