Loading the Next Level (UE 4.27)

This time, I am trying to make another function that causes the program to move from one level to the next, once the goal in each level is achieved. I did try the OpenLevel function, only to find that objects from the first level were transported along with the player character.

I’ve thought of using multiple branches and a counter that determines which level to go to; every time a level is completed, the integer representing the level number would increase by 1. Then, the program would check the level number; if it equals 2, for example, the game loads Level 2 after the first one is done. If the integer equals 5, the game loads Level 5 after Level 4 is finished.

What would be the best way to script this?

(I did look up similar forum questions, but the included image links are apparently broken.)

1 Like

Here is what I have, so far:

Please note this is from a child object of a parent actor.

Regarding what you have in blueprint right now and not your main post: usually you want to avoid many copy / paste and try and make things more automated.

For example, you can use a Struct to know what level has been completed or not:

// pseudo
struct LevelsStruct
{
    int32 LevelOrder; // Same as array?
    FName LevelToLoad;
    bool bLevelCompleted;
}

Have an instance of the struct per level in an array. Like this you can loop from lowest to highest looking for the first level that has not been completed. This one will naturally be the next level to load:

// pseudo
ForEach LevelsArray
{
    if !LevelsArray[i].bLevelCompleted
    {
        // Load level.
        // Break loop
    }
}

Looping like this will always load the next level that has the bool set to false. So if index 0 is false, meaning not completed, it checks to load next level. When level has been completed, set that bool to true and run the even again to get next bool that is false:

// Start For Loop
// loop 0
LevelsArray[0].bLevelCompleted == true // Skip
// loop 1
LevelsArray[1].bLevelCompleted == true // Skip
// loop 2
LevelsArray[2].bLevelCompleted == false // Load
// Break For Loop

Hope it makes sense… Wrote it like this because do not have access to UE right now. I’ll do it with BP later. The idea is to make it less iterative and easier to just add / remove / reorganize levels.

Having a struct will the info of each level will also make it a lot easier to load specific levels.

Somewhat. I’m not sure if I can implement C++ into a BP project. But okay. I appreciate the help, so far.

Its informal. The idea is the logic to implement in blueprint… also saves to write a wall of text. :grimacing:

I just remembered. That code is really Python, right? It’s been a long time since I’ve coded in that. Anyway, once you are able, please let me know what would work in blueprint mode. Or at least ideas on what to include.

This is the struct (just an example):

In Game Instance I created an array and populated the struct for each level:

I’m assuming you have a blueprint in every level that calls the Event PowerOn. If that blueprint had a public integer, you could do this:

  • First get a reference to the Game Instance:

  • I’m using an interface to “complete” each level. You can use that reference to update the struct corresponding to that level, in this case calling CE_CompleteLevel passing the LevelNumber integer.

  • This is that event in Game Instance:

  • First check if the level has been completed, if not then update and continue to next level.

  • Next update the bool in the struct.

  • The branch checks if all levels have been completed by comparing the int with the array length-1, if not ++LevelToLoad.

  • Finally call to load next level:

Simple enough so far…

So we basically turned this:

Into this:


With the added bonus that if I add 100 more levels, all I have to do is update the array of structs.

Hope it helps.
Also, forget the idea I typed in prev post… it’s overly complicated for the same result.

1 Like

So far, I have made the structure and then declared an array structure variable under the Goal BP. But, I don’t seem to have anything to put into each array member; there are no results when I click on each pull-down bar. Was there something I forgot to do?

Share a screenshot or video to know what you are referring to specifically.

Here are two screenshots showing the structure itself and the variable when declared under the Goal actor’s BP.


Screen Shot 2022-02-08 at 1.51.53 PM
Variable type has to be the same as the struct.

Oh! I think I got it now. Now, the menu looks something like this:

Nice!

For now you can fill the info for each level.

If you like to research and complicate things to learn: You can fill the struct with a data table and / or load game.

1 Like

Where did you get the BP_GameInstanceLevel, by the way? Is that the name of the variable? I tried typing in “cast to LevelStructure” (which is the structure variable), but no such option appeared.

It’s basically a blueprint that’s persistent through the entire game. You can visualize it as being a step above Level blueprints, allowing you to create logic that handles levels and actors distributed across different levels, among other stuff.

This video explains it very well:

Highly recommend you watch this video too… several times:

It covers a lot of info very fast, but it’s priceless. Game Instance is mentioned 7:30. I suggest watch from the beginning so it makes sense.

1 Like

Comprehensive example is comprehensive.

2 Likes

Under the LoadNextLevel event, how did you make a “Get” node with the array options from the “Level Struct Array” variable (which is “LevelStructure” in my program)? Every time I tried to click and drag from that variable, I’d only have array and integer slots when I try to get a copy or ref.

Also, this is under Goal or PuzzleGameInstance (which is my custom Game Instance)?

You mean this?

You can also drag from the output pin and search for Break.

1 Like

Ah, I didn’t know I could do that! That helps tremendously.

1 Like

Now, referring to the below image –

I haven’t been able to make a “Set” node for the PuzzleGameInstance blueprint in this same way as before. Also, where does it go to on the right side?