Trying to use (AI MoveTo) Node to move AI but in C++.

Trying to use AI MoveTo Node to move ai but in c++ but do not know how

i am not sure but found this function:

UAIAsyncTaskBlueprintProxy* UAIBlueprintHelperLibrary::CreateMoveToProxyObject(UObject* WorldContextObject, APawn* Pawn, FVector Destination, AActor* TargetActor, float AcceptanceRadius, bool bStopOnOverlap)

under Source\Runtime\AIModule\Private\Blueprint\AIBlueprintHelperLibrary.cpp.

And also Do Not Know how should i implement OnSuccess and OnFail Output of this function.

Screenshot 2023-08-13 002714

1 Like

This blueprint function behaves differently than the c++ functions since it returns an async task. That blueprint version is blueprint internal, so you can’t use it directly in c++.

Instead, you can use any of the following. The first one is the closest function to the blueprint one, and the most powerful:

  • FPathFollowingRequestResult AAIController::MoveTo(const FAIMoveRequest& MoveRequest, FNavPathSharedPtr* OutPath)
  • EPathFollowingRequestResult::Type AAIController::MoveToActor(AActor* Goal, float AcceptanceRadius, bool bStopOnOverlap, bool bUsePathfinding, bool bCanStrafe, TSubclassOf<UNavigationQueryFilter> FilterClass, bool bAllowPartialPaths)
  • EPathFollowingRequestResult::Type AAIController::MoveToLocation(const FVector& Dest, float AcceptanceRadius, bool bStopOnOverlap, bool bUsePathfinding, bool bProjectDestinationToNavigation, bool bCanStrafe, TSubclassOf<UNavigationQueryFilter> FilterClass, bool bAllowPartialPaths)

They return an enum or a struct that should have the info you need.

The enum:

UENUM(BlueprintType)
namespace EPathFollowingRequestResult
{
	enum Type
	{
		Failed,
		AlreadyAtGoal,
		RequestSuccessful
	};
}

The struct:

struct AIMODULE_API FPathFollowingRequestResult
{
	FAIRequestID MoveId;
	TEnumAsByte<EPathFollowingRequestResult::Type> Code;

	FPathFollowingRequestResult() : MoveId(FAIRequestID::InvalidRequest), Code(EPathFollowingRequestResult::Failed) {}
	operator EPathFollowingRequestResult::Type() const { return Code; }
};

It looks like there’s a function that gets called when a move is completed that you can use in AAIController:
virtual void OnMoveCompleted(FAIRequestID RequestID, const FPathFollowingResult& Result);

There’s also a delegate that gets fired by that function:
FAIMoveCompletedSignature ReceiveMoveCompleted

1 Like

Can you please provide some example for implementing it.

This should work. Haven’t tested it though. Make sure OnAIMoveCompleted is declared as a UFUNCTION.

void AMyAIController::MoveToDestination(const FVector& DestinationLocation)
{
	FAIMoveRequest MoveRequest;
	MoveRequest.SetGoalLocation(DestinationLocation);

	OnMoveCompleted.AddDynamic(this, &AMyAIController::OnAIMoveCompleted);

	FPathFollowingRequestResult MoveResult = MoveTo(MoveRequest, nullptr);

	if (MoveResult.Code == EPathFollowingRequestResult::RequestSuccessful)
	{
		UE_LOG(LogTemp, Warning, TEXT("MoveTo request successful!"));
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("MoveTo request failed"));
	}
}

void AMyAIController::OnAIMoveCompleted(FAIRequestID RequestID, EPathFollowingResult::Type Result)
{
	if (Result == EPathFollowingResult::Success)
	{
		UE_LOG(LogTemp, Warning, TEXT("Move completed successfully!"));
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Move completed, but failed"));
	}

    OnMoveCompleted.RemoveDynamic(this, &AMyAIController::OnAIMoveCompleted);
}
1 Like

can you please tell me where to call MoveToDestination.
Does the AI that owns it automatically call this function?
i tried to GetController in a AI and cast it to AIController but it failed.

No, this function is one that I made.

In the above example, AMyAIController is an extended class of AAIController, and you would put the function in the extended class.

Yes, you’re right.it will leads to a situation that i have to get AI and write AI move function in AMyAIController. it should work just fine. but it feels a little weird for me to write AI logical code in AIController . hahahahha