Just to wrap this up and for anyone else that finds this from search:
The level instance will need to finish loading before it can be queried. Here’s a quick way to do this in BPs:
Additionally here is a modified reusable version of the function with more null checking to prevent possible crashes:
void ULevelUtilities::GetAllActorsOfClassFromStreamLevel(const ULevelStreaming* LevelStreaming,
TArray<AActor*>& OutActors, UClass* Class)
{
// Clear the output array
OutActors.Empty();
// Validate LevelStreaming input
if (!LevelStreaming)
{
return;
}
// Get the loaded level
ULevel* LoadedLevel = LevelStreaming->GetLoadedLevel();
if (!LoadedLevel)
{
return;
}
// Validate the class input
if (!Class)
{
return;
}
// Filter actors by the specified class
for (AActor* Actor : LoadedLevel->Actors)
{
if (Actor && Actor->IsA(Class))
{
OutActors.Add(Actor);
}
}
}