Using Data Tables to import spreadsheet data

In Unreal, Data Tables are a method of importing and using custom game data exported from a spreadsheet application. To do this, you first ensure that your spreadsheet follows some guidelines for format; additionally, you write a C++ struct that contains the data for one row of the spreadsheet. Then, you export a CSV file and select your C++ struct as the data type for that file.

The spreadsheet format

Your spreadsheet must follow some simple rules in order to correctly export to Unreal.

The very first cell must remain blank. After this, the first row will contain the names of the fields. These will be the same as the variable names in your C++ struct later, so do not use spaces or other special characters.

The first column will contain the lookup key for each entry. That is, if the first cell of the first item in this spreadsheet is 1, then in Unreal, you would use 1 to find that entry. This must be unique for every row.

Then, the following columns contain the values for each variable.

A sample spreadsheet

Let's create a simple spreadsheet to import into Unreal. It should look like this:

A sample spreadsheet

As mentioned in the previous section:

  • Column A contains the lookup keys for each row. The first cell is empty and the following cells have the lookup keys for each row.
  • Column B contains the values for the SomeNumber field. The first cell contains the field name (SomeNumber) and the following cells contain the values for that field.
  • Column C contains the values for the SomeString field. Just as with column B, the first cell contains the name of the field (SomeString) and the following cells contain the values for that field.

I'm using Google Spreadsheets—with this, you would click on File | Download as | Comma-separated values (.csv, current sheet) to export this to CSV. Most spreadsheet applications have the ability to export to the CSV format.

At this point, you have a CSV file that can be imported into Unreal. However, do not import it yet. Before we do that, we'll need to create the C++ struct for it.

The Data Table struct

Just as you created the actor class earlier, let's create a new class. Choose Actor as the parent class and give it a name such as TestCustomData. Our class won't actually inherit from Actor (and, for that matter, it won't be a class), but doing this allows Unreal to generate some code in the background for us.

Next, open the TestCustomData.h file and replace the entire file with the following code:

#pragma once

#include "TestCustomData.generated.h"

USTRUCT(BlueprintType)
struct FTestCustomData : public FTableRowBase
{
  GENERATED_USTRUCT_BODY()

  UPROPERTY( BlueprintReadOnly, Category = "TestCustomData" )
  int32 SomeNumber;

  UPROPERTY( BlueprintReadOnly, Category = "TestCustomData" )
  FString SomeString;
};

Notice how the variable names are exactly the same as the header cells in the spreadsheet—this is important, as it shows how Unreal matches columns in the spreadsheet to the fields of this struct.

Next, remove everything from the TestCustomData.cpp file, with the exception of the #include statements.

Now, switch back to the editor and click on Compile. It should compile without any issues.

Now that you've created the struct, it's time to import your custom spreadsheet.

Importing the spreadsheet

Next, simply drag your CSV file into the Content Browser tab. This will show a pop-up window, asking you to pick how you want to import the data and also what type of data it is. Leave the first drop-down list to Data Table and expand the second drop-down list to pick TestCustomData (the struct you just created).

Click on OK and it will import the CSV file. If you double-click the asset in the Content Browser tab, you'll see a list of the items that were in the spreadsheet:

Importing the spreadsheet

Querying the spreadsheet

You can query the spreadsheet in order to find particular rows by name.

We'll add this to our custom actor class, MyNewActor. The first thing we need to do is expose a field to a Blueprint, allowing us to assign a Data Table for our actor to use.

Firstly, add the following code just after the GENERATED_BODY line:

public:
  UPROPERTY( BlueprintReadWrite, EditAnywhere, Category = "My New Actor")
  UDataTable* DataTable;

The preceding code will expose the Data Table to Blueprint and allow it to be edited within Blueprint. Next, we'll fetch the first row and log its SomeString field. In the MyNewActor.cpp file, add this code to the end of the BeginPlay function:

if( DataTable != NULL )
{
  FTestCustomData* row = DataTable->FindRow<FTestCustomData>( TEXT( "2" ), TEXT(" LookupTestCustomData"  ) );
  FString someString = row->SomeString;
  UE_LOG( LogTemp, Warning, TEXT( "%s" ), *someString );
}

You will also need to add #include TestCustomData.h at the top of your MyNewActor.cpp file so that you can see the Data Table properties in it.

Compile the code in the editor. Next, open up the Blueprint you created from this actor class. Switch to the Class Defaults tab and find the My New Actor group (this should be at the very top). This should show a Data Table field that you can expand to pick the CSV file you imported.

Compile and save the Blueprint and then press Play. You should see the value of SomeString for the entry 2 logged to the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.54.168