x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Level Streaming: Loading levels in for loop only loading first level

Hi there! I am currently trying to load in levels from an array of FNames, that I use as parameters in the LoadLevel function. I want these levels to load in when the player also loads in. The problem I am having is that no matter how big the array is, it only loads the first level in that array (I have tried switching entries in the array to make sure that I am not simply using the wrong naming conventions for the other levels etc.). Is there any particular reason why this would not be possible? Thank you in advance!

 // Load the stored levels
     TArray<FName> streamingLevelsReferences = saveObject->GetStreamLevelReferences();
     for (int i = 0; i < streamingLevelsReferences.Num(); i++)
     {
         if (streamingLevelsReferences[i] != "")
         {
             UE_LOG(LogTemp, Error, TEXT("Loading: %s!"), *streamingLevelsReferences[i].ToString());
             FLatentActionInfo LatentInfo;
             UGameplayStatics::LoadStreamLevel(this, streamingLevelsReferences[i], true, false, LatentInfo);
         }
     }
Product Version: UE 4.20
Tags:
more ▼

asked Aug 17 '18 at 04:28 PM in C++ Programming

avatar image

Borzi
174 43 39 47

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I assume this is yet another bug for CPP programmers. I have, however, come up with a fix, using the LatenInfo parameter. In the class that is trying to load in the level, store the following members:

     UFUNCTION()
     void LoadNextLevel();

 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Load/Save", Meta = (AllowPrivateAccess = "true"))
     TArray<FName> m_levelsToLoad;
     int m_loadedLevelsIndex = 0;
     FLatentActionInfo m_loadingActionInfo;

In the constructor, assign the latent action info like so:

 m_loadingActionInfo = FLatentActionInfo();
     m_loadingActionInfo.CallbackTarget = this;
     m_loadingActionInfo.ExecutionFunction = FName("LoadNextLevel");
     m_loadingActionInfo.UUID = 123456;
     m_loadingActionInfo.Linkage = 0;

Load level should look something like this:

 LoadNextLevel()
 {
     if (m_loadedLevelsIndex < m_levelsToLoad.Num())
     {
         UGameplayStatics::LoadStreamLevel(this, m_levelsToLoad[m_loadedLevelsIndex], true, false, m_loadingActionInfo);
         // Increment counter
         m_loadedLevelsIndex++;
     }
 }

Calling LoadLevel() once should load all the levels consecutively - hope this saves someone the hassle that I had to go through.

more ▼

answered Aug 23 '18 at 07:37 PM

avatar image

Borzi
174 43 39 47

avatar image Gossy Aug 24 '18 at 04:55 AM

It's not a bug, it's intentional. The UGameplayStatics level streaming functions are not really meant for usage in C++ because, as you noted, they use the Latent Action Manager.

Each of the Functions has a guard ensuring that any particular UObject can only execute one FPendingLatentAction for level streaming at a time. This means that, in blueprint, when the action is "completed" you know which one it was for.

To work with the Level Streaming in C++ you should be getting the ULevelStreaming objects from the UWorld and marking the ShouldBeLoaded and ShouldBeVisible flags directly. Although, if you look at the source code for the UGameplayStatics functions, you'll note it's actually quite a task ensuring you get the right package to load because PIE and other variants of renaming.

I tried and found it easier / more grokkable to deal with the Level Streaming in Blueprint, in fact I've just submitted a Blueprint Asset Pack to the Marketplace that helps with just this.

avatar image Borzi Aug 24 '18 at 06:01 PM

Thanks for the information, but this works fine!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question