skip to Main Content

Consuming Data Tables

In this tutorial, you’re going to see how you can consume Data Tables using C++. To create a Data Table you need a struct, so for the first part of this tutorial I’m going to create a struct in C++ code.

Creating the necessary Struct

Create a struct and in the header file type in the following variables(in case you don’t quite remember how to create a struct, check out this post):

Don’t forget to edit the USTRUCT() macro to: USTRUCT(BlueprintType).

Now, we should proceed by creating and populating the actual Data Table.

Creating and populating a Data Table

Once you create the above struct, compile your code and switch to your Editor. Right click somewhere in the content browser and from the Miscellaneous category choose a Data Table and for a structure, choose the struct we created above as the following image suggests:

DataTableCreation

Once you’re done with that, populate your data table with whatever values you feel like. However, pay attention to the Row Name value, because this is what we’re going to use in order to retrieve values from the table. My tables looks like this:

datatable

Now that we populated the Data Table, the next and last step is to consume it somehow and access it’s values

Accessing the Data Table Values

In order to gain access to the Data Table values I created a new C++ class which inherits from the Actor class. In the header file, I included the “Engine/DataTable.h” file and as a property, I added the following:

UPROPERTY(EditAnywhere)
UDataTable* DataTable;

Once I’m done with that, I switched to the source file of my class and right after the Super::BeginPlay() I typed in the following code:

That’s it! The last things we need are:

  • To make a Blueprint based on the above class
  • Hookup the Data Table we created in the Blueprint properties as seen in the image below:

table_hookup

Place your Blueprint into your level and press play, my output is the following one:

datatable_output

There is another way to access the data table. You can copy the reference from your editor and use a constructorshelper to instantiate it into your class constructor, however I’ve read somewhere in the Unreal Engine forums that this approach may result to problems. The suggested approach is to expose the property like we did in this post.

Avatar photo

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top