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.
Thanks for any help