目的:在多个地方使用同一份结构体配置
C++定义结构体
USTRUCT(BlueprintType)
struct FXXX : public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "XXX")
float XXX;
}
注意:
- 类的元数据加上 BlueprintType
- 继承 FTableRowBase
- 需要编辑的参数加上 UPROPERTY 标签
- 参数的元数据加上EditAnywhere, BlueprintReadWrite
- Category 不能多层,例如 “A|B”
蓝图配置
新建DataTable:
选择你的结构体类型:
PS:选中某一行后直接F2就可以改名字了
蓝图使用
C++使用
内部存了从Name到结构体指针的Map,可以直接从Map拿完整的结构体
class UDataTable
: public UObject
{
TMap<FName, uint8*> RowMap;
virtual const TMap<FName, uint8*>& GetRowMap() const { return RowMap; }
virtual const TMap<FName, uint8*>& GetRowMap() { return RowMap; }
}
蓝图的实现也是类似,在创建的时候选的类型信息,会被保存下来,蓝图就根据这个类型信息,调用UScriptStruct::CopyScriptStruct,获取一份数据的复制出来。
bool UDataTableFunctionLibrary::Generic_GetDataTableRowFromName(const UDataTable* Table, FName RowName, void* OutRowPtr)
{
bool bFoundRow = false;
if (OutRowPtr && Table)
{
void* RowPtr = Table->FindRowUnchecked(RowName);
if (RowPtr != nullptr)
{
const UScriptStruct* StructType = Table->GetRowStruct();
if (StructType != nullptr)
{
StructType->CopyScriptStruct(OutRowPtr, RowPtr);
bFoundRow = true;
}
}
}
return bFoundRow;
}
也可以使用 GetAllRows 和 FindRow 函数,ContextString 给空就好了