C++: 建立DataTable

我的需求:在UE4游戏工程里建立4种自定义表格
参考文档: UE4.11 引擎自带的代码文件 GameplayTagsManager.h

/** Simple struct for a table row in the gameplay tag table */
USTRUCT()
struct FGameplayTagTableRow : public FTableRowBase
{
    GENERATED_USTRUCT_BODY()

    /** Tag specified in the table */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=GameplayTag)
    FString Tag;

    /** Text that describes this category - not all tags have categories */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=GameplayTag)
    FText CategoryText;

    /** Constructors */
    FGameplayTagTableRow() { CategoryText = NSLOCTEXT("TAGCATEGORY", "TAGCATEGORYTESTING", "Category and Category"); }
    FGameplayTagTableRow(FGameplayTagTableRow const& Other);

    /** Assignment/Equality operators */
    FGameplayTagTableRow& operator=(FGameplayTagTableRow const& Other);
    bool operator==(FGameplayTagTableRow const& Other) const;
    bool operator!=(FGameplayTagTableRow const& Other) const;
};

我的头文件 WeaponDataTable.h

#pragma once
#include "WeaponDataTable.generated.h"

USTRUCT()
struct FWeapon_Entry : public FTableRowBase
{
    GENERATED_BODY()
    
        /** Tag specified in the table */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FString Tag;

        /** Text that describes this category - not all tags have categories */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
            FText CategoryText;
    
        /** Text that describes this category - not all tags have categories */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
            FText CategoryText1;

        /** Text that describes this category - not all tags have categories */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
            FText CategoryText2;

        /** Text that describes this category - not all tags have categories */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
            FText CategoryText3;
};


USTRUCT()
struct FWeapon_Client : public FTableRowBase
{
    GENERATED_BODY()

        /** Tag specified in the table */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FString Tag;

    /** Text that describes this category - not all tags have categories */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FText CategoryText;
};

USTRUCT()
struct FWeapon_Spell_Entry : public FTableRowBase
{
    GENERATED_BODY()

        /** Tag specified in the table */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FString Tag;

    /** Text that describes this category - not all tags have categories */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FText CategoryText;
};

USTRUCT()
struct FWeapon_Spell_Client : public FTableRowBase
{
    GENERATED_BODY()

        /** Tag specified in the table */
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FString Tag;

    /** Text that describes this category - not all tags have categories */
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = GameplayTag)
        FText CategoryText;
};

我的对应的cpp文件

#include "YourProjName.h" //你的项目头文件
#include "WeaponDataTable.h"

最终效果


C++: 建立DataTable_第1张图片
看,自定义表格成功

结论:

数据表在代码层面也是结构体,明白这一点,那么就不难理解上面代码结构体名和最终数据表可选类型的对应关系。本帖将要推出

  1. 用蓝图实现数据表
  2. C++细说数据表内容

你可能感兴趣的:(C++: 建立DataTable)