[16/50] Use UDataTable to manage game data in Unreal

In one of our previous posts on the topic of data driven game play in Unreal Engine, we did a quick overview of Data Tables. As the name suggests, Data Tables are tabular records of structured data. And when we say structured, we mean every column has a specific type of data that it can accept.
Data Tables need a backing data structure which should be a UStruct
derived from FTableRowBase
. Within this struct all most all Unreal AActors
and UObject
are supported with relevant UPROPERTY
qualifiers.
Let's look at a simple example of how such a row could look like for a data table containing records of inventory items available in the game.
USTRUCT(BlueprintType)
struct FInventoryItem : public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LostFerry")
FText Name;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LostFerry", meta = (MultiLine=true))
FText Description;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LostFerry")
TSoftObjectPtr<UMaterialInterface> Image;
}
Once this struct is setup, we can create a Data Table with this structure in the editor by right-clicking on the content browser and selecting Data Table followed by the base row struct we just created.
After creating the table, the data can be either directly manipulated from within the editor after opening the data table or imported via a CSV or JSON file. We found that when object or class references are used, writing the correct text in the CSV or JSON can be very tedious and error prone, so we just use the drop downs within the interface provided by the editor to manage the data tables.
One of the constraints of the Data Tables is that they are designed to be read-only during game play which limits a lot use cases. For example, if in an inventory system we wanted to initially mark all items as locked such that the player cannot access them via a bool
field and, later, toggle that bool
field during the game play as the player unlocks the items - this is not possible in a clear cut way and nor is recommended.