Accessing static Game Data: DataAssets, ObjectLibrary, AssetManager, AssetRegistry

Hi, I’m loading some static game data using ObjectLibrary. ObjectLibrary’s comments say it’s better to use AssetManager, but I couldn’t figure out how to accomplish this. AssetRegistry is an editor subsystem and internet FUD said not to use it.

I subclassed UDataAsset adding some properties, then use the editor created those assets in some folders. So /Content/Data/Categories has 4 assets created from Category.h. In my GameState, I’m loading and caching them when accessed with GetCatagories(). That happens in this code:



void ATortillaRushGameState::GetCategories(TArray<UCategory*> &OutCategories)
{
    if (!IsLoaded) LoadGameLibraryCaches();

    for (UCategory* Item : CategoryCache)
    {
       OutCategories.AddUnique(Item);
    }
}

void ATortillaRushGameState::LoadGameLibraryCaches()
{
    TArray<UObject*> OutCache;

    // Categories

    // Clear the current cache
    CategoryCache.Empty();
    // Load Objects into OutCache (LoadObjectsFromPath empties OutCache)
    LoadObjectsFromPath(UCategory::StaticClass(), TEXT("/Game/Data/Categories"), OutCache);
    // Copy Categories into CategoryCache
    for (UObject* Item : OutCache) CategoryCache.AddUnique(Cast<UCategory>(Item));

    IsLoaded = true;

    // Same for other caches

    UE_LOG(LogTemp, Warning, TEXT("Loaded static assets: %d Categories, %d Upgrades, %d Ingredients, %d Recipes"), CategoryCache.Num(), UpgradeCache.Num(), IngredientCache.Num(), RecipeCache.Num());
}

void ATortillaRushGameState::LoadObjectsFromPath(UClass* InBaseClass, FString AssetPath, TArray<UObject*> &OutCache)
{
    OutCache.Empty();

    UObjectLibrary* MyObjectLibrary = UObjectLibrary::CreateLibrary(InBaseClass, true, GIsEditor);

    TArray<FString> AssetPaths;
    AssetPaths.Add(AssetPath);

    MyObjectLibrary->LoadAssetDataFromPaths(AssetPaths);

    // Query Results
    TArray<FAssetData> AssetData;
    MyObjectLibrary->GetAssetDataList(AssetData);

    // Cache assets with Sync Load
    for (FAssetData AssetDatum : AssetData)
    {
       OutCache.AddUnique(AssetDatum.GetAsset());
    }
}



ObjectLibrary appears to work, but I ran all over the county trying to get AssetManager to work using PrimaryDataAsset and failed.

Did I misunderstand AssetManager’s use or purpose? I didn’t understand how to select the class (UCategory), or the path, or how to get the objects out. How could I get this to work with AssetManager?

Would AssetRegistrywork outside the editor? This seems like a handy AssetRegistry example.

Thanks for your help!

Bump does anyone use static data in their games? Or do you design it into the actors themselves?

If you’re trying to load blueprint assets, it works a little bit differently



TArray<FAssetData>AssetData;

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

AssetRegistry.Get().GetAssetsByClass( UBlueprint::StaticClass()->GetFName() , AssetData , **true** );

for (auto &myData : AssetData) {

    UBlueprint* BP = GetBlueprintFromCDO( myData.GetAsset() );

    /// ..
}


“true” param in GetAssetsByClass() is an important thing to note there.
And there’s no reason why this wouldn’t work outside editor, AssetRegistry is a Runtime module.