Legacy/Streamed Levels, Test If Actor Is In Level Bounds

From UE4: Community Wiki
Jump to: navigation, search

Overview

Author: Rama (talk)

Dear Community,

Here's how you can determine if an actor is within the bounds of a streamed in level!


//in playercontroller class

//Get the Currently Streamed Levels
const TArray<ULevelStreaming*>&amp; StreamedLevels = GetWorld()->StreamingLevels;

for(const ULevelStreaming* EachLevelStreaming : StreamedLevels)
{
	if( !EachLevelStreaming) 
	{
		continue;
	}
	
	ULevel* EachLevel =  EachLevelStreaming->GetLoadedLevel();
	
	//Is This 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 this Level's Bounds
	if(ALevelBounds::CalculateLevelBounds(EachLevel).IsInside(GetPawn()->GetActorLocation()))
	{
		ClientMessage("Yes Player Is Within This Level");
	}
}

Calculate Level Bounds

The most notable function here is:

FBox ALevelBounds::CalculateLevelBounds

IsInside

CalculateLevelBounds returns an FBox,

IsInside is an FBox function that takes in an FVector and sees if the FVector is within that box's bounds

ALevelBounds::CalculateLevelBounds(EachLevel).IsInside(GetPawn()->GetActorLocation())

Getting the Streaming Level Name

Please note as of 4.8 the way to obtain the name of the streaming level is:

EachLevelStreaming->GetWorldAssetPackageName()

Summary

Enjoy!

Rama (talk)