Serialize Game

Hello All.

I’m trying to serialize parts of my game, which is spread over several levels and game modes. I need to load each level, interrogate which objects are in the scene, and serialize them as needed. Unfortunately I cannot get the levels to load into the world correctly. I’m not certain of the order of operations below either, in particular the GEngine->WorldAdded() part, but also the general setup and loading of the levels. After the UGameplayStatics::OpenLevel call the world appears to be empty.

Can anyone offer any guidance on this?

Thank you.

UWorld* world = NewObject<UWorld>();
            
UWorld::InitializationValues initValues;
initValues.AllowAudioPlayback(true);
initValues.CreateAISystem(true);
initValues.CreateFXSystem(true);
initValues.CreateNavigation(true);
initValues.CreatePhysicsScene(true);
initValues.InitializeScenes(true);
            
world->InitializeNewWorld(initValues);
GEngine->WorldAdded(world);

FWorldContext& wc = GEngine->CreateNewWorldContext(EWorldType::Game);
wc.SetCurrentWorld(world);

FFullyLoadedPackagesInfo fullyLoad;
fullyLoad.FullyLoadType = EFullyLoadPackageType::FULLYLOAD_Map;
fullyLoad.PackagesToLoad.Add(level->LevelPath);
wc.PackagesToFullyLoad.Add(fullyLoad);

UGameplayStatics::OpenLevel(world, level->LevelPath, true);

TArray<AActor*> Managers;
UGameplayStatics::GetAllActorsOfClass(world, AMyManager::StaticClass(), Managers);
if (Managers.Num() > 0)
{
    AMyManager* manager = Cast<AMyManager>(Managers[0]);
    *outFile << *manager;
}

I may be getting a little closer. Sometimes I am able to serialize 6 levels worth of data before it crashes, other times, I only get a few. Here’s what I have thus (which is contained within a loop over the levels):

UWorld* world = UWorld::CreateWorld(EWorldType::Game, true);
FWorldContext& wc = GEngine->CreateNewWorldContext(EWorldType::Game);
wc.OwningGameInstance = gameInstance;
wc.SetCurrentWorld(world);

//APlayerController* pc = wc.World()->SpawnActor<APlayerController>(APlayerController::StaticClass());
//wc.World()->AddController(pc);

UPendingNetGame* pending = nullptr;
FString Error;
if (GEngine->LoadMap(wc, *level->LevelPath.ToString(), pending, Error))
{
    // the below causes an error which is something I wouldn't expect to
    //AMyGameMode* gameMode = (AMyGameMode*)UGameplayStatics::GetGameMode(world);
    //*outFile << *gameMode;

    // getting the World Settings, then getting the game mode is a workaround
    TArray<AActor*> WorldSettings;
    UGameplayStatics::GetAllActorsOfClass(wc.World(), AWorldSettings::StaticClass(), WorldSettings);
    if (WorldSettings.Num() > 0)
    {
        AWorldSettings* worldSettings = Cast<AWorldSettings>(WorldSettings[0]);
        AMyGameMode* gameMode = (AMyGameMode*)worldSettings->DefaultGameMode.GetDefaultObject();
        *outFile << *gameMode;
    }

    TArray<AActor*> Managers;
    UGameplayStatics::GetAllActorsOfClass(wc.World(), AMyManager::StaticClass(), Managers);
    if (Managers.Num() > 0)
    {
        AMyManager* manager = Cast<AMyManager>(Managers[0]);
        *outFile << *manager;
    }
}

GEngine->DestroyWorldContext(wc.World());