Download

Can't get AIMoveTo to work

So I’m very new to C++ programming. I’ve taken about 90 lectures from the Unreal C++ udemy course and I’m trying to do a few things on my own. I know how to use blueprints, however I’m sure I’m doing something very wrong but I’m not sure what. Wondering if I could get some help.

So I created a c++ class for my character, and I have a navigation volume set up. I asked someone where I could find documentation on an AIMoveTo function, and he pointed me here: https://github.com/EpicGames/UnrealEngine/blob/76085d1106078d8988e4404391428252ba1eb9a7/Engine/Source/Runtime/AIModule/Private/Tasks/AITask_MoveTo.cpp

So I wrote this code in the beginplay function of my character class:


AActor* Self = GetOwner();

AAIController* MyController = GetController();

UAITask_MoveTo::AIMoveTo(MyController, FVector(0.f, 0.f, 0.f), Self, 5.f, EAIOptionFlag::Default, EAIOptionFlag::Default, true, false);

When I try converting my controller to AAIController, it tells me “A value of type “AController” cannot be used to initialize an entity of type “AAIController”.” I’m trying to put my character’s controller reference as the first argument to the AIMoveTo function, but it’s not working. I also included all the header files listed at the top of the function.

Obviously I need a reference to an AAIController. How do I do that? Am I using the wrong function? Am I doing everything wrong? :rolleyes: Thanks for any help.

Hi,

You need to cast your AController returned by GetController() to AAIController like this:



#include "AIController.h"

AAIController* MyAIController = Cast<AAIController>(GetController());

if(MyAIController)
{
   //Do stuff.
}


Because GetController() could also return a PlayerController if you have that assigned :slight_smile:

You could also skip declaring “Self” by just simply using “this” as the parameter.

After several hours of trying and not knowing what I was doing, I think I was trying to use a function related to the behavior tree MoveTo task. So I did some googling and am now using MyAIController->MoveToLocation. Finally worked, thanks for the help!