How to get a list of assets from code?

Hi.

Is it possible to get a list of all assets from a specific folder in C++? Say, I want to get a list of all static mesh assets from “/Game/Assets/Meshes/” in order to use them to create Actors dynamically.

Maybe to have a TArray of all meshes in this specific path.

You could use the asset registry to do this. This won’t actually load the assets, so you can lazy load them if you need to.

FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>(FName("AssetRegistry"));
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();

// Need to do this if running in the editor with -game to make sure that the assets in the following path are available
TArray<FString> PathsToScan;
PathsToScan.Add(TEXT("/Game/Assets/Meshes/"));
AssetRegistry.ScanPathsSynchronous(PathsToScan);

TArray<FAssetData> MeshAssetList;
AssetRegistry.GetAssetsByPath(FName("/Game/Assets/Meshes/"), MeshAssetList);

Alternatively you can do the following, which will load all the assets in the path immediately and then return you them.

TArray<UObject*> MeshAssets;
EngineUtils::FindOrLoadAssetsByPath(TEXT("/Game/Assets/Meshes/"), MeshAssets, EngineUtils::ATL_Regular);

You’ll need to make sure the assets in that path get cooked by adding them to the “Additional Asset Directories to Cook” in your project packaging settings.

Neither of these solutions work for me neither of them return anything at all the arrays are empty

You’ve definitely added the path(s) containing the assets you want to use at runtime to your “Additional Asset Directories to Cook” in your project packaging settings?

Hi, How to set this MeshAssests to RootCompoent?

Is there way to scan and registry in BP?

Hey,

For anyone who stumbles on this and finds the FindOrLoadAssetsByPath returns 0 - You need to remove the trailing / from the end of the filepath. Otherwise it fails in the IsValidLongPackageName QC step.