Wiki Code Tutorials

Finding out Which Streamed Level an Actor is In

Dear Community,

I’ve gone back and updated one of my earliest wikis.

Streamed Levels, Test If Actor Is In Level Bounds
https://wiki.unrealengine./Streamed_Levels,_Test_If_Actor_Is_In_Level_Bounds#Getting_the_Streaming_Level_Name

is a wiki on how to find out which streamed level an actor is contained in, during runtime!

My old code obtained the StreamingLevels array by pointer, and my new method retrieves the array by reference.

Notice how much simpler the code becomes when getting by reference!

I also use a ranged for loop to simplify the code even more :slight_smile:


**New Version**



```


//Getting the array by reference!
const TArray<ULevelStreaming*>& StreamedLevels = GetWorld()->StreamingLevels;

for(const ULevelStreaming* EachLevelStreaming : StreamedLevels)
{
	if( !EachLevelStreaming) 
	{
		continue;
	}
	
	ULevel* EachLevel =  EachLevelStreaming->GetLoadedLevel();
	
	//Is Level Valid and Visible?
	if( !EachLevel || !EachLevel->bIsVisible) 
	{
		continue;
	}
		 
	//Print Name of current Level Streaming to know which level the unit is in!
	ClientMessage( EachLevelStreaming->GetWorldAssetPackageName() );
	  
	//Is the Player Location Within Level's Bounds
	if(ALevelBounds::CalculateLevelBounds(EachLevel).IsInside(GetPawn()->GetActorLocation()))
	{
		ClientMessage("Yes Player Is Within Level");
	}
}


```


Original Code



//Get the Currently Streamed Levels
TArray<ULevelStreaming*>* StreamedLevels = &GetWorld()->StreamingLevels;
 
ULevel* EachLevel = NULL;
for(int32 v=0; v < StreamedLevels->Num(); v++)
{
	if( ! (*StreamedLevels)[v]) continue;
	//~~~~~~~~~~~~~~~~
 
	EachLevel =  (*StreamedLevels)[v]->GetLoadedLevel();
	if(!EachLevel) continue;
	//~~~~~~~~~~~~
 
	//Is Level Visible?
	if(!EachLevel->bIsVisible) continue;
	//~~~~~~~~~~~~~~~~~~~
 
	//Print Package Name For Level!
	ClientMessage( (*StreamedLevels)[v]->PackageName.ToString());
 
	//Is the Player Location Within Level's Bounds
	if(ALevelBounds::CalculateLevelBounds(EachLevel).IsInside(GetPawn()->GetActorLocation()))
	{
		ClientMessage("Yes Player Is Within Level");
	}
}



**Obtaining Streaming Level Name**

Please note I've adjusted the code to 4.8 so you can tell which streaming level the actor is in!



```


//Print Name of current Level Streaming to know which level the unit is in!
ClientMessage( EachLevelStreaming->**GetWorldAssetPackageName**() );


```



Enjoy!

:)