How to load level from pak files?

Hi, I am working on loading levels from pak files. I successfully loaded widget assets, but I failed to load the level using a similar method. I placed them in the same directory, but I encountered issues:

LogLinker: Warning: Failed to load '/Game/Pak2/NewWorld': Can't find file. 

Here is my code:

    TSharedPtr<FPakPlatformFile> pakPlatformFile = MakeShareable(new FPakPlatformFile());
    pakPlatformFile->Initialize(&FPlatformFileManager::Get().GetPlatformFile(), TEXT(""));
    IPlatformFile& oldPlatformFile = FPlatformFileManager::Get().GetPlatformFile();
    FPlatformFileManager::Get().SetPlatformFile(*pakPlatformFile);

    if (WITH_EDITOR)
    {
        pakPlatformFile->Mount(TEXT("D:/code_study/Content/Paks/pakchunk2-Windows.pak"), 1, TEXT("D:/code_study/Content/Pak2/"));
        GAllowUnversionedContentInEditor = true;
    }

    else
    {
        pakPlatformFile->Mount(TEXT("D:/code_study/Content/Paks/pakchunk2-Windows.pak"), 1, TEXT("../../../Content/Pak2/"));
        FPlatformFileManager::Get().InitializeNewAsyncIO();
    }

    FString worldPath = TEXT("/Game/Pak2/NewWorld");
    UGameplayStatics::OpenLevel(GetWorld(), FName(*worldPath));
    FPlatformFileManager::Get().SetPlatformFile(oldPlatformFile);

BTW I am using UE 5.3. I wonder if there is any tutorial introducing how to load maps from pak files?

1 Like

Ah, the problem is solved. I should firstly load the world and then open the level.

    FString worldPath = TEXT("World'/Game/Pak2/NewWorld.NewWorld'");
    UWorld* LoadedWorld = LoadObject<UWorld>(nullptr, *worldPath);
    if (LoadedWorld)
    {
        FString LevelName = LoadedWorld->GetName();
        UGameplayStatics::OpenLevel(GetWorld(), FName(*LevelName));
    }
    else
    {
        UE_LOG(LogTemp, Error, TEXT("Failed to load world."));
    }
1 Like

(post deleted by author)