I’m using AssetManager and AssetRegistry to load content at runtime. It all works in Editor, but fails when using in cooked build.
Here Is how I initialize AssetRegistry:
#include "ActionRPGGame.h"
#include "Modules/ModuleManager.h"
#include "AssetRegistryModule.h"
#include "Engine/AssetManager.h"
void FActionRPGGameModule::StartupModule()
{
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();
//AssetRegistryModule.Get().OnFilesLoaded().AddUObject(this, &UARUIAbilityManagerComponent::FinishedLoadinFiles);
TArray< FString > ContentPaths;
TArray<FString> RootPaths;
FPackageName::QueryRootContentPaths(ContentPaths);
AssetRegistry.ScanPathsSynchronous(ContentPaths);
};
IMPLEMENT_PRIMARY_GAME_MODULE(FActionRPGGameModule, ActionRPGGame, "ActionRPGGame" );
Then:
FAssetRegistryModule& AssetRegistryModule = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry");
IAssetRegistry& AssetRegistry = AssetRegistryModule.Get();
TArray<FAssetData> AssetData;
FARFilter Filter;
Filter.TagsAndValues.Add("AbilityTagSearch", InAbilityTag.ToString());
AssetRegistryModule.Get().GetAssets(Filter, AssetData);
FPrimaryAssetId PrimaryAssetId = FPrimaryAssetId(FPrimaryAssetType("Ability"), AssetData[0].AssetName);
FPrimaryAssetTypeInfo Info;
if (Manager->GetPrimaryAssetTypeInfo(PrimaryAssetId.PrimaryAssetType, Info))
{
FStreamableDelegate del = FStreamableDelegate::CreateUObject(this, &UAFAbilityComponent::OnFinishedLoad, InAbilityTag, InInputName, PrimaryAssetId);
Manager->LoadPrimaryAsset(PrimaryAssetId,
TArray<FName>(),
del);
}
Which calls this function, when delegate is called:
void UAFAbilityComponent::OnFinishedLoad(FGameplayTag InAbilityTag,
FGameplayTag InInputName, FPrimaryAssetId InPrimaryAssetId)
{
if (UAssetManager* Manager = UAssetManager::GetIfValid())
{
UObject* loaded = Manager->GetPrimaryAssetObject(InPrimaryAssetId);
TSubclassOf<UGAAbilityBase> cls = Cast<UClass>(loaded);
if (cls)
{
NativeAddAbility(cls, nullptr, InInputName);
}
{
Manager->UnloadPrimaryAsset(InPrimaryAssetId);
}
}
}
In turn it gets to this function:
const FPrimaryAssetData* UAssetManager::GetNameData(const FPrimaryAssetId& PrimaryAssetId, bool bCheckRedirector) const
{
const TSharedRef<FPrimaryAssetTypeData>* FoundType = AssetTypeMap.Find(PrimaryAssetId.PrimaryAssetType);
// Try redirected name
if (FoundType)
{
const FPrimaryAssetData* FoundName = (*FoundType)->AssetMap.Find(PrimaryAssetId.PrimaryAssetName);
if (FoundName)
{
return FoundName;
}
}
And it turns out that >AssetMap< Is empty for some reason in cooked build.
Any idea how to fix it ?