本文使用 UE 4.26,引擎自带 ARPG 项目,从代码和编辑器两个方面记录一下 UE4 中的 DataTable,也就是数据表的用法。
一、DataTable 是什么
DataTable 就是数据表(以下简称 DT),也就是二维的,M 行 N 列的矩阵,如下图所示:
是一个 5 行(Row),三列(Col)的数据表。程序可以通过策划配置的数据表找到对应关系做相应的逻辑,对策划很友好。
二、编辑器中使用
2.1 创建一个数据表
在编辑器中,右键,Miscellaneous -> DataTable
即可创建一个 DT:
需要选择 Row (即列)的数据结构,比如选了 GameplayTagTableRow,就会创建一个如下所示的 DT:
新创建的 DT 默认是空的, 点击 Add 按钮可以创建一个默认行,如果 DT 的列结构是代码里的,则默认值在代码中设置;如果 DT 的列结构是资源,则在资源中设置。这个例子中 GameplayTagTableRow 是代码里写的,所以默认值在代码中设置(没有设置默认值,所以 Tag 默认是 None,DevComment 默认是空):
/** Simple struct for a table row in the gameplay tag table and element in the ini list */
USTRUCT()
struct FGameplayTagTableRow : public FTableRowBase
{
GENERATED_USTRUCT_BODY()
/** Tag specified in the table */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=GameplayTag)
FName Tag;
/** Developer comment clarifying the usage of a particular tag, not user facing */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category=GameplayTag)
FString DevComment;
/** Constructors */
FGameplayTagTableRow() {
}
FGameplayTagTableRow(FName InTag, const FString& InDevComment = TEXT("")) : Tag(InTag), DevComment(InDevComment) {
}
GAMEPLAYTAGS_API FGameplayTagTableRow(FGameplayTagTableRow const& Other);
/** Assignment/Equality operators */
GAMEPLAYTAGS_API FGameplayTagTableRow& operator=(FGameplayTagTableRow const& Other);
GAME