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.

I’m replying to this old thread because it’s one of the first Google results on this topic.

All you have to do is use FObjectFinder, I’m pretty sure this image is enough to explain the process because it’s so straightforward.

The assets you see in the content finder can be either classes, like if you create a derived player controller blueprint, or they can be actual instances (objects). In the case of data assets, what you have are instances, so if you want to reference them from cpp you use FObjectFinder (as opposed to using FClassFinder to find a class like the player controller example).

Some people on this thread were mentioning using the asset manager, but I believe that may be poor advice. Depending on the complexity of your data asset, chances are it doesn’t have to be a primary asset! For run of the mill secondary assets, just load them as needed with FObjectFinder. I may be wrong but I think primary assets are only useful when you’re talking about hundreds if not thousands of assets that you have to preload to avoid hitches.

4 Likes

Where is the refrence point, you need a point of entry to your class and to your object, one to call on the class as refrence and the other to spawn.

You need another pointer towards the class as refrence to the object of the class.
Then you can use the spawn function with refrencing the class as an object class and the extra point as the object inside the class that you are trying to make visible on the map.

Edit
Opps I must be tired, I did not see how old this is.

Try to load your data asset with this: UAssetManager::GetPrimaryAssetObject | Unreal Engine Documentation

Or I tried this and it seems to work:

static ConstructorHelpers::FObjectFinder MyDataAsset(TEXT(“/Game/DataAssets/MyDataAsset”));

if (MyDataAsset.Succeeded())
{
UMyDataAsset* DataAsset = MyDataAsset.Object;
// Use the data asset here
}

none of these answer how to use DataAsset in c++

FindObject would be great if only I could include my dataasset header