Get all items(Actors) from LevelStreaming

I can’t find a way to list all the Actors inside sublevel (LevelStreaming). For an enumeration, I try to use TActorIterator, but its constructor accepts a reference to UWorld, it does not accept a reference to ULevelStreaming, since they both inherit from UObject. I can list all the Actors from the world like this:



        UWorld* world = GetWorld();
        TActorIterator<AActor> Iterator(world, AActor::StaticClass());
        for (; Iterator; ++Iterator)
        {
            AActor* Actor = Cast<AActor>(*Iterator);
            GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Yellow, Actor->GetFName().ToString());  //Print name of the Actor
             //another code
        }
    

This works, but I only get objects from the main map (Persistent Level), and I have no idea how to get a list of all the Actors from the additional levels that I add as ULevelStreaming.



    UWorld* world = GetWorld();
    auto levels = world->GetStreamingLevels();
    for (auto level : levels) {
        ULevelStreaming* currentLevel = Cast<ULevelStreaming>(level);
        TActorIterator<AActor> It(currentLevel, AActor::StaticClass()); //Here is compiler error with TActorIterator's constructor
        for (; It; ++It)
        {
            AActor* Actor = Cast<AActor>(*It);
            GEngine->AddOnScreenDebugMessage(-1, 30.f, FColor::Yellow, Actor->GetFName().ToString());
        }
    }


Got it, i need to get all Actors from ULevel, not LevelStreaming. Like this:



      UWorld* world = GetWorld();
      auto StreamingLevels = world->GetStreamingLevels();
      for (UStreamingLevel* StreamingLevel : StreamingLevels ) {
            ULevel* level = StreamingLevel->GetLoadedLevel();
            if(!level) continue;
            for (AActor* Actor : level->Actors)
                    { // Actor action }
}


And of course, it’s need to be loaded before.

1 Like