What is a Data Asset in Unreal Engine?
Data Assets are used in Unreal Engine to store information.
They offer an alternative to Data Tables. When Data Tables become very large, they may take too long to go through and read all the values inside them sequentially because of their many rows.
The concept behind data assets is to create a unique asset for every row that would typically be found in a data table. This approach makes them more challenging to organize and quickly review. However, they usually perform better and result in fewer crashes and errors.
Additionally, Data Tables are read-only elements, while in Data Assets, you can actually write information.
So let’s learn how to use them, so you can decide for yourself in which cases you want to use a Data Table or a Data Asset.
1. Create a Primary Data Asset
To use data assets, we first need to create a Primary Data Asset. You can think of this step as similar to creating a Structure for a Data Table. Just as we create the structure before the data table, the same principle applies here.
To create the Primary Data Asset, simply right-click or use the ‘+ Add‘ button in the Content Browser, then select ‘Blueprint Class‘.
Next, in ‘All Classes’ search for: PrimaryDataAsset.
2. Add variables to the Data Asset
First, open the Data Asset you just created.
Now, navigate to the variables panel and create variables as you would typically do inside Blueprints.
How do you create a Data Asset?
Now, we are going to create multiple data assets in Unreal Engine. Think of this step as similar to creating the rows in a data table.
To create a Data Asset, simply right-click or use the ‘+ Add’ button in the Content Browser, then select ‘Miscellaneous → Data Asset‘.
Then select the primary data asset you created in step 1 from the list. Alternatively, you can also use any of the default data assets provided by Unreal Engine.
Next, simply give the data asset a name corresponding to an item, an enemy, or attacks, or whatever you want to list, and open it.
Inside, you just have to fill in the variable data types that you created in step 2.
Once you are done, you can either repeat this step or simply duplicate the data asset to create as many data assets as you need.
Make sure to organize everything well in the folders of the Content Drawer, as the list of data assets grows, it can become increasingly difficult to find and organize them.
Using Data Assets in Blueprints
Data Asset Variables
To access the Data Asset variables, first create a variable and set its type to the primary data asset you created in step 1. In my case, it is named ‘Item Data Asset‘, but select the one you created and choose ‘Object Reference‘.
Compile the Blueprint and set the default value of the variable to the data asset you want to use. In this case, I will use the data asset for my banana item.
When getting or setting the data asset variable, you can easily access all the variables inside it, similar to how you would when casting. The same applies for accessing functions.
Data Asset Map
Instead of using a regular single variable, you can also use a map.
For example, you can use a map for creating an inventory. Then, connect the map to any map nodes, such as ‘add’, to add items to your inventory.
I will cover lessons on Maps in the future, so stay tuned!
You can learn more in the official documentation.
Or if you are a more advanced user, take a look at this roundtable discussion.
Hope this helps! Keep on creating!