Back to the blog

UE4 C++ Data Assets

Hello again,

Today, I would like to write about the engine we are using in our new project, and that would be Unreal Engine 4.

We’ve been experimenting with the engine lately to learn all of its features, and today I discovered a nice one, I don’t know if UDK had this but I think it could come in handy.

I’m talking about the Data Asset. Data Assets are basically a way to have a static asset inside the Content Browser to populate it with our own data, for example we could have an items database if we were making an RPG game which we can access anywhere in the game without having to instantiate it directly, the possibilities are endless.

So let’s cut right to the chase and open the Unreal Editor.

If you go to the Content Browser and click on Create, go to the category Miscellaneous and you will find a bunch of classes and the one that interest to us right now is the one called Data Asset:

tutorial1

Once we click on it, a popup window will show up asking us for the asset class we want to create the asset from. So let’s Cancel the action for now, create a new C++ class and choose the parent class as DataAsset (mine’s going to be called “ItemsDatabase”).

When you create the class open it with Visual Studio and you can write something like this:

USTRUCT()
struct FItemInfo{

	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere)
	FString itemName;

	UPROPERTY(EditAnywhere)
	UTexture2D* itemThumbnail;

	UPROPERTY(EditAnywhere)
	UBlueprint* itemBlueprint;

	UPROPERTY(EditAnywhere)
	FColor itemColor;
};

/**
 * Items Database DataAsset, here we can save all of our game items
 */
UCLASS()
class PROJECTW_API UItemsDatabase : public UDataAsset
{
	GENERATED_BODY()

	UPROPERTY(EditAnywhere)
	TArray<FItemInfo> items;

};

Compile and run the Editor again, if you create a new Data Asset in the Content Browser, the popup window asking for a Data Asset class to pick will show your new class:

tutorial1-1

Go ahead and select your new class and it will create a new Asset, I called mine “ItemsDB”. Double click it and a nice window will show up with the array that we just added in our class and once we click on the (+) icon to add a new element we can just fill all the data we defined in our struct.

tutorial1-2

This way you can have all your items information in one asset that your designers can modify in a friendly way and that you can refer to anywhere in your game, just put a UPROPERTY reference in whatever class you want to access it from and you can use all the info you filled from the editor.

I hope this has been useful; if you have any questions, pleas feel free to ask in the comments section.

Also, I would like to thank the UE4┬ácommunity, they guided me today in the right direction to learn about this, I was kind of lost trying to figure out a way to store the data this way, here’s the link to the forums where they helped me; you can find more valuable information there.

Cheers!

Published by: Juan Camilo