skip to Main Content

Handling level streaming through C++

In this post we’re going to implement a project that uses level streaming. Then, using a custom console command, we will be able to load and change the position of the desired sub levels.

For this tutorial I’m using the 4.12 version of UE 4 Editor.

Creating the project

Create a blank C++ project and right away, save the default map with a name of BaseMap. Then, add two new maps in the same folder as the BaseMap, named MapOne and MapTwo and place some objects in each one. The reason we need to save all the maps we want to stream, in the same folder, is due to the implementation of the engine. Maps which are located in different folders cannot be streamed.

Here is a screenshot of each map in my case:

map_one
Click on image to enlarge in a new tab
map_two
Click on image to enlarge in a new tab

Save and close your maps. Then, open up the BaseMap and in the World Settings Menu Activate the Enable World Composition Option. The following message will pop up:

world_comp_msg

Click the OK button. Then, from your editor open up the Levels panel (Window -> Levels). Select both of your maps and by right clicking load then in your base map. This is my result:

Click on image to enlarge in a new tab
Click on image to enlarge in a new tab

Then, we need to create a Layer in which our maps will reside. We need a new layer so our sub levels won’t be loaded by default. To add a new layer, click on the marked icon to summon the World Composition and:

  • Click on the “+” Icon to create a new layer
  • Name your layer (in my case I named it “MyLayer”)
  • Disable the streaming distance
  • Click the create button

add_layer_1

When you’re done with that, select both of your maps from the Levels panel, right click on them and assign them to MyLayer:

As I mentioned in the start of the post, we’re going to use a custom command through the console to move our maps during game time. To achieve that, we’re going to modify the GameMode C++ class that comes with the blank c++ template. More information of how to create custom console commands can be found here.

Moreover, in order to move the sub levels, we’re going to need a location. Instead of typing in locations, I will create a new c++ actor class and place some instances on the BaseMap level. Then, I will store all the placed actors of the BaseMap level in an array. As a result, I will simply type one number (which will be the index of actor in the array) and my sub level will get spawned in the location of that Actor.

If that sounds confusing, don’t worry! Everything will get clear once we type in our code. However, before we do that, add a new C++ Actor class named LevelMovePoint.

Editing our Game Mode C++ class

In the header file of your game mode, type in the following code:

In the source code, include the LevelMovePoint header file and then type in the following code:

Save and compile your code.

Before moving any further, open up the BaseMap and make sure that its your persistent map and the other maps are unloaded. To make sure that your map is the persistent one check the lower right corner of your viewport:

persistent_level

To make a map Persistent, from the Levels Panel right click on it and select the option “Make Current”.

When you’re done with that, create a Blueprint based on your LevelMovePoint actor and place some instances on your BaseMap. Moreover, override the game mode of the BaseMap to our C++ GameMode class:

Click on image to enlarge in a new tab
Click on image to enlarge in a new tab

To test your code, click on the play button and open up the console window (using the ~ key). Then, use the following command:

movemap “mapone” 2

The parameter in the “” is the string name of the map and the second one is the index of the transforms array. Note that if the given string doesn’t match any of yourr maps, the game will crash.

Avatar photo

This Post Has One Comment

  1. Hey Orfeas,
    this is a great tutorial. And I could reproduce the code easily with some minor tweaks. However I have a question: Is it possible to load the same Map 2 times (with 2 different MoveActors)?
    When I call the command MoveMap map1 0
    and then use the command MoveMap map1 1 then the engine seems to reload map1 on the position of the actor who is stored at index 0. Any Ideas how to solf this problem?

    Kind regards,
    Lennart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top