There is something I don’t understand with PlayerController and AIPlayerController with C++.
So here’s my situation:
I want to create a TopDown mouse driven movement multiplayer online. I did implement movement, using the CharacterMovement class. When a Player clicks, I compute the clicked position in 3D with a mouse to screen ray : PlayerController::GetHitResultUnderCursorByChannel(…) and manually add the direction vector to the AddMovementInput(FVector) method. It works fine.
Now, I’ve seen some examples of people using AAIController::MoveToLocation method. I think this is exactly what I need and it must be a better implementation than mine. But whenever I try to Cast my Controller member, it returns nullptr.
I’ve seen quite a few posts about C++ integration of AAIController. Here is my Pawn config after seeing these posts:
In my character cpp, I do something like this (client side now, but tried server side):
PlayerController->GetHitResultUnderCursorByChannel(TraceTypeQuery1, false, HitResult);
if (Controller != nullptr)
{
AAIController* AIController = Cast<AAIController>(Controller);
if (AIController != nullptr)
{
AIController->MoveToLocation(TargetPosition);
}
else
{
UE_LOG(LogTemp, Warning, TEXT("Cast to AIController failed"));
}
}
As far as I understand, the Controller is either a APlayerController or a AIController but I need both. I don’t really know how to combine both and could not find enough informations.
Has anyone any idea on how to achieve this correctly ?
I am a beginner so I am not sure my answer is corect.
My guess is the AIController is for AI enemies, and APlayerController is for the controlled player.
Do you want to call AIController 's blueprint function in your c++ code and use it for your player movement stratage?
I don’t think you need an AI controller to move a pawn, you can use UAIBlueprintHelperLibrary::SimpleMoveToLocation to move a player controlled character
In fact, I looked into it a bit and this is how the TopDown template project does it, so you can take a look there - simply create a TopDown C++ project from the launcher and see how the movement is handled in the player controller class
you might be right but i wouldnt recommend it, SimpleMoveTo has no callbacks so if you want to do any logic when it reaches the target you cant, unless you run a separate test on tick which is awkward
Everything is exposed in C++, OnRequestFinished can give you the delegate that you want, and you can even create your own subclass of UPathFollowingComponent and give it to your player controller, since SimpleMoveTo functions first try to find a component on the controller, and if they don’t find one they create one and add it
You can then override all the relevant functions, such as OnPathFinished, OnSegmentFinished, AbortMove and anything else. Should be a pretty straight forward and quick task to get all the functionality that’s needed
Everything is doable when using C++, and technically you could even copy the entire AI Move to Task code and rewrite it to support non AI controlled characters, so you can get all the blueprint callbacks as well