Load Several Stream Levels at once

I have several stream levels that I am trying to load at one time. I have all 5 saved into an array. Is there a way to load all 5 at once? I was thinking I could plug it into a ForEachLoop and plug that into a Load Stream Level, but that only loads the first item in the array. So maybe I am misunderstanding how that node works.

Did you ever figure out why this is? I have the same issue when trying to use a “for each” to unload a bunch of levels listed in an array.

I had a quick look at the Engine code. The LoadStreamLevel and UnloadStreamLevel are latent actions that only get queued up if there isn’t already one running. Therefore, a for-loop won’t work. The node will simply ignore any additional calls after the first one, and there will be no error messages in the log. You’ll have to detect when the first level is loaded/unloaded, then load/unload the second one, and so on.

Hi,

In case this helps someone in the future. You can loop over you levels and unload them. In the engine code it all ends up in a list. It checks if there exists an Action with the same Callbacktarget and UUID from the latent info.

int32 UUID = 0;
for(auto level : YOUR_LEVEL_ARRAY)
{
FLatentActionInfo LatentActionInfo;
LatentActionInfo.CallbackTarget = this;
LatentActionInfo.UUID = UUID;
UGameplayStatics::UnloadStreamLevelBySoftObjectPtr(GetWorld(), level, LatentActionInfo, false);
UUID++;
}

1 Like

A rather janky workaround for this is a recursive event.

I have implemented this as an array of World Soft Object References so that it updates as the files are renamed but you can do the same with an array of string.

You can’t directly create an array of World Soft Object References. You need to add a Load Stream Level node and right click the level input and promote to variable and then make it an array. No idea why you can’t just directly create it.

Call the event with an index of 0 to start it.

I believe each iteration of the for loop doesn’t wait for the level to be loaded and load stream level must finish before it is called again otherwise it is ignored.

With the recursive solution each successive iteration is only run when the previous one has finished.

2 Likes

Thanks so much! This is working. Here’s a bit of a variation on that.

In my case, I store required sublevels as a set of Name types. Then, when I need to load a set of levels, I call the “LoadMultipleLevels” event, passing it the set. It creates a temporary array from that, and calls “LoadLevelChained” which chips away at that array.