# Create a new Level from C++ code

**URL:** <https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703>\
**Category:** World Creation\
**Tags:** unreal-engine, level, question, CPP\
**Created:** [June 22, 2016, 4:03pm UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703 "2016-06-22T16:03:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LaP0573](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LaP0573](https://forums.unrealengine.com/u/LaP0573)\
**Post date:** [June 22, 2016, 4:03pm UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/1 "2016-06-22T16:03:52Z")

</div>

Greetings,

Is it possible to create a new Level (so a new .umap file) directly from C++ code, for example from a plugin’s code ? I would like to automate the level creation (not having to go through the editor =\> new Level menu) then populate it directly from code, without having to interact with the editor whatsoever.

How can I do that ?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_67d829db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_67d829db](https://forums.unrealengine.com/u/anonymous_user_67d829db)\
**Post date:** [June 22, 2016, 9:08pm UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/2 "2016-06-22T21:08:11Z")

</div>

Yes! I had to do this as part of my plugin (although I create a new UWorld not a ULevel).  
The way I did it was by creating a template world in my plugin’s Content folder (with basic things like a Light Source and Sky Sphere) and then copy that for each new level. Here’s the rough code I used:

```
auto WorldTemplateObj = StaticLoadObject(UWorld::StaticClass(), NULL, TEXT("World'/MyPlugin/LevelTemplate.LevelTemplate'"));
ObjectTools::FPackageGroupName PGN;
pgn.ObjectName = MyNewLevelName;
pgn.PackageName = MyNewPackageName;
TSet<UPackage*> ObjectsUserRefusedToFullyLoad;

auto World = CastChecked<UWorld>(ObjectTools::DuplicateSingleObject(WorldTemplateObj, PGN, ObjectsUserRefusedToFullyLoad));

```

Alternately, you can instead use `UWorld::CreateNewWorld`, which will give you a truly clean world.

---

<div class="post-metadata">

**Author:** ![LaP0573](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LaP0573](https://forums.unrealengine.com/u/LaP0573)\
**Post date:** [June 27, 2016, 10:55am UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/3 "2016-06-27T10:55:17Z")

</div>

Thank you, worked perfectly !  
Any idea how I could go about opening the level in the editor via the C++ code though ? The level properly appears in the content browser (as unsaved though) but my editor still has the previous level opened. Do you know how I could open the new one that I just created without having to double click on it in the assets browser ?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_67d829db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_67d829db](https://forums.unrealengine.com/u/anonymous_user_67d829db)\
**Post date:** [June 27, 2016, 7:35pm UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/4 "2016-06-27T19:35:50Z")

</div>

I too am unclear on how to open a World without user interaction. It looks like you might have to use `FEditorFileUtils::LoadMap`, but the code within looks very thorny.

---

<div class="post-metadata">

**Author:** ![LaP0573](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LaP0573](https://forums.unrealengine.com/u/LaP0573)\
**Post date:** [June 29, 2016, 1:15pm UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/5 "2016-06-29T13:15:54Z")

</div>

That looked promising, but I tried this :

```
FEditorFileUtils::LoadMap(TEXT("World'/Game/Scenes/Shot_42.Shot_42'"));

```

And it’s not working. I suspect it’s because the Shot\_42 map (that I created thanks to your technique !) hasn’t been savec/serialized to a file yet. So, I looked further and found this :

```
FEditorFileUtils::SaveMap(World, TEXT("World'/Game/Scenes/Shot_42.Shot_42'"));

```

However, I’m getting a compilation error :

```
Module.ImportIntoLevel.cpp.obj : error LNK2019: unresolved external symbol "public: static bool __cdecl FEditorFileUtils::SaveMap(class UWorld *,class FString const &)" (?SaveMap@FEditorFileUtils@@SA_NPEAVUWorld@@AEBVFString@@@Z) referenced in function "public: void__cdecl FImportIntoLevelModule::PluginButtonClicked(void)" (?PluginButtonClicked@FImportIntoLevelModule@@QEAAXXZ)
1>D:\Unreal Projects\PythonPluginTest\Plugins\ImportIntoLevel\Binaries\Win64\UE4Editor-ImportIntoLevel.dll : fatal error LNK1120: 1 unresolved externals

```

I don’t understand why I’m getting this error while `FEditorFileUtils::LoadMap` works perfectly. Any idea ? And thanks for your help 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_67d829db](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_67d829db](https://forums.unrealengine.com/u/anonymous_user_67d829db)\
**Post date:** [June 29, 2016, 9:28pm UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/6 "2016-06-29T21:28:15Z")

</div>

That’s a linker error, so _usually_, it would mean you forgot a module in your build file but it doesn’t here. In this case, looking at `FileHelpers.h`, it looks like SaveMap is missing its [API Specifier](https://docs.unrealengine.com/latest/INT/Programming/Modules/API/) `UNREALED_API`. Compare the following declarations:

```
static UNREALED_API void LoadMap(const FString& Filename, bool LoadAsTemplate = false, const bool bShowProgress=true);

static bool SaveMap(UWorld* World, const FString& Filename );

```

I think this is a mistake by the Unreal dev team. A possible messy workaround would be to copy the implementation from `FileHelpers.cpp` into your own file but this may be infeasible, depending on how many private properties it uses.

An easier route (if it works, I don’t know) would be to save the Persistent Level, `FEditorFileUtils::SaveLevel(World->PersistentLevel,FileName)`, which should create a

---

<div class="post-metadata">

**Author:** ![LaP0573](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LaP0573](https://forums.unrealengine.com/u/LaP0573)\
**Post date:** [July 4, 2016, 9:20am UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/7 "2016-07-04T09:20:22Z")

</div>

Sorry for the late answer ! Perfect, I used your easier route and it worked just fine. I called the method without a FileName (because it would crash, for some reason, or not save the map at the right place) and it saved it with the right name where I wanted, which is great. I could then LoadMap and it worked !

Thank you again 🙂

---

<div class="post-metadata">

**Author:** ![AlexQuevillon](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/alexquevillon/32/751975_2.png) [@AlexQuevillon](https://forums.unrealengine.com/u/AlexQuevillon)\
**Post date:** [October 19, 2023, 10:57am UTC](https://forums.unrealengine.com/t/create-a-new-level-from-c-code/361703/8 "2023-10-19T10:57:38Z")

</div>

[![](https://d3kjluh73b9h9o.cloudfront.net/original/4X/4/c/d/4cdd3bfd4c2b02bd7843de80a76c38b2ecfa2343.jpeg "UE5 C++ 9 - How To Create Assets with C++? - Unreal Tutorial World Sequence Sound Enum Struct") ](https://www.youtube.com/watch?v=oFp3MKu4MoY&t=1s)
