How to use Unreal Data Table assets to store Game Data

Introduction

Unreal Engine provides several tools for organizing and managing data within a game.

One such tool is the Data Table, which allows developers to define and store data in a structured format that can be easily accessed and modified.

In this article, we'll explore the basics of using data tables in Unreal Engine and demonstrate how they can be used to manage game data.

What are Data Tables?

Data Tables are a way of organizing and managing data within Unreal Engine. They are essentially spreadsheets that store data in rows and columns, with each row representing a specific object or instance of data.

Data Tables can be used to store any type of data that can be represented in a table format, including game settings, level layouts, character statistics, and more.

Unreal also has other assets like Data Assets, but we may check that out in another article in the future.

Creating a Data Table

To create a new Data Table in Unreal Engine, right-click in the Content Browser and select "Data Table" from the "Miscellaneous" section of the context menu.

A new Data Table asset will be created, and you can double-click it to open the Data Table Editor. Here, you can add new columns to the table and define the data type for each column.

For example, if you were creating a Data Table to store character statistics, you might add columns for "Health", "Mana", "Attack", "Defense", and so on.

Populating a Data Table

Once you have created a Data Table, you can populate it with data. To do this, simply add new rows to the table by clicking the Add Row button in the Data Table Editor.

Each row in the table represents an instance of data, so you'll want to add a new row for each object or instance of data that you want to store.

For example, if you were creating a Data Table to store character statistics, you might add a row for each character in your game, with the values for each column representing the character's stats.

One cool thing about Data Tables is that you can import JSON and CSV files directly into them, providing a direct way to connect external data into Unreal in a easy way.

Accessing Data from a Data Table

Once you have created and populated a Data Table, you can access the data from within your game code.

To do this, you'll first need to load the Data Table into memory, you do this for instance by storing the Data Table in a class property. But in the end is up to you.


UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, meta=(RowType ="QuodComboData"))
UDataTable* ComboDataTable;

Once the Data Table is loaded, you can access individual rows and columns using the "Get Data Table Row" function.

For example, if you had a Data Table that stored character statistics, you could load the Data Table into memory and then retrieve a specific character's stats by calling the Get Data Table Row function and passing in the character's name or ID.


// Get the datatable asset from where you store it
const UDataTable* ComboDataTable = UQuodDynamicAssetLoaderGlobals::GetAssetByName(GetOwner(), AttackDataTableName);

// Find a specific row in the datatable
const FQuodAttackData * attackDataEntry = ComboDataTable->FindRow(TEXT("Attack01"), TEXT("UQuodAttackDataComponent::GetAttackData"));

// Get all rows if you need them all
TArray AllRows;
ComboDataTable->GetAllRows(TEXT("UQuodComboUtils::GetAttackData"), AllRows);

One important thing, as you are seeing by the return type of these functions, is that a Data Table query will always return a struct of the type that the Data Table is based on.

Conclusion

Data Tables are a powerful tool for organizing and managing data within Unreal Engine like Data Assets or the Data Registry. By creating and populating Data Tables, you can store and manage a wide range of game data in a structured and easily accessible format.

Whether you're managing game settings, level layouts, character statistics, or any other type of data, Data Tables are a great way to keep your game organized and efficient.