Download

How to get my DataAsset in C++?

Hi guys. I am new to Unreal Engine and struggling with accessing to my DataAssets in C++.

I made a simple game that Player has to pick up items which are spawned per 2-5 seconds, to recover his health. After completing that, I thought I better add some more items not just simgle, like speed-up item, more recovery item, etc. And so I made C++ class named SpawnableItem_DataAsset which derives from UDataAsset, and actual instances from the data assets (Are they blueprints? I made them by right clicking mouse and selecting Miscellaneous > DataAsset > SpawnableItem_DataAsset in Content browser), which now stores data for an item, such as its actor, its spawn rate, and so on. Then, I added an SpawnableItem_DataAsset array variable to my actor blueprint to spawn items, and made nodes to select one of them and spawn it. It worked! But the event graph is messed up, so I decided to translate nodes in the event graph to C++.

So, first, in my C++ class to spawn items, I added these as below.




UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (MustImplement = SpawnableItemInterface))
TArray<TSubclassOf<USpawnableItem_DataAsset>> SpawnableItems;

UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (MustImplement = SpawnableItemInterface))
TArray<USpawnableItem_DataAsset*> SpawnableItems2;


But none of those worked. No compile error, but I cannot get my data assets with them.
With TArray<USpawnableItem_DataAsset*>, I can select my defined data asset from the editor, but when I select it, the value is still None.
Is anyone knows how to solve this? Thanks.

Did you register your data assets with the Asset Manager?

I’m not sure what you mean when you say that “when I select it, the value is still None.” Where are you trying to select it?

In c++ I dont think you can use a editor created data asset (those only work for blueprints), at least not to declare a variable. You must create the asset in c++ (a struct or class or array etc etc)

I can’t quite make out what it is you’re doing, but the first thing to note is that you can’t (well, you can but you shouldn’t) spawn ‘instances’ of data assets. They are design to be used as static data. You don’t typically subclass them into blueprints either (the editor shouldn’t even allow you to do that).

It sounds like these items should be regular blueprints, and not data assets.

Thank you guys for your replying.

I made just a Data asset and I am not sure what Asset Manager is. Do I need to register my assets to it in order to access them in C++?

About selecting, I added a C++ script which has UPROPERTYs as I said, and made a blueprint from the script. In that blueprint(double click the blueprint), I can see the selection menu as below.

BatteryMan - Unreal Editor 2020_06_04 3_38_57.png

But I cannot select any SpawnableItem_DataAsset although I have 2 datas which I made from Miscellaneous > DataAsset > SpawnableItem_DataAsset.

The data has an actor and a spawn rate(How rare it is. This is used to choose one to spawn.). I want to take these actor data from the asset data, and spawn it as an actor using the spawn rate in C++.

In my case, because I only have 2-3 items. it is ok to just make 2-3 different blueprints. but What if I have dozens of them, such that they shares the same type of data(e.g. they have an actor and spawn rate), but values of their data are different(different actor, different spawn rate value)? I thought DataAsset exists for this.

Before I came up with DataAsset, I tried Interface. I made c++ interface, and made blueprints which inherit form the interface. But What I needed is data before spawn, such as a spawn rate, so it was a bad idea. I couldn’t use datas of an actor before spawning it.

Just in case you know about Unity, I moved to Unreal form Unity and am looking for equivalent of ScriptableObject.

If its a C++ data asset created from the editor, I believe you have to include or forward declare it in your other c++ files that want to use it. It would also help if you posted the c++ code of the asset so we can make sure its properly setup. Macros have to be correct into order to be able to use it in any type of blueprint.

You also have the option to not use unreal engine data assets and you can just create the struct yourself directly in c++ (and you can expose them to blueprints)

Google “UE4 c++ DataAsset” and you find plenty examples…

This is my data asset script.

SpawnableItemDataAsset.png
And then in my spawner, data assets are stored in SpawnableItemDataArray and will be spawned like this.


That event graph is a bit long, so I am trying to write it in C++ instead of visual scripting. But as I said, if I just declare TArray of TSubclassOf<USpawnableItem_DataAsset> with UPROPERTY in C++, to access to the data, I cannot attack my data.

I have never thought of struct, I will look for it later. Thanks.

What I found mostly is how to write data assets in C++ and use it in blueprint…

I also found how to use asset data from Asset Manager, but it is a bit complex for my tiny project since I have to a lot of things such as changing Asset Manager settings, changing DefaultEngine.ini, loading assets from script, etc.

But I found ARPG project useful as an example of data management using Asset Manager. I am downloading that now and trying to learn that anyway. Thanks.