7

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 ?

Kas
  • 3,209
  • 3
  • 22
  • 51
  • We are missing quite a bit of code here to help you there. First: try to write variable names beginning with a lowercase please. This is mostly standard and confuses others more. Second: does `GetAllActorsOfClass` take the array by reference? – Hayt Sep 09 '16 at 07:35
  • Seriously down voting ? , I don't understand why down vote this question ? , lol – Kas Sep 09 '16 at 07:57
  • see here: http://stackoverflow.com/help/mcve – Hayt Sep 09 '16 at 07:59
  • @Hayt , I hop you've noticed this question's tags , This question is about programming with Unreal, Anyone who used to be working with unreal would understand this code and it's references. :) – Kas Sep 09 '16 at 08:01
  • @Hayt This is an unreal specific method which you won't find in plain C++ GetAllActorsOfClass , Sorry to say next time please check question's tags before commenting , answering or voting. – Kas Sep 09 '16 at 08:05
  • It still lacks information. I assume `AEnemy` is your class? How is it declared? How do you add them to the world? Anything which might make your code not-behave like it is intended to be. (if this would be all native unreal code without anything from you this would else be a bug in the unreal engine ;) ) – Hayt Sep 09 '16 at 08:12
  • @Hayt AEnemy is a class which inherit from Actor class , That's what I said Unreal Developers would know that. – Kas Sep 09 '16 at 08:13
  • 3
    Using uppercase variables is actually the standard in UE4, so the edit to the post was uncalled for in my opinion. – Bas in het Veld Sep 09 '16 at 11:54
  • @BasinhetVeld In-deed, you are correct – Kas Sep 09 '16 at 11:57

1 Answers1

10

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);
Kas
  • 3,209
  • 3
  • 22
  • 51
  • 2
    You can even skip the classToFind variable and just do: UGameplayStatics::GetAllActorsOfClass(GetWorld(), AEnemy::StaticClass(), foundEnemies); – Phillip Sep 09 '16 at 13:50