AI Tasks with MoveTo do not wait for move to complete


I’m having a problem getting my AI to move properly. What seems to be happening is that the AI doesn’t wait for the MoveTo() task to finish before moving on. I’ve included my C++ method below that runs the task. I built the blackboard and behavior tree in the editor and only switched to C++ for this single task.

I also have the same problem with the MoveToLocation node in blueprints. Am I just missing some sort of thread lock call?


EBTNodeResult::Type UFilmCarTask::ExecuteTask(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) {
	m_blackboard = OwnerComp.GetBlackboardComponent();
	AStaticMeshActor* Car = dynamic_cast<AStaticMeshActor*> (m_blackboard->GetValueAsObject("CarToFilm"));

	FVector CarLoc = Car->GetActorLocation();
	FBox CarBounds = Car->GetComponentsBoundingBox();

	AAIController* AI_con = OwnerComp.GetAIOwner();
	APawn* AI_pawn = AI_con->GetControlledPawn();
	FVector CameraLoc = AI_pawn->GetActorLocation();

	FVector ClosePoint = CarBounds.GetClosestPointTo(CameraLoc);
	FVector* Path = 0;
	genPath(CarBounds, ClosePoint, Path);
	EPathFollowingRequestResult::Type result;
	for (UINT i = 0; i < 60; ++i) {
		FAIMoveRequest moveTarget(Path[i]);
		result = AI_con->MoveTo(moveTarget);
	delete[] Path;

	if (result == EPathFollowingResult::Type::Success) return EBTNodeResult::Succeeded;

	return EBTNodeResult::Failed;

Ok, so with some more searching, I’ve found that when it comes to blueprints, they suggest you add nodes in the task blueprint to check when the pawn gets to its destination. However, what seems to me as the obvious C++ analogy locks up the editor.

while (AI_con->GetMoveStatus() == EPathFollowingStatus::Type::Moving) {}

So that’s no good. I’ve seen that in BTTask_MoveTo.cpp there appears to be a messenger service.

		result = AI_con->MoveTo(moveTarget,&outPath);
		if (result.Code == EPathFollowingRequestResult::RequestSuccessful){
			WaitForMessage(OwnerComp, UBrainComponent::AIMessage_MoveFinished, result.MoveId);
			WaitForMessage(OwnerComp, UBrainComponent::AIMessage_RepathFailed);
			NodeResult = EBTNodeResult::InProgress;
			UE_LOG(LogTemp, Warning, TEXT("Request Successful!"));

But that doesn’t force the ExecuteTask method to wait either.