Access the AI MoveTo function but with C++

Hey guys,

I’m studying AI tutorials to find out how to simply move an NPC or AI character towards a location (or other actor). So far I’ve seen lots of Blueprint tutorials that simply use the “AI MoveTo” node in order to move their characters. I’m attempting to do this in C++, but I can’t figure out what the correct syntax (headers and functions) I need to access in order to use the equivalent “MoveTo” function.

I currently have an NPC Actor with an attached script derived from the UActorComponent. Is it possible to access or “possess” the AI for this NPC character in order to use the AI MoveTo function?

What header files do I need, and what syntax/functions will set up the C++ script to access these AI functions?

Thanks for any info.

1 Like

Update:

So I made an over-arching AIController C++ script called “MyAIController2”. In my NPC’s character Blueprint, I set the “AI Controller Class” option to be using my “MyAIController2” script.

Then from a separate script on the same NPC character, I’m trying to reference the AI Controller script instance, and call the MoveToActor function from it.

Am I referencing/casting the MyAIController2 script properly from my other script? Here is what I have:


AMyAIController2 * AIInstance1 =
    Cast<AMyAIController2>(thisCharacter);

AIInstance1->MoveTowardsActor(mainCharacterActor->GetOwner());

Here is my “MyAIController2” script:


#include "MyAIController2.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Runtime/Engine/Classes/AI/Navigation/NavigationData.h"
#include "Runtime/Engine/Classes/AI/Navigation/NavigationSystem.h"

/*AMyAIController2::AMyAIController2(const FObjectInitializer& ObjectInitializer) : Super (ObjectInitializer)
{
 UNavigationSystem * NavSys = GetWorld()->GetNavigationSystem();
 Filter1 = UNavigationQueryFilter::GetQueryFilter(*NavSys->MainNavData, DefaultNavigationFilterClass);

 //TSubclassOf<AMyAIController2> Filter2 = Cast<Filter1>(UNavigationQueryFilter::GetQueryFilter(*NavSys->MainNavData, DefaultNavigationFilterClass);
}*/



void AMyAIController2::MoveTowardsActor(AActor* ActorTarget)
{
 MoveToActor(ActorTarget, 500.0f, true, true, true, DefaultNavigationFilterClass, true);
}


But everytime my main character gets within a certain distance (and triggers the NPC character to move towards him), the editor crashes. I think maybe because I’m not properly making a subclass – but I don’t know how to make a subclass to plug into the “MoveToActor” function that comes with the AI Controller class.

What is the proper way to get a Navigation Filter to plug into the MoveToActor function?

Maybe I’m close, and maybe I’m way off…I just don’t know.

1 Like

I looked into the engine code behind that blueprint node and then just implemented that code into my own methods off the controller.

The code can be found under Source\Runtime\AIModule\Private\Blueprint\AIBlueprintHelperLibrary.cpp.

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

I have not messed with the navigation filter yet, but it is shown being set in this method.

2 Likes

Oh that’s interesting. I didn’t even know you could do that. So, now I’m just wondering what is the “UObject* WorldContextObject” parameter? What should I (or could I) plug into that parameter?

All the other parameters make sense to me, and I should be able to plug into. But I don’t know what the WorldContextObject is supposed to be.

Thanks for the reply!

Forgot to ask as well, if there’s a function with a set of parameters, and I want to simply leave those parameters un-plugged, what do I put for those? Is it a “nullptr” or “NULL” or something like that? In Blueprints you simply don’t plug any wires into those parameter nodes…but what is it for C++?

Also, you wouldn’t happen to know how to implement the “Success” and “Failure” outputs from the path call would you?

Thanks again for any help.

UPDATE: It works!! Thank you so much. :slight_smile: That is so cool. I’m still curious about some of my questions in the previous post I made though (about the null parameters, and the success or failure of the path etc.)…but I got it to work overall. My enemies now walk towards me. Thank you.

How do you Implement Success and failure?