How do I use PrepareMapChange/CommitMapChange?
I am currently loading a level with UGameplayStatics::OpenLevel(GetWorld(), mapToLoad, 1, "");
however, this unloads my whole game and reloads it again. I need the game to simply keep my current data but move into a new map/location. I have had a look around and the PrepareMapChange states that "... GameModes where the game state should be preserved through a transition." which seems to be what I need.
However, I cannot seem to get this working as I get an error ("LogEngine:Warning: PREPAREMAPCHANGE: Couldn't find package for level 'Room01'") when trying to give the array my map names: Room01, Room02.. with each of them as a map file in the content/maps. I feel I am missing something so can anyone give me any pointers of how I should be using this correctly?
I know this is old, but I'll answer for any future people who have the same question.
Call GetWorld()->PrepareMapChange and give it a list of maps (FName list) starting with the main (persistent level) first, followed by any sublevels that need to be loaded at the spawn point. Example:
The FNames must be in the form:
Note that the extension is not being used, and we use "/Game" rather than "/Content". If you have a filepath, you can covert it to the needed format, called a "Long Package Name", by calling:
If you neglect to add a needed sublevel to the list sent to PrepareMapChange, the call to CommitMapChange will block for however long it takes to load and set up the neglected level.
To check to see if the maps are done preloading use:
That second part is important because IsMapChangeReady tells you if the levels sent in the list to PrepareMapChange are ready, but not if any sublevels or assets those maps reference are ready.
When done call:
It's important NOT to call the GEngine version of the same file. The UWorld version makes sure the call is done the right way.
When CommitMapChange is done, there will be a callback to GameMode::PostCommitMapChange(). Teleport the player and do any setup necessary for the new level there. Note that TeleportTo will not change the orientation of the Player, so you need to set that manually by calling
How long the switchover takes after calling CommitMapChange depends on how long the garbage collection takes when clearing the old level. If you're exiting a small level, then the transition is very small. On my tests with large levels, the switch over took about a second and a half. Usually a quick fade-out, CommitMapChange, fade-in can easily hide the transition.
This method will NOT work in "Play In Editor" mode (PIE). You must run it in Standalone mode to see it.
Hope this helps.
EDIT: To make this pipeline work in PIE mode:
1) In EditorEngine.cpp (Engine\Source\Editor\UnrealEd\Private):
Look for the first inner loop that contains the line:
FWorldContext &PieContext = *ContextIt;
Near the end of that loop add:
2) In UnrealEngine.cpp (Engine\Source\Runtime\Engine\Private):
Follow this question
Once you sign in you will be able to subscribe for any updates here