World Partition / Level streaming - Is this the best way to check what levels are in a given location?

Hello!

When using World Partition, I understand that an actor is automatically assigned to a Level (to a Cell) depending on its position, but not in Runtime.

If I spawn an actor while playing (for example dropping an item), it will be spawned in the persistent level and therefore it won’t be removed when the cell in that position is unloaded.

When spawning, I could easily assign the Level in the FActorSpawnParameters, like this:

	ActorSpawnParameters.OverrideLevel = Level;

If the item is spawned by another actor which is already in the desired Level, it’s easy to do, because I just use GetLevel() to get the proper level.

However, if the player is executing the action of spawning the item, I can’t do it in the same way because the player is in the PersistentLevel.

So I found a way to achieve this, but I’m not sure if that’s the proper way in terms of performance, or if there is a better way to do so.
This is how I’m currently doing it:

ULevel* UWorldPartitionSaveSubsystem::GetLevel(FVector Location, float Radius)
{
	TArray<const UWorldPartitionRuntimeCell*> Cells;
	FWorldPartitionStreamingQuerySource WorldPartitionStreamingQuerySource;
	WorldPartitionStreamingQuerySource.Location = Location;
	WorldPartitionStreamingQuerySource.Radius = Radius;
	WorldPartitionStreamingQuerySource.bUseGridLoadingRange = false;
	GetWorld()->GetWorldPartition()->RuntimeHash->ForEachStreamingCellsQuery(WorldPartitionStreamingQuerySource, [&Cells](const UWorldPartitionRuntimeCell* Cell)
	{
		Cells.Add(Cell);
		return true;
	});
	return Cells.Num() > 0 ? Cells[0]->GetLevel() : nullptr;
}

With this method I can access to the level in a given location and it works, but it doesn’t seem to be the best idea, because I’m basically iterating through all cells.