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);
		}
	}

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.

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.

Thanks for the information, but this works fine!