Loading assets from a pak file

Looking into this too at the moment.

I was able to get something using a second project with the same name.

!!!THIS IS STILL WORK IN PROGRESS AND THE RESULTS ARE NOT AS EXPECTED!!! See below.
EDIT:

Solved my issues, see EDIT below.

  1. From your main project go to File->PackageProject->Windows->WindowsPlatform
  2. Chose MainPackageDir
  3. Make a second project that has the EXACT SAME PROJECT NAME but in a different directory
  4. Add some materials to Content/Mods
  5. Package the second project the same way as the first
  6. Chose a SecondPackageDir
  7. Go to SecondPackageDir/WindowsNoEditor/PROJECTNAME/Content/Pak
  8. Rename the .pak to ProjectName_Mod.pak (or Whatever.pak) as at the moment it is the same filename as the main project and that will not work.
  9. Copy the ProjectName_Mod.pak to MainProjectDir/WindowsNoEditor/PROJECTNAME/Content/Pak
  10. Start .exe of the main project

But nothing happend? Right, how could it? The assets are only mounted correctly at the moment, not used in the game.

So how to access the stuff:
Create a C++ BlueprintFunctionLibrary and add the following function.



    /**
     *  Get (and load) all assets from a folder relative to GameDir/Content that match the class.
     *
     *    @param assetClass   Class of the assets to look for
     *    @param path            Path is relative to GameDir/Content e.g. "/somepath". NOTE: "" or "/" WILL NOT WORK!!!
     *    @param assets        OUT: The assets found
     */
    UFUNCTION(BlueprintPure, Category = "Utility", meta  = (DeterminesOutputType = "assetClass", DynamicOutputParam = "assets"))
        static void GetAllAssetsInDirectoryRecursive(const TSubclassOf<UObject> assetClass, const FString path, TArray<UObject*>& assets);
{
    UObjectLibrary* objectLibrary = UObjectLibrary::CreateLibrary(assetClass.Get(), false, true);
    objectLibrary->LoadAssetsFromPath(*(FString(TEXT("/Game")) / path));
    objectLibrary->GetObjects(assets);
    // As the library is a UObject it should be picked up by the GarbageCollector now *pray*
}


  1. Add the function in your code where you want to access the assets and package the main project again.
  2. Run the .exe of the main project

The C++ function works flawlessly within the main project.

My Issues:
I’m currently only trying with textures and materials from the mod
-Textures seem to work without issue
-Materials are bugged at this time. Just a simple red material from the mod shows up as a high specular glitch that changes color depending on time and view angle
-This only works when restarting the game, not at runtime.

And as we package the mod project the normal way, we also include engine content that we don’t want. This might also lead to my material issue. The mod.pak is ~100MB for a simple texture and material.

EDIT:
-To fix the material issue, the projects must have the same rendering settings. Materials are broken when loaded from a pak file - Rendering - Unreal Engine Forums
-It is possible to exclude the engine content when packaging by using chunks. This way the custom content is one or multiple chunks (separate .pak files). Cooking Content and Creating Chunks in Unreal Engine | Unreal Engine Documentation
Then move only the correct chunkX.pak over to the main project. It should also be possible to make manual cooking/packaging code that ignores the engine stuff but did not look in that yet.