Is it possible to cast to a Data Asset Class in Blueprints?

I have a c++ data asset class UBaseDataAsset that inherits from UDataAsset and another c++ class UPlantDataAsset that inherits from UBaseDataAsset. I store objects in TArray of UBaseDataAsset, but when I try to access them through a loop I can’t cast to UPlantDataAsset and can only work with element as UBaseDataAsset even though UPlantDataAsset is a child of UBaseDataAsset.

Actor holding your list, display function and TArray holding mixed list

UPROPERTY(EditAnywhere,BlueprintReadWrite)
TArray<UBaseDataAsset*> Lists;

UFUNCTION(BlueprintCallable)
void PrintList();

print function


void AMyActor::PrintList() {

	for (int32 i = 0; i < Lists.Num(); i++) {
		if (UPlantDataAsset* plant = Cast<UPlantDataAsset>(Lists[i])) {
			if (GEngine) {
				GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, plant->PlantName);
			}
		}
	}

}


Plant data asset

Actor list add

print out of plant from mixed TArray (derived class)

The inheritance hierarchy follows your project

Caster.zip (20.0 KB)

Regenerate project to test (5.4) or just look at the files in the source folder

Thank you for your answer, but I was more asking about Blueprint casting. The thing is that there can be a bunch of classes that will inherit from UBaseDataAsset in the future, but then I would need to have a c++ function for each one of them to cast and return to access their individual properties in Blueprints. It may be not a big problem, but the solution seems a little messy.

Also, since my TArray of UBaseDataAsset located in Inventory class I have to include all UBaseDataAsset children header files so that Inventory can return just the specific types.

Blueprint casting works the same way just cast an array element to the target class that inherits from the data asset. This will of course introduce hard references to the casted classes.

You could in theory have a basic uobject that holds data and use interfaces to set / get data that could work without casting.

Not sure what I am doin wrong here exactly.



if your data asset is in c++ then try exposing it as Blueprintable and as BlueprintType

UCLASS(Blueprintable, BlueprintType)
class YOUR_API UPlantDataAsset : public UBaseDataAsset

1 Like

Big thanks! Now it works

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.