引擎版本为4.24
在日常的UE4开发当中,我们需要读取DataTable类型的数据。
需要做的:
第一步:导入头文件
#include "Engine/Datatable.h"
第二步:设定读取路径
FString TableLoadPath=FString(TEXT("/Game/XXX"));
第三步:加载DataTable文件
//加载DataTable
UDataTable* ProductTable = LoadObject<UDataTable>(NULL, *TableLoadPath);
第四步:遍历DataTable
if (ProductTable != NULL)
{
for (auto it : ProductTable->GetRowMap())
{
FString rowName = (it.Key).ToString();
//FProduct为你的FStruct
FProduct* pRow = (FProduct*)it.Value;
//输出需根据你的FStruct进行调整
UE_LOG(LogTemp, Warning, TEXT("read by traversal --- RowName:%s, Name:%s, price:%d, introduct:%s"), *rowName, *(pRow->ProductName), pRow->ProductPrice, *(pRow->ProductIntroduce));
}
}