Correct way of using DataAsset for configuration

Hi all,

I’m having an hard time understanding how to properly use DataAssets in C++. I’m trying to configure a custom WorldSubsystem in it’s initialize method.

Theese are the steps I’ve done so far:

  1. Create a C++ class which inherits from UPrimaryDataAsset (which contains only 1 field)
UCLASS(BlueprintType)
class MYPROJECT_API UTimeSubsystemConfigDataAsset : public UPrimaryDataAsset
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Calendar")
	int32 DaysInMonth = 2;
}
  1. Created a Blueprint DataAsset which has my C++ class as Parent;

  2. Configured AssetManager in Project Settings->DataAsset manager

  3. Trying to load the dataAsset to configure my subsystem (Relevant code)

if(UAssetManager* AssetManager = UAssetManager::GetIfValid())
	{
		FPrimaryAssetType Type = FPrimaryAssetType(FName("TimeSubsystem"));
		TArray<FAssetData> Assets;
		AssetManager->GetPrimaryAssetDataList(Type, Assets);
		if(Assets.Num() > 0)
			GEngine->AddOnScreenDebugMessage(-1, 1.f, FColor::Red, TEXT("DATA ASSET FOUND"));
	}

The AssetManager is initializated and valid but does not populate the Assets array.
Digging into debug I found that the GetPrimaryAssetDataList return false due to the line code in the image:

Am I doing it wrong? I feel like something is missing but I cannot see what’s wrong. The documentation is not so well documented (especially for non english speakers, but that’s my problem).

Any help would be really appreciated

Thanks

Luca

2 Likes

Perhaps the data asset manager is initialized after game instance subsystems

Hi Natalo, thanks for your reply. Actually the AssetManager is initialized (the top-most IF on point 4 checks for the validity of AssetManager and return true).

Also my subsystem is not a GameInstanceSubsystem but a WorldSubsystem (I should have said it explicitly in my post, sorry)

Not an expert, but I believe that your custom UPrimaryDataAsset class needs to override the following function in order for the asset manager to handle it properly:
FPrimaryAssetId GetPrimaryAssetId() const override;

Here’s how I override on mine in the .cpp:

{
	return FPrimaryAssetId(ItemType, GetFName());
}```
2 Likes

Hi @KelbyVP, thanks a lot! Once I overrided the GetPrimaryAssetId() method my DataAsset was successfuly found.

Thanks a lot!

1 Like

Glad it worked!