Loading Map from Pak at Runtime

I’m trying to add content dynamically to my game during runtime and am running into some strange behavior. I’ve bundled up all the assets for a particular map into a separate pak file which then gets mounted using the following code:

    FPakPlatformFile* PakPlatform = new FPakPlatformFile();
    PakPlatform->Initialize(&PlatformFile, TEXT(""));
    FPlatformFileManager::Get().SetPlatformFile(*PakPlatform);

    UE_LOG(LogTemp, Log, TEXT("Mounting pak %s"), *PakPath);
    if (!PakPlatform->Mount(*PakPath, 0, TEXT("../../../"))) {
        UE_LOG(LogTemp, Error, TEXT("Failed to mount %s"), *PakPath);
}
else {
	UE_LOG(LogTemp, Log, TEXT("Serializing asset data for %s"), *PakFilename);
	FArrayReader SerializedAssetData;
	int32 DashPosition = PakFilename.Find(TEXT("-"), ESearchCase::CaseSensitive, ESearchDir::FromEnd);
	if (DashPosition != -1)
	{
		PakFilename = PakFilename.Left(DashPosition);
		FString AssetRegistryName = PakFilename + TEXT("-AssetRegistry.bin");
		UE_LOG(LogTemp, Log, TEXT("AssetRegistryName %s"), *AssetRegistryName);
		if (FFileHelper::LoadFileToArray(SerializedAssetData, *(FPaths::GameDir() / AssetRegistryName)))
		{
			// serialize the data with the memory reader (will convert FStrings to FNames, etc)
			AssetRegistryModule.Get().Serialize(SerializedAssetData);
		}
		else
		{
			UE_LOG(LogTemp, Warning, TEXT("%s could not be found"), *AssetRegistryName);
		}
	}
}

This code seems to work when my pak file is already present when the game is started. The problem I’m having is when the pak file is added to the game while it’s running (whether by downloading it from a server or manually copying the file). The map still loads but the textures are all messed up and there is an infinite loop of log messages which state:

The file '.../.../.../Game/Content/Maps/NextMap.umap' contains unrecognizable data, check that is is of the expected type.

The level I’m trying to load has a few sublevels, which are the ones listed in the warnings. I know there is nothing wrong with the pak file since the map will load just fine if I restart the game leaving the pak file there when it starts up.

So my question:
What am I missing? What is Unreal doing under-the-hood upon startup in regards to pak files that I’m not replicating properly when I try to manually load the pak file?

Well, it turns out I was trying too hard. I replaced all the above code with the following:

if (FCoreDelegates::OnMountPak.IsBound()) {
    FCoreDelegates::OnMountPak.Execute(PakPath, 4); //Number should be 0-4; specifies search order
}

And now it works. Turns out there are several useful FCoreDelegates which can help with basic things. Hope this helps someone else.

I’m trying to dynamically add content

and use UnrealPak.exe to packing pak, but no find AssetRegistry.bin file

How to get the AssetRegistry.bin?

Can you give me demo, or implementation of steps?

These instructions for how I make the pak file is assuming windows. The process is similar on OSX:

You’ll first need to cook the assets. I find that the easiest way to do that is to simply add the content into a new project and create a packaged build:
-1st go to File->Packaging->Packaging Preferences and make sure “For Distribution” is checked.
-2nd go to File->Packaging->Windows->64-bit to create the packaged build.

During that process Unreal will cook the content for you and by default they’ll be in %YourProjectDirectory%\Saved\Cooked\WindowsNoEditor
There will also be a txt file that was generated in:
“C:\Users\yourusername\AppData\Roaming\Unreal Engine\AutomationTool\Logs\C+Program+Files+Epic+Games+4.8\PakList_ProjectName-WindowsNoEditor.txt”
This file is what Unreal is using to create the pak file and will save you time to make yours. You can go through that file and delete all the lines of files you don’t need for your Content (everything that points to engine, config, and starter content for example). Then you can run the following command to create the pak file:
“C:\Program Files\Epic Games\4.8\Engine\Binaries\Win64\UnrealPak.exe” C:\location\of\output\Content.pak -create=C:\path\to\modifed\PakList_ProjectName–WindowsNoEditor.txt

If you want to make a compressed pak file then in the paklist txt file you need to add “-compress” to the end of each line (or use the compress setting before cooking the content).

1 Like

I encountered a strange problem, Static mesh texture at runtime exception in win64.

52180-20150731.png

Thanks for you

IPlatformFile& InnerPlatform = FPlatformFileManager::Get().GetPlatformFile();
FPakPlatformFile* PakPlatform = new FPakPlatformFile();
PakPlatform->Initialize(&InnerPlatform, TEXT(“”));
FPlatformFileManager::Get().SetPlatformFile(*PakPlatform);

UE_LOG(LogGame, Warning, TEXT("IndexExpansionContent"));

#if PLATFORM_ANDROID
//GFilePathBase UE4Game
FString InFeaturePackPath = TEXT(“Content/RestrictedAssets/Pak/MyAssetPak.pak”);
#else
FString InFeaturePackPath = FPaths::GameContentDir() + TEXT(“RestrictedAssets/Pak/MyAssetPak.pak”);
#endif
if (FCoreDelegates::OnMountPak.IsBound())
{
//PakPlatform->Mount(*InFeaturePackPath, 4, *FPaths::GameContentDir());
FCoreDelegates::OnMountPak.Execute(InFeaturePackPath, 4);
}
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *FPaths::GameContentDir());
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, *InFeaturePackPath);
UE_LOG(LogGame, Warning, TEXT(“OnMountPak”));

UStaticMesh*  objX = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), nullptr, TEXT("StaticMesh'/Game/RestrictedAssets/Fbx/baijian5.baijian5'")));
UE_LOG(LogGame, Warning, TEXT("StaticLoadObject objX"));
AStaticMeshActor* actor = GetWorld()->SpawnActor<AStaticMeshActor>(FVector::ZeroVector, FRotator::ZeroRotator);
actor->GetStaticMeshComponent()->Mobility = EComponentMobility::Movable;
actor->GetStaticMeshComponent()->SetStaticMesh(objX);

You should comment out those first 3 lines (IPlatformFile…) as they may be doing something undesirable (Unreal does a lot under the hood that I don’t fully understand, even when looking under the hood). I had similar texture issues when I asked the original question. I’m not seeing anything obviously wrong with the way you’re spawning the mesh. I can take another look at my own code to see if I’m doing anything else differently.

comment out those first 3 lines (IPlatformFile…)
but FCoreDelegates::OnMountPak.IsBound() == false at edit mode
Do I still need to modify other place?

Can you give me a demo code?
My email address is @gmail.

Thanks to you!

I guess I forgot to mention that I’m always testing in a standalone build and not within the editor. My project relies on everything being loaded dynamically at runtime from pak files which are loaded on demand, which I couldn’t get working from within the editor.

Hi, I’m having similar problems, I’ve got the pak file loading, I’ve got the OpenLevel parsing the Init Code again (cannot debug step into that code with as debugGame does not bind the OnMountPak) but the original level is still showing and the World name is the same. How did you get the newly loaded level to display? Thanks.

@w27… were you able to solve the problem of " FCoreDelegates::OnMountPak.IsBound() == false at edit mode " ?
same on my side. always false.

hello,Can you explain the specific role of this function it?“FCoreDelegates::OnMountPak.Execute(PakPath, 0, nullptr)”,Thanks for you!

I’ve got static mesh by loading pak file,and static mesh texture have black spot at runtime in win64.
Could you give me some advices?Thanks.

我的也是静态烘焙的贴图全是错的,只有动态灯光是对的,你解决没有啊

Hello!
From the UnrealEngine community,
How to dynamically load the specified content in the PAK (as in a blueprint, material, and the name of the image from the PAK).
Can you give me a demo code?
My email address is

Thanks to you!

Hello!
From the UnrealEngine community,
How to dynamically load the specified content in the PAK (as in a blueprint, material, and the name of the image from the PAK).
Can you give me a demo code?
My email address is

Thanks to you!

Hello!
From the UnrealEngine community,
How to dynamically load the specified content in the PAK (as in a blueprint, material, and the name of the image from the PAK).
Can you give me a demo code?
My email address is

Thanks to you!

如何通过名称动态加载PAK里指定的内容(如通过一个蓝图、材质和图片的名称从PAK里获取)

http://dorgon.horizon-studio.net/archives/693

I have the same program. Do you have a solution now . 我也是同样的问题,cooked 后的asset打包后,在进行加载,材质的贴图不正确,就跟上图一样,现在不知道怎么弄了!

Guys please help!!
https://answers.unrealengine./questions/562601/mounting-pak-files-android-load-fail.html