x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

"Get All Actors Of Class" in C++

Hi, I'm looking for something similar to this blueprint node ("Get All Actors Of Class") in C++. Thanks

Product Version: UE 4.8
Tags:
more ▼

asked Aug 24 '15 at 12:49 PM in C++ Programming

avatar image

jimmyJ
33 2 4 7

avatar image patrick58d Feb 28 '18 at 12:08 PM

Thanks for sharing this guys.

To get UGameplayStatics to work I needed this include

include "Runtime/Engine/Classes/Kismet/GameplayStatics.h"

(comments are locked)
10|2000 characters needed characters left

4 answers: sort voted first
more ▼

answered Aug 24 '15 at 12:51 PM

avatar image

Mhousse1247
3.4k 152 28 317

avatar image jimmyJ Aug 24 '15 at 01:30 PM

Thanks! exactly what i need

(comments are locked)
10|2000 characters needed characters left

Or you can try this:

Searching for actors of C++ classes: (Thanks gjudkins for reminding about it :)

 TArray<AActor*> FoundActors;
 UGameplayStatics::GetAllActorsOfClass(GetWorld(), YourClass::StaticClass(), FoundActors);

Searching for actors of BP classes:

 TSubclassOf<AActor> ClassToFind; // Needs to be populated somehow (e.g. by exposing to blueprints as uproperty and setting it there

 TArray<AActor*> FoundActors;
 UGameplayStatics::GetAllActorsOfClass(GetWorld(), ClassToFind, FoundActors);
more ▼

answered Aug 24 '15 at 01:38 PM

avatar image

Atheist91
1.1k 18 22 38

avatar image gjudkins Jan 08 '17 at 08:50 PM

I found that this doesn't work as-is. To get it to work you need to replace 'ClassToFind' with '{YourClassName}::StaticClass()'

Then it works beautifully. :)

avatar image Atheist91 Jan 09 '17 at 09:21 AM

Yeah, the StaticClass function should work here as well.

I didn't make it very clear in my answer, but the ClassToFind needs to be populated somehow (for example by exposing it to blueprints as property and setting it there).

If a class of actors you want to search for is a C++ class, then sure you can use that. However, if you want to narrow down the search to specific BP class, then TSubclassOf is what you need here (although I myself never had to do such thing yet).

(comments are locked)
10|2000 characters needed characters left

Here is a simple convenience function for collecting all actors of a given class into a TArray:

 #include "EngineUtils.h"
 
 template<typename T>
 void FindAllActors(UWorld* World, TArray<T*>& Out)
 {
     for (TActorIterator<T> It(World); It; ++It)
     {
         Out.Add(*It);
     }
 }

An example of finding all of the ATriggerBox instances would be:

 TArray<ATriggerBox*> Boxes;
 FindAllActors(GetWorld(), Boxes);
more ▼

answered Nov 22 '17 at 04:44 PM

avatar image

Xenojiva
21 3

avatar image ColdSteel48 Dec 24 '17 at 08:34 PM

Nice and very neat Function thank you!

(comments are locked)
10|2000 characters needed characters left

If you need to get all UObjects of a certain class (not just Actors) here is a nifty function I wrote:

 template<typename T>
 void GetObjectsOfClass(TArray<T*>& OutArray)
 {
     FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
     TArray<FAssetData> AssetData;
     AssetRegistryModule.Get().GetAssetsByClass(T::StaticClass()->GetFName(), AssetData);
     for (int i = 0; i < AssetData.Num(); i++) {
         T* Object = Cast<T>(AssetData[i].GetAsset());
         OutArray.Add(Object);
     }
 }


Use it like this: TArray AnimSequences; GetObjectsOfClass(AnimSequences);

more ▼

answered Jul 09 '18 at 12:43 PM

avatar image

dsanjit
88 5 7 11

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question