# Load asset at runtime - 2020

**URL:** <https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [October 30, 2020, 4:54pm UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951 "2020-10-30T16:54:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Keyplan3D](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Keyplan3D](https://forums.unrealengine.com/u/Keyplan3D)\
**Post date:** [October 30, 2020, 4:54pm UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951/1 "2020-10-30T16:54:51Z")

</div>

Hello guys,  
I need to load at runtime a set of asset that will be downloaded from a distant server (a kind of DLC), and I’m stuck.

My strategy is to use the Editor for creating a package with my assets for a chosen platform, than use the .pak file in a second project (where it will be loaded at runtime).

\*\*1. It is the right way to do ? \*\*

For the moment, I can only mount the .pak file, and get the list of files in the pack:

```auto

    IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
    FPakPlatformFile* PakPlatformFile = **new** FPakPlatformFile();

    PakPlatformFile->Initialize(&PlatformFile, TEXT(""));

    FPlatformFileManager::Get().SetPlatformFile(*PakPlatformFile);

    **const** FString PakFilename = FPaths::ProjectContentDir() + TEXT("PakFiles/FirstDLC-MacNoEditor.pak");

    **const** FString MountPoint = FPaths::EngineContentDir();

    
    FPakFile pak_file{PakPlatformFile->GetLowerLevel(), *PakFilename, **false** };

    pak_file.SetMountPoint(*MountPoint);

    PakPlatformFile->Mount(*PakFilename, 0, *MountPoint)

```

**2. How to load theses assets into memory ?**

From what I saw, the most recent solution is to use the AssetManager object:

→ Create a primary asset object in the project, and link it with all asset that I want to export, with the same bundle tag for each asset.  
 → Export the package  
 → Try to import the primary asset, what goes load all secondary assets linked.

```auto

        UAssetManager &asset_manager = UAssetManager::Get();
        FPrimaryAssetId package_id = asset_manager.GetPrimaryAssetIdForPackage(package_name);

        FPrimaryAssetId primary_asset{"DLCObjects:FirstPack"};
        FAssetBundleData bundle_data;
        bundle_data.AddBundleAsset("AllTextures", soft_path);
        asset_manager.RecursivelyExpandBundleData(bundle_data);

    
        asset_manager.AddDynamicAsset(primary_asset, FStringAssetReference{}, bundle_data)

        /*
        TArray<FName> bundle_names;
         bundle_names.Add(FName{"AllTextures"});
          //Then call *asset_manager.GetPrimaryAssetObject(primary_asset);
*/*

```

But I never successfully get my UObject. I’m missing something ?

In an other hand, I find there is an AssetRegistry.bin file in the .pak file. I saw that when we mount a package, the file is loaded in memory and get references to the assets, but here too a got nothing from my file. Is this method still working ?

I also tried some methods with ObjectLibrary, but if I well understood the Asset Manager doc, this is an ancient method and the Asset manager is preferred.

Which method should I prefer today ?

---

<div class="post-metadata">

**Author:** ![paranoio](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@paranoio](https://forums.unrealengine.com/u/paranoio)\
**Post date:** [November 16, 2020, 8:31pm UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951/2 "2020-11-16T20:31:19Z")

</div>

hi, im also interested on this,  
I dont have the solution to your problem just wanted to share what i could find

i was able to load assets as dlcs using only blueprints(i did this like one year ago so i dont remember all the details),  
the pak file and other files should be uploaded to your server,  
be sure to use a valid structure and dont override those of your game  
for example if you have /game/shooter/bp\_hero  
and you want to add an skeletal mesh i would recommend to use a completely different path  
/games/shooterMounted/skeletalMesh

you define this structure from the editor when you create your new pak  
so i created two projects one for my game and one for the pak assets

there are a lot of options when creating the pak, make sure you export only  
the assets you want and not the entire game  
also i think i had issues with some materials not loading when using skeletal meshes

i think the pak exporter is actually a command line (its show in the logs after exporting)  
so in theory you could do it from windows console  
and maybe with python ?

---

<div class="post-metadata">

**Author:** ![Benaclejames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benaclejames/32/484626_2.png) [@Benaclejames](https://forums.unrealengine.com/u/Benaclejames)\
**Post date:** [November 18, 2020, 12:08am UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951/3 "2020-11-18T00:08:20Z")

</div>

Same here, I just wish there was more documentation on this. Ideally I’d really want to give players an SDK to create their own paks to then upload to an http server for players clients to download and import to useable objects at runtime but the few examples I’ve seen tend to be extremely outdated. Any assistance here would help a ton!

---

<div class="post-metadata">

**Author:** ![Keyplan3D](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Keyplan3D](https://forums.unrealengine.com/u/Keyplan3D)\
**Post date:** [November 18, 2020, 3:04pm UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951/4 "2020-11-18T15:04:38Z")

</div>

I’ll try to write my progression on this work when I made good advance, in the hope we got more documentation infos/ Unreal dev help.

For the moment, I can create .pak file from assets, and load them at runtime.

**Create .PAK:**

I use command-line tool provided by Unreal for creating paks files from a second project. First, I cook my content (I’m on macOS):

_./UE4Editor-Cmd UPROJECT -run=cook -targetplatform=MacNoEditor_

Where UPROJECT is the path to the .uproject file where are yours assets. Then:

_./UnrealPak /path/where/your/file/will/be/created/file.pak -create=/path/to/your/cooked/content/folder/ -compress_

The cooked asset can be found in the Saved/Cooked/ folder of your project.

**Load .PAK:**

I use this code to load at runtime:

```auto

      IPlatformFile& platform_file = FPlatformFileManager::Get().GetPlatformFile();
      pak_platform_file = **new** FPakPlatformFile();
      TArray<FAssetData> asset_datas;

      **if** (pak_platform_file->Initialize(&platform_file, TEXT(""))) {

            pak_platform_file->InitializeNewAsyncIO();

            FPlatformFileManager::Get().SetPlatformFile(*pak_platform_file);
        

            **const** FString pak_path{FPaths::ProjectContentDir() + TEXT("PakFiles/AssetPak-MacNoEditor.pak")};

            **const** FString mount_point{"/MountPoint/"}; //Where datas are after extraction

            **bool** is_mounted{pak_platform_file->Mount(*pak_path, 0, *mount_point)};

            **if** (is_mounted) {

                FPackageName::RegisterMountPoint("/ABC/", *mount_point);

                FAssetRegistryModule& registry = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");

                registry.Get().ScanPathsSynchronous({ "/ABC/" });
                registry.Get().GetAssetsByPath("/ABC/", asset_datas, **true** , **false** );

                //for(FAssetData &current_data: asset_datas) { current_data.GetAsset(); }
            } 
     }

```

Then you can get UObject from the GetAsset() function that will load it.

**Important things:**

- It’s look like they are a bug when we try to use a folder that is a root path for Unreal ("/Game/", “/Engine/”, …), which make files will not be find by the scan. So just don’t use them.
- _mount\_point_ must be an existing file on disk.
- **I** \*\* have no idea if it’s a good way to do the things or not.\*\*
- The load of an object will make a warning in logs: _‘asset’ has been saved with empty engine version. The asset will be loaded but may be incompatible._  
I think they are something i do wrong when creating the .pak file, but I don’t know what. Assets are still loaded.

This code works for me … halfway. Indeed, if you try to load simple assets likes Texture2D, you shouldn’t have problems. But when I try to load a Static Mesh for exemple, the reference to his Materials will be missing because Unreal search it at the same place they was in the project where I had my assets, and the log produce this type of warning:  
_‘…/…/…/…/…/…/…/MountPoint/Content/scene\_sphere\_binary.uasset’ failed to load ‘/Game/scene\_sphere\_binary/Wood’: Can’t find file._  
And the same is true for Materials → Texture2D.  
I’m searching for a way to change the references before load (or load a first time with fail, get references and reload the object), but for the moment I didn’t find anything really conclusive.

---

<div class="post-metadata">

**Author:** ![Benaclejames](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benaclejames/32/484626_2.png) [@Benaclejames](https://forums.unrealengine.com/u/Benaclejames)\
**Post date:** [November 19, 2020, 12:57am UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951/5 "2020-11-19T00:57:47Z")

</div>

Thanks a ton for the info! While I found it very informative, I wasn’t able to get anything to load. The pak will mount but GetAssetsByPath returns zero results for contents of a pak that I know contains valid objects. I’ve also noticed running in standalone mode gives a fatal error. God, I wish there was some more documentation for this kind of stuff!

---

<div class="post-metadata">

**Author:** ![Kulagin](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Kulagin](https://forums.unrealengine.com/u/Kulagin)\
**Post date:** [June 27, 2023, 9:16am UTC](https://forums.unrealengine.com/t/load-asset-at-runtime-2020/152951/6 "2023-06-27T09:16:29Z")

</div>

> [@Keyplan3D](#):
>
> 1. How to load theses assets into memory ?

Might be: [Load Static Mesh With Path in c++](https://forums.unrealengine.com/t/load-static-mesh-with-path-in-c/292234)  
I haven’t done loading the pak file yet, but it is my plan:

1. Load .pak file.
2. Load assets with StaticLoadObject.
