# Any way to get hierarchy of Levels/Sublevels from code/blueprints?

**URL:** <https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659>\
**Category:** C++\
**Tags:** hierarchy, Level-Streaming, unreal-engine, level, question, UE4-27\
**Created:** [February 21, 2022, 4:48pm UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659 "2022-02-21T16:48:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kot\_153](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kot\_153](https://forums.unrealengine.com/u/Kot_153)\
**Post date:** [February 21, 2022, 4:48pm UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659/1 "2022-02-21T16:48:58Z")

</div>

In my level I have several folders with sublevels like:  
Persistent level  
\_Place1  
\_\_\_Floor1  
\_\_\_Floor2  
\_Place2  
\_\_\_Floor1

And for these sublevels I want to get full logical path like “Place1/Floor2”, but so far all I found is either how to get logical name (Floor1) or actual path (/Game/Maps/Place1\_Floor2).

So is there any way of getting that subfolders hierarchy of Level?

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [February 22, 2022, 5:26am UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659/2 "2022-02-22T05:26:39Z")

</div>

> [@Kot\_153](#):
>
> or actual path (/Game/Maps/Place1\_Floor2).

I would create a chain of GetOuter()'s until you hit the PersistentLevel, using each ULevel to get further out (GetOuter Of ULevel)

Here’s the relevant code from my RamaSaveSystem Plugin (paid plugin on marketplace) to get you started!

**You can pass in an actor that is inside your innermost level** to initiate the chain, going outward, then switch to using each ULevel’s GetOuter and listing each package name along the way!

```auto
FString URamaSaveComponent::GetActorStreamingLevelPackageName()
{
	AActor* Owner = GetOwner();
	if(!Owner) return "No Owning Actor";
	  
	ULevel* Level = Owner->GetLevel();
	if(!Level) 
	{
		VSCREENMSG("Rama Save System, Level PTR BAD");
		return "No level for Actor apparently! (should not ever see this unless actor pending delete perhaps)";
	}
	
	//Consistency if it is the persistent level
	//! Causes crash if called right as a level is being hidden
	if(Level->IsPersistentLevel())
	{
		//if(WITH_EDITOR) RS_LOG2(RamaSave,"Saved actor in", "PersistentLevel");
		 
		return "PersistentLevel";
	}
	
	//Rama Victory!!!
	UObject* Package = Level->GetOuter(); 
	if(!Package) 
	{ 
		return "PersistentLevel";
	}
	 
	//if(WITH_EDITOR) RS_LOG2(RamaSave,"Saved actor in", *Package->GetName());
	 
	return Package->GetName();
}

```

so my psuedo code idea:

1. Obtain an actor inside the innermost level
2. Use my code above to Actor-\>GetLevel()
3. Level-\>GetOuter() = Result
4. Result-\>GetOuter()-\>GetName (Display it), Result-\>GetOuter() (recursion)
5. until Level-\>IsPersistentLevel()

💖

Rama

---

<div class="post-metadata">

**Author:** ![Kot\_153](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kot\_153](https://forums.unrealengine.com/u/Kot_153)\
**Post date:** [February 22, 2022, 9:28am UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659/3 "2022-02-22T09:28:32Z")

</div>

Hi! Thanks for the reply, but that’s not what I’m looking for.

Let’s say I have **HierarchyTest** level with this hierarchy:  
 ![2022-02-22_11-25-39_UE4Editor_22](https://d3kjluh73b9h9o.cloudfront.net/original/4X/d/8/9/d8948d927ba143c8484b5e229a9963cfcf0ee8fc.png)

So for any actor from **HierarchyTest\_Sublevel1** I want to get path like

**HierarchyTest/SubFolder1/HierarchyTest\_Sublevel1**

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [February 22, 2022, 4:24pm UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659/4 "2022-02-22T16:24:15Z")

</div>

> [@Kot\_153](#):
>
> subfolders hierarchy

ooooh, so you’re saying you want the subfolders which you are creating in the Levels window?

very interesting! I wonder where that info is stored, if the actual levels have any association with that.

You might succeed at getting that info in an editor build, but I am not confident, yet, that it would ship with a packaged game.

If you really want this information in-game, I suggest you extend the Level Blueprint, create a custom variable, and track the folder that “Self” (each level) is in Yourself.

something like this:

```auto
/*
	By Rama
*/
 
#pragma once
       
#include "Engine/LevelScriptActor.h"
#include "JoyLevel.generated.h"
  
UCLASS()
class AJoyLevel : public ALevelScriptActor
{
	GENERATED_BODY()
public:
	     
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Joy")
	FString JoyfulFileName = "";
	   
	/** Set this to true when you want to use the copy from umap to data asset option */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Joy")
	bool UseUMapWorldEnvSettings = false;
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Joy")
	FString WorldEnv;
	 
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Joy")
	float LevelSettings_MagneticSparklesBrightnessMult = 1;
	
public:
	UFUNCTION(BlueprintImplementableEvent, BlueprintCallable, Category="Joy Level")
	void SetNewSkyActive(bool NewSky);
	 
};

```

1. Open level blueprint of each sublevel
2. File → reparent → your custom class
3. Edit Class Defaults toolbar button (along top)
4. Enter the folder name for the sublevel

That will give you maximum control over this process you are doing, and give you Additional Benefits, because now you can store ANY INFO YOU WANT, per sublevel in your Levels window, and get that info ingame

✨✨✨ annnnd ✨✨✨

get that info in a packaged game with ease!

❤

Rama

---

<div class="post-metadata">

**Author:** ![Kot\_153](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kot\_153](https://forums.unrealengine.com/u/Kot_153)\
**Post date:** [February 23, 2022, 8:18am UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659/5 "2022-02-23T08:18:43Z")

</div>

Some interesting ideas there, thanks for the suggestions!

But actually I don’t care about packaged game, I just wanted to use this to clean up World Outliner while working in editor (put each actor to subfolder with name of the sublevel that actor is stored in).

So following the example from previous message result should look like this.

 ![2022-02-22_18-35-29_UE4Editor_22](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/f/7/7f75bec01fe9eb1cd89832e00b6c0b7dd95da169.png)

And this is actually a result of a working script.  
So in case anyone stumbles upon this thread in future - here’s how I did it.

Added **WorldBrowser** to PrivateDependencyModuleNames in my MyProjectEditor.Build.cs

```auto
PrivateDependencyModuleNames.AddRange(new string[] { "WorldBrowser" });

```

Then in one of BPL classes

```auto
// Necessary includes
#include "WorldBrowserModule.h"
#include "Engine/WorldComposition.h"
#include "WorldBrowser/Private/LevelCollectionModel.h"

...

// Getting model which contains list of all SubLevels
FWorldBrowserModule& WorldBrowserModule = FModuleManager::GetModuleChecked<FWorldBrowserModule>("WorldBrowser");
TSharedPtr<FLevelCollectionModel> SharedWorldModel = WorldBrowserModule.SharedWorldModel(WorldContextObject->GetWorld());

...

// Now iterating by all SubLevels
for (TSharedPtr<FLevelModel> Child : SharedWorldModel->GetAllLevels())
{
	if (Child.Get()->GetDisplayName() == "Persistent Level")
	{
        // Will return the name of the level - HierarchyTest from the example above
	    FString LevelName = WorldContextObject->GetWorld()->GetMapName();
	}
	else
	{
        // Will return HierarchyTest_Sublevel1 from the example above
		FString SubLevelName = Child.Get()->GetDisplayName();
        // Will return SubFolder1 from the example above
		FName SubLevelPath = Child.Get()->GetFolderPath();
	}
}
	

```

Now this is quite hacky way since it uses Private stuff of WorldBrowser, but it works well.

Thanks to these topics:

> [@Loading Umaps into LevelEditor](https://forums.unrealengine.com/t/loading-umaps-into-leveleditor/419947):
>
> Im trying to add a widget that will load specific levels into the level editor when working on a World Composition Object. The WorldBrowserModule Api exposes a function virtual TSharedPtr\<class FLevelCollectionModel\> SharedWorldModel(UWorld\* InWorld); Which seems to be what I need. However FLevelCollectionModel, is not exported. So nobody can use this APi. Are you planing to expose this? Or provide an api that can be used?

> [@How can I load/unload a sublevel(tile) in a world composition?](https://forums.unrealengine.com/t/how-can-i-load-unload-a-sublevel-tile-in-a-world-composition/443698/4):
>
> UGameplayStatics class is meant for runtime code. If you’d like to manage levels in the editor, you need EditorLevelUtils. In my implementation, I keep an array of level names in the form of presets, so the user can easily switch between level presets. Then I retrieve ULevelStreaming\* from the world (method in custom World Settings) and pass it to EditorLevelUtils methods. This is ready snipper for you slight_smile[https://gist.github.com/DoctorErgot/c98aa7fcb7ef7548338e24d950b8667c](https://gist.github.com/DoctorErgot/c98aa7fcb7ef7548338e24d950b8667c)

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [February 24, 2022, 12:25am UTC](https://forums.unrealengine.com/t/any-way-to-get-hierarchy-of-levels-sublevels-from-code-blueprints/500659/6 "2022-02-24T00:25:00Z")

</div>

Wow great solution! Very innovative!

Thanks for sharing!

If anyone ever did care about ✨ packaged ✨, they could use a Blueprint script (formerly blutility) with an editor trigger to do what you did, iterate through, and automatically assign to level script actor

Then, packaged game would be updated in 1 click no matter how the level evolves!

❤

Rama
