Download

How to properly use DataTables in C++?

I have played around with DataTables in blueprints with not much of a problem. I can easily reference a specific DataTable asset and get individual rows and such. However, I am trying to get the same thing to work with C++. I have defined a USTRUCT that the DataTable will use, but I have no clue what is the proper way to acquire an instance of the DataTable in code. Ideally, I want to create the DataTable in the editor (which works so far), and then reference that instance of the DataTable in a C++ class in order to access individual rows and manipulate information.


#pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Engine/DataTable.h"
#include "EngineMinimal.h"
#include "ItemFactory.generated.h"

USTRUCT(BlueprintType)
struct FItemTableRow: public FTableRowBase
{
    GENERATED_BODY()

public:
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category= "Item Basic Info")
    FString ItemName;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Basic Info")
    FString ItemDescription;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Visual Info")
    UTexture2D* ItemIcon;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Visual Info")
    UStaticMesh* ItemMesh;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Visual Info")
    FVector collisionBoxSize;

    // Pickup Properties
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Pickup Info")
    bool isPickup;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Pickup Info")
    bool spawnsEffect;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Pickup Info")
    uint8 MaxStackSize;

    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Pickup Info")
    UParticleSystem* particleEffect;

    // Use Properties
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Item Use Info")
    bool isUse;


    FItemTableRow()
    {
        // Constructor Code...
    }



};

/**
 *
 */
UCLASS()
class UItemFactory : public UObject
{
    GENERATED_BODY()

        UPROPERTY(VisibleAnywhere)
        UDataTable* itemTable;

public:
    UItemFactory();

};

In the above example, I intend to create some sort of Item Factory class that uses the DataTable to initialize game items.
Also, here is the tutorial I tried to follow but got stuck on: Data Driven Gameplay Elements | Unreal Engine Documentation