The UVK::Level class

The UVK::Level class comprises 2 distinct code regions

  1. The 3 regular gameplay class events
  2. The level file handling.

Events

The level contains a pointer to a Game Mode class, which contains all other gameplay class types except the level itself, the GameInstance and the Scriptable Objects. More about the Game Mode class and the Level's relationship with it will be explained in the Game Mode chapter.

Level files

Opening and closing files

The level class also handles saving and loading level files.


To open a new level, call the

template<typename T>
static void open(String location)

function. As a template parameter put the type of the level you want to open, as your function parameter put the location to your level starting from the Content/ folder (don't list an extension)


To save a level call the

static void save(String location);

function with the location starting from the Content/ folder (don't list an extension)

Other functions

The level class offers so called get functions to make navigating the class hierarchy easier and less verbose.

static PlayerController* getPlayerController(Level* lvl);
static Pawn* getPawn(Level* lvl);
static GameState* getGameState(Level* lvl);
static PlayerState* getPlayerState(Level* lvl);

To change the ambient colour and intensity, call the

static FVector4& getAmbientLighting()

function. The return value is a 4 argument vector with the x, y and z values corresponding to r, g and b. The fourth value(w) is the intensity of the lighting.


To change the background colour, call the

static FVector4& getSceneColour();

function. The return value is a reference to a 4 value vector with x, y, z and w corresponding to r, g, b and a


To change the name(not to be confused with the filename) of the level, call the

static std::string& getLevelName();

function. It returns a reference to a standard string containing the name.

Casting

You can use the cast function to convert a generic Level pointer into its original type, like this

auto* originalLevel = Level::cast<YourOriginalLevelType>(aPointerToAGenericLevel);

Additional information for engine developers

The function for opening levels is kind of complicated, so here comes an explanation:

When a user calls the

template<typename T>
static void open(String location)
{
    global.openFunction = [location]()
    {
        global.currentLevel->endPlay();
        delete global.currentLevel;
        T* lvl = new T();
        global.currentLevel = lvl;
        T::openInternal(location);
        global.currentLevel->beginPlay();
    };
}

function, a global variable of type std::function, member of the UVK::UVKGlobal class, is assigned to a lambda. The following lambda only needs to capture the location variable, since that is the only thing the function needs. After it is assigned, the function will be called next frame.

When the lambda is executed 6 things will happen

  1. The end event for the current level will be called
  2. The heap memory for the current level will be freed
  3. A new level will be constructed and allocated to the heap from the type provided
  4. The current level will be set to the newly created level
  5. The internal function for opening a level file will be called, which will load all settings and actors from the provided level file into memory
  6. Finally, the begin function for the new level will be called

Now we need to move to a different file. GLPipeline.cpp L105 calls a function from the UVK::UVKGlobal class called global.finalizeOpening();. This function is called every frame if the editor isn't in use. What it does is, it calls the function pointer that was assigned in the original open function, and then resets the function pointer to an empty lambda.