How do I refer to a datatable's object in code?

I’ve been trying to pick apart Data Driven Gameplay Elements | Unreal Engine Documentation and make my own example to be sure I understand the process, and I’m stuck on one step: I’ve made some arbitrary enum EMyEnum to hold a unique value, and a struct to hold each row of data from my table:


     USTRUCT(BlueprintType)
     struct FMyEnumDataRow : public FTableRowBase
     {
         GENERATED_USTRUCT_BODY()
             UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
             EMyEnum EnumValue;
     };

I’ve already imported my CSV, used my struct as the data table row type, and verified that the data imported correctly by opening the data table (which I just named MyEnumTestDT) in the editor and viewing its contents.

Where I’m stuck is the next step, namely obtaining a reference to MyEnumTestDT in code so I can run searches across it. Once I have that reference (presumably as a UDataTable), I know I can find data by doing something like:


     int8 rowID;
     FMyEnumDataRow  = UDataTable->FindRow<rowID>

However, that’s all predicated on actually finding a reference to the data table in the first place. So how, given the presence of some UDataTable MyEnumTestDT in the content folder, do I obtain a reference to that data table in code?

You need to get the refrence to the Data Table.


	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Data Tables")
		UDataTable* MyDataTable;

ANd then you can do something like this.


	UMyDataSingleton* DataTabels = UMyDataTablesLibrary::GetDataSingleton(bIsValid);
	if (DataTabels && bIsValid)
	{
		FMyTableStruct* Item = DataTabels->MyDataTable->FindRow<FMyTableStruct>(FName(*FString::FromInt(ItemRowID)), "", true);
		if (Item)
		{
			return *Item;
		}
		else {
			return FMyTableStruct();
		}
	}

Hope this helps.

1 Like

Hm okay, is the implication that there is some object that actually exists within the level who I hand a reference to the desired table in the editor?

In the case you are seeing above am refrencing a Singleton Class.
wrote a Wiki on this .

The Singleton is a class that can only have 1 instance.
It is not necesary but it insures you only allocate memory for the data once.

Then you can Create a Blueprint of the Singleton class and set the desierd data table in editor yes.
It is possible to set the Singleton by Searcing “Single” under General in your Project Settings.

Ooh perfect, thank you!! That clears up a lot :slight_smile:

You are welcome :).