Dynamic duplicate level with C++

Hey unreal experts!

I’m trying to implement a replay system and it has to use Dynamic Duplicated Levels.
The system is implemented with C++.
I want to add levels to the level collection of my world and mark it as duplicated.
I didn’t find a way to do it within the unreal editor, so I try to add the level via code.
This is the code from the Lyra project:

void ULyraExperienceManagerComponent::OnExperienceLoadComplete()
{
	//Populate Dynamic Duplicated Levels

	UWorld* World = GetWorld();
	if (World)
	{
		FName sceneName = FName(TEXT("L_ShooterGym"));
		if (sceneName != NAME_None)
		{
			World->DuplicateRequestedLevels(sceneName);
		};
	}

	FLevelCollection* const DuplicateCollection = World->FindCollectionByType(ELevelCollectionType::DynamicDuplicatedLevels);
	// Nothing to do here if the DuplicateCollection exists and is visible
	if (DuplicateCollection && DuplicateCollection->IsVisible())
	{
		OnExperienceFullLoadCompleted();
		return;
	}

First I get the world, then add a map level to the world’s LevelCollection with the DuplicateRequestedLevels(sceneName) method.
The LevelCollection is a TArray and it has multiple lists of levels and each has a type: dynamic, persistant, etc…

this is the DuplicateRequestedLevels method in World class:

void UWorld::DuplicateRequestedLevels(const FName MapName)
{
	if (GEngine->Experimental_ShouldPreDuplicateMap(MapName))
	{
		// Duplicate the persistent level and only dynamic levels, but don't add them to the world.
		FLevelCollection DuplicateLevels;
		DuplicateLevels.SetType(ELevelCollectionType::DynamicDuplicatedLevels);
		DuplicateLevels.SetIsVisible(false);
		ULevel* const DuplicatePersistentLevel = DuplicateLevelWithPrefix(PersistentLevel, 1);
		if (!DuplicatePersistentLevel)
		{
			UE_LOG(LogWorld, Warning, TEXT("UWorld::DuplicateRequestedLevels: failed to duplicate persistent level %s. No duplicate level collection will be created."),
				*GetFullNameSafe(PersistentLevel));
			return;
		}
		// Don't tell the server about this level
		DuplicatePersistentLevel->bClientOnlyVisible = true;
		DuplicateLevels.SetPersistentLevel(DuplicatePersistentLevel);
		DuplicateLevels.AddLevel(DuplicatePersistentLevel);

		for (ULevelStreaming* StreamingLevel : StreamingLevels)
		{
			if (StreamingLevel && !StreamingLevel->bIsStatic)
			{
				ULevel* DuplicatedLevel = DuplicateLevelWithPrefix(StreamingLevel->GetLoadedLevel(), 1);
				if (!DuplicatedLevel)
				{
					UE_LOG(LogWorld, Warning, TEXT("UWorld::DuplicateRequestedLevels: failed to duplicate streaming level %s. No duplicate level collection will be created."),
						*GetFullNameSafe(StreamingLevel->GetLoadedLevel()));
					return;
				}
				// Don't tell the server about these levels
				DuplicatedLevel->bClientOnlyVisible = true;
				DuplicateLevels.AddLevel(DuplicatedLevel);
			}
		}

		LevelCollections.Add(MoveTemp(DuplicateLevels));
	}
}

aventually line LevelCollections.Add(MoveTemp(DuplicateLevels)); should add the level to the collection.

but I get an empty collection with searching for the duplicated levels type, like that:

FLevelCollection* const DuplicateCollection = World->FindCollectionByType(ELevelCollectionType::DynamicDuplicatedLevels);

So it’s clear that the duplicated level was not added, I can’t debug the World class since its locked I think.
unreal_world_script

Thanks for any help

1 Like

I didn’t find a way to edit my previous question, so I am just adding more here:

The project I’m working with is the LyraStarterGame.

To be able to DuplicateRequestedLevels (see prev. question code), the Experimental_ShouldPreDuplicateMap should be enabled, as such, in DefaultEngine.ini file:

[/Script/EngineSettings.GeneralProjectSettings]
Experimental_ShouldPreDuplicateMap=True

I added these lines, and still GEngine->Experimental_ShouldPreDuplicateMap(MapName)
returns false.

So, following these docs: DemoNetDriver and Streamers in Unreal Engine | Unreal Engine 5.2 Documentation

I’ve added the L_ShooterGym level to the Levels window, but clicking on the level shows nothing on the details panel.
which is contrary to what the docs say:

so basically I can’t even mark a level to not be “Is Static” since the details are not showing.

I guess Lyra Game Starter levels are Read-Only, so I can’t modify them.

Will try with my own level.

[UPDATE]
I tried looking for solutions with Bard, Bing, ChatGpt 4, ChatGPT 3.5 - how is it even possible to work with this engine, if it starts to be a bit more complicated the the simple blueprints all are excited about?! What am I missing?

3 days just to understand how to add a level to Dynamic Duplication Collection.

Where’s the catch?

1 Like

Implementing Instant Replays (Killcam)

from Alvaro Jover-Alvarez

he is implementing in lyra killcam

i have also try it and im stuck on this error message

Error: UWorld::DuplicateRequestedLevels: Attempted to duplicate streaming levels for partitioned world. This is not a supported operation.

this is marked for UEFN forums, you’ll probably have better luck on the Unreal Engine specific ones

2 Likes

I think you were pretty close on figuring this out, maybe half way down the path I’d say but if that level thing is still an issue, I can help : you need to open Levels window from “Window” menu and try to edit variables for level there after selecting sublevel I think… I know I’m late, but I tried to help haha… cuz I’m kinda stuck in this thing myself, I haven’t tried with lyra just yet, I’m trying to understand everything from the surface, if you got any advice or resources you can share, please do so. It’s so stupid that I can’t find enough information on this topic.