How to get the current level where the player is?

Hello guys!

I was looking for a way to get the current level, where the player is.

I know that the GetWorld()->GetLevels() returns a array containing all the loaded levels, but i need the one where the player spatially is.

I just want to get the reference to the ULevel* where the player is, to access its LevelBoundsActor variable and get its size.

Any help will be appreciated :slight_smile:

PS: I was messing with this, but it doesn’t work



Array<ULevel*> LevelArr = GetWorld()->GetLevels();
ALevelBounds* LevelBounds = Cast<ALevelBounds>(LevelArr[0]->LevelBoundsActor);
float Radius = LevelBounds->BoxComponent->Bounds.SphereRadius;


Try this: GetWorld()->GetName()

Thanks for your reply.

As I said, I want access to the ULevel where the player is, to get its ALevelBounds and its bounding box actor, to get the map size

I don’t want the map name, :S

Actors have a “GetLevel” method that returns which Level they are currently in.



MyPlayerActor->GetLevel();


Thanks for your reply! :slight_smile:

I have tried to use this method, but for some reason, I’m getting a crash :s

Here is the snippet:



//ULevel* CurrentLevel = GetLevel();
//ALevelBounds* LevelBounds = CurrentLevel->LevelBoundsActor.Get();
//float Radius = LevelBounds->BoxComponent->Bounds.SphereRadius;
float Radius = GetLevel()->LevelBoundsActor->BoxComponent->Bounds.SphereRadius;
UE_LOG(LogTemp, Warning, TEXT("Map name = %s"), *(GetWorld()->GetName()));
UE_LOG(LogTemp, Warning, TEXT("Map radius = %f"), Radius);


I also tried to use the first three lines, to deference the weak object pointer. I’ve got the same crash.



LoginId:1532a53a4f9b492eb2604680935773a3
EpicAccountId:01709396a0b4441297e4214a46b7974e

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x000002e0

UE4Editor_FPSGame_4473!ABlackHoleSkill::BeginPlay() [D:\GAME DEVELOPMENT\PROJECTS\UDEMY\LATEST\StealthGame_ProjectBase_4.25\StealthGame\Source\FPSGame\Private\BlackHoleSkill.cpp:74]
UE4Editor_Engine!AActor::DispatchBeginPlay() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Engine\Private\Actor.cpp:3397]
UE4Editor_Engine!AWorldSettings::NotifyBeginPlay() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Engine\Private\WorldSettings.cpp:246]
UE4Editor_Engine!AGameStateBase::HandleBeginPlay() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Engine\Private\GameStateBase.cpp:205]
UE4Editor_Engine!UWorld::BeginPlay() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Engine\Private\World.cpp:4184]
UE4Editor_Engine!UGameInstance::StartPlayInEditorGameInstance() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Engine\Private\GameInstance.cpp:478]
UE4Editor_UnrealEd!UEditorEngine::CreateInnerProcessPIEGameInstance() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\PlayLevel.cpp:2792]
UE4Editor_UnrealEd!UEditorEngine::OnLoginPIEComplete_Deferred() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\PlayLevel.cpp:1474]
UE4Editor_UnrealEd!UEditorEngine::CreateNewPlayInEditorInstance() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\PlayLevel.cpp:1708]
UE4Editor_UnrealEd!UEditorEngine::StartPlayInEditorSession() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\PlayLevel.cpp:2580]
UE4Editor_UnrealEd!UEditorEngine::StartQueuedPlaySessionRequestImpl() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\PlayLevel.cpp:1089]
UE4Editor_UnrealEd!UEditorEngine::StartQueuedPlaySessionRequest() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\PlayLevel.cpp:1016]
UE4Editor_UnrealEd!UEditorEngine::Tick() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\EditorEngine.cpp:1611]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Editor\UnrealEd\Private\UnrealEdEngine.cpp:414]
UE4Editor!FEngineLoop::Tick() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:4850]
UE4Editor!GuardedMain() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Launch\Private\Launch.cpp:169]
UE4Editor!GuardedMainWrapper() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:137]
UE4Editor!WinMain() [D:\Build\++UE4+Licensee\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:268]
UE4Editor!__scrt_common_main_seh() [d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288]
kernel32
ntdll


Any clues?

Thanks!

That’s a null ptr somewhere.




float Radius = GetLevel()->LevelBoundsActor->BoxComponent->Bounds.SphereRadius;



Either GetLevel is returning null, or LevelBoundsActor, or BoxComponent. Just add some if statements and find out.

I’ll verify it, thanks! :slight_smile:

UPDATE:

The culprit is the LevelBoundsActor. Its null.

But how can I obtain such reference from the ULevel? I need to obtain the bounding box around the current level, and I didn’t find anything except LevelBoundsActor in the ULevel documentation. :S

Since GetLevel()->LevelBoundsActor is a TWeakObjectPtr<ALevelBounds>, i used the Get to dereference it. But both GetLevel()->LevelBoundsActor and GetLevel()->LevelBoundsActor.Get() are returning null, so what am I doing wrong here? :X