[Solved] Get All Actors from sublevels

Need function “Get All Actors of class” in C++.

I tried to use this template:


template
void FindAllActors(UWorld* World, TArray& Out)
{
    for (TActorIterator It(World, T::StaticClass()); It; ++It)
    {
        T* Actor = Cast(*It);
        if (Actor && !Actor->IsPendingKill())
        {
            Out.Add(Actor);
        }
    }
}

It works with persistent level, but i get Error, when I tried to get actors from SubLevel :


Can't save ../Content/NewMap.umap: Graph is linked to object(s) in external map.
External Object(s):
Group3ExampleTypology_2

Try to find the chain of references to that object (may take some time)?

[UPDATE]

It worked, with this template. I just need to empty array after use it. Stupid mistake.

This is 100% a guess. You’re storing the sublevel actors in to a TArray that is in an actor or object not in the sublevel. So when the map tries to save there is a reference. But that’s just a guess.

Pretty sure all actors are in the “World” regardless of which sub-level they loaded in on.

You can query the name of the sub-level the an actor belongs to though:

BTW, you can hide or show all actors in an entire sub-level this way. We use this to show/hide a developer-only Point Cloud level:



// iterate through streaming levels
// show or hide the point cloud level based on current visibility
const TArray<ULevelStreaming*>& streamedLevels = GetWorld()->GetStreamingLevels();
for (ULevelStreaming* streamingLevel : streamedLevels)
{
   FString levelName = streamingLevel->GetWorldAssetPackageName();
   if (levelName.ToLower().Contains("pointcloud"))
   {
      if (streamingLevel->IsLevelVisible())
      {
          LOG("Hide point cloud!");
          streamingLevel->SetShouldBeVisible(false);
      }
      else
      {
          LOG("Show point cloud!");
          streamingLevel->SetShouldBeVisible(true);
      }
      return;
   }
}


Might be useful for you.

Just because it took me some time to find:
this has been added to the engine it seems

#include “Kismet/GameplayStatics.h”
UGameplayStatics::GetAllActorsOfClass

A note: This won’t work (4.24.3) if the level contains a LandscapeStreamingProxy. When making the level visible for the second time, it will crash. Clearly a bug, so don’t use this method for open world setups

Be helpful !