Kas
Kas

Reputation: 3923

Why GetAllActorsOfClass returns empty?

I have a PlayerControl.cpp class which derives from Pawn class

In that class , I have a method to get all Actors in Map

TSubclassOf<AEnemy> ClassToFind;
 TArray<AActor*> FoundEnemies;
 UGameplayStatics::GetAllActorsOfClass(GetWorld(), ClassToFind, FoundEnemies);

But FoundEnemies array is always empty , When I do the same thing in BP it works.

Can someone tell me why is this not working in C++ ? Or If I am doing wrong , How to do it correct ?

Upvotes: 13

Views: 18041

Answers (1)

Kas
Kas

Reputation: 3923

Finally , I found answer for my own question

I should assign a value to the variable "ClassToFind" So adding line classToFind = AEnemy::StaticClass(); fixed the issue

TSubclassOf<AEnemy> classToFind;
    classToFind = AEnemy::StaticClass();
    TArray<AActor*> foundEnemies;
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), classToFind, foundEnemies);

Upvotes: 21

Related Questions