DataAsset OOP is broken?

Why does this TMap array:

TMap<FName, TObjectPtr<UCustomClass>> DataAsset;

not seem to store instances of child classes derived from UCustomClass? It feels like it only accepts objects of the exact base class. I’m assigning these values in a Blueprint, so I would expect it to store instances of UCustomClass and still allow me to cast them to the appropriate child class in C++. However, when I attempt to cast to a child class, the result is nullptr. What could be causing this?

For example, this:

Cast<UChildCustomClass>(*DataAsset.Find("something"))

is always nullptr somehow

To help you, I think we would need more code context: your custom class definition (and its child), where you assign values in the BP, etc.

I think there is a method to check for the runtime type of an object, maybe you could test that method on the returned object of the Find method to see what’s exactly inside.

I think I forgot mentionning but these are PrimaryDataAsset classes and then I create DataAssets from them:

UCLASS()
class UCustomClass : public UPrimaryDataAsset
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere, Category = "Defaults")
	float Damage = 0.f;
};

And here’s the child PDA which is just containing few more variables:

UCLASS()
class UChildClass : public UCustomClass 
{
	GENERATED_BODY()

public:
	UPROPERTY(EditAnywhere)
	float RandomMultiplier = 0.f;
};

So I am able to create Data Assets from these 2 classes, and then select them in the array, but I can’t access (by casting) to the child at runtime in the array

I’m not sure if this is related but it seems there is some specific cases with inheritance of Primary Data Assets.

See the “Remarks” section of the following article, about inheritance :

Other forums questions seems to mention issues with Data Assets and inheritance:

I don’t know exactly what is the issue, but there could be some form of limitation somewhere

I’ve done this sort of thing plenty and it’s worked fine. Are you sure you’re looking it up with the same string that you’re adding it to the map?
Have you used your debugger to inspect DataAsset prior to accessing it? Are you sure you’re doing the lookup on an instance of the blueprint that is configured the way that you expect?

There are a lot of unknowns here, but overall the code that you’ve shared should work.

1 Like

Yea, you were right. I just didn’t realize the string wasn’t exactly the same somehow… Thanks for your help

Incorrect or overly complex inheritance hierarchies can lead to problems where DataAssets do not behave as expected. :thinking:

it’s not complex, I’m just creating an entire system from scratch so DataAssets were usefull for me

My pleasure. Sometimes it just takes someone asking a “dumb” question that forces you to check things you were otherwise assuming or being slightly blind to. It happens to everyone!