Can't get reference to blueprint data asset

I’ve set up a base ASHNPC actor c++ class which can be given a data asset as a way to store information about the npc.
I have a data asset that has been defined in c++ called UNPCDataAsset which is set to Blueprintable allowing me to create a BP data asset that inherits from UNPCDataAsset.

The npc actor expects to be given a object ref to the data asset. This is where the problem beings, I can only set the ref to the c++ file of the data asset and not the blueprint data asset that inherits from the c++ class.

// The Data Asset
UCLASS(Blueprintable)
class SHADOWS_API UNPCDataAsset : public UPrimaryDataAsset
{
	GENERATED_BODY()

public:

	UPROPERTY(EditAnywhere)
	int32 ID;

	UPROPERTY(EditAnywhere)
	FText DisplayName;

	UPROPERTY(EditAnywhere)
	TSoftObjectPtr<UTexture2D> Icon;

	UPROPERTY(EditAnywhere)
	TArray<FText> Speech;
};

// The NPC actor class
UCLASS(Blueprintable)
class SHADOWS_API ASHNPC : public AInteractableBase
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASHNPC();

protected:

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
	class UCapsuleComponent* CapsuleComponent = nullptr;

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
	class USkeletalMeshComponent* SkeletalMeshComponent = nullptr;

	virtual void BeginPlay() override;
	virtual void OnInteract(class ASHCharacter* caller) override;

	UPROPERTY(EditAnywhere)
	TSubclassOf<UNPCDataAsset> DataAssetObjRef;
	UPROPERTY()
	UNPCDataAsset* NPCData;
};

Below is a picture of the data asset that has been created in BP that inherits from the data asset class. Also is me trying to set the TSubClassOf to that blueprint but only having the option for the c++ class.

Any help would be greatly appreciated!

2 Likes

I had the same problem. What helped med alleviate the problem was to use TAssetSubclassOf instead.

It gives a soft class pointer, which then can be converted into a class via the Load Class Asset Blocking node. There are probably also other ways of loading it, but that worked for me.

Check this answer out for more in depth explanation:

Saved me a lot of time! THANK YOU

I actually believe that I found the answer to my problem.

I found that the issue I was that I was creating a DataAsset (Using Content Browser → Miscellaneous → DataAsset) and having that asset be a child of my C++ UDataAsset class.

The fix for me was instead of creating a DataAsset to inherit from my C++ class, I instead created a normal Blueprint to inherit form that C++ class.

I’m not sure exactly what is going on under the hood that is causing the difference here (If I’m being honest I didn’t really look into why this was the solution)

I’ve updated the post for you Chumble with my solution, hopefully it helps you!

To clarify, you simply replaced your TSubClassOf with a TAssetSubclassOf and were able to reference your BP data assets inheriting from the C++ data asset? I’m currently in this same scenario but using TAssetSubclassOf seems to make no difference for me…

I tried it out and confirmed that by creating a child blueprint class this way it causes it to be usable with the TSubclassOf. But I’m not really sure if this is the best approach … I’m not looking to create a blueprint class that can execute code, I simply want a data container (which was really why I was using data assets in the first place). I feel like there must be another solution to allow me to reference data assets created through Misc > Data Asset … gonna keep hunting. Thanks for your update though!

1 Like

Ended up just doing

MyDataAsset* DataAssetRef;

Since my data assets were created through Misc > Data Asset they were not truly “children” of my c++ data asset and therefore would not be found with SubclassOf.

2 Likes

You can create it all in Blueprints.
Right click choose Blueprint class → allclasses → DataAsset → PrimaryDataAsset
This will create a PrimaryDataAsset. You will then find this when you use Misc->Data asset.

You can then use a node like GetAssetsByClass to loop through all your assets and if you check the Search sub class check box you will get those as well
Take note of the _C at the end of the name

In my example below WeaponAsset_DA was created as a child of the GameAsset_DA

339940-one.png

339943-three.png