I’ve just started converting my BPs to C++ today and I know close to nothing how Unreal’s C++ works. Right now I’m converting this piece of BP:
I’ve been trying to figure how to get AI controller for an hour now. Documentation and google can’t halp me either
It depends. “Get AIController” is a bit of a special function as in it’s a global utility function, rather than being part of a specific class.
This “utility” function takes an “Actor” as input and adapts based on what you throw at it. If you give it a pawn, it’ll find the controller of the pawn. If you give it a controller, it’ll test the controller itself. That’s all it does, really. This utility function is located in the AIBlueprintHelperLibrary, and you can call it as is from c++.
class AMyActor : public AActor
virtual void NotifyActorBeginOverlap(AActor* OtherActor) override;
void AMyActor::NotifyActorBeginOverlap(AActor* OtherActor)
for (AActor* Enemy : Enemies)
AAIController* C = UAIBlueprintHelperLibrary::GetAIController(Enemy);
if (C && C->GetAIPerceptionComponent())
If your array of “Enemies” contain pawns, the more “standard” approach would be to use Pawn → GetController → Cast to AIController.
In c++ this would translate to
for (APawn* Enemy : Enemies)
AAIController* C = Enemy->GetController<AAIController>();
Assuming your enemies are pawns (or characters, which anyway inherit from pawns) you use GetController, and cast its result to a generic AIController or to your specific AIController. Supposing it’s the former (in this example we’re just enabling their hearing sense):
// your array it's something like TArray<APawn*> myEnemiesArray
for (auto& Enemy : myEnemiesArray)
AController* myEnemyController = Enemy->GetController();
myAIEnemyController = Cast<AAIController>(myEnemyController);
//whatever other logic you need
Hope that helps!