继承自 FTableRowBase
USTRUCT(BlueprintType)
struct FSimpleStruct :public FTableRowBase
{
GENERATED_USTRUCT_BODY()
public:UPROPERTY(EditAnywhere)
FString name;
UPROPERTY(EditAnywhere)
int32 health;
UPROPERTY(EditAnywhere)
UTexture2D* icon;
};
选择 FSimpleStruct 结构体
FindRow
UserInfoDataTable = LoadObject<UDataTable>(this, TEXT("DataTable'/Game/CPPFunction/DataDrive/DT_UserInfo.DT_UserInfo'"));
if (UserInfoDataTable)
{
for (FName RowName : UserInfoDataTable->GetRowNames())
{
FSimpleStruct* UserInfo = UserInfoDataTable->FindRow<FSimpleStruct>(RowName, TEXT("name"));
if (UserInfo)
{
UKismetSystemLibrary::PrintString(GetWorld(), UserInfo->name);
}
}
}
else
{
UKismetSystemLibrary::PrintString(GetWorld(), TEXT(" Not Find DataTable!"));
}
GetRowMap
UserInfoDataTable = LoadObject<UDataTable>(this, TEXT("DataTable'/Game/CPPFunction/DataDrive/DT_UserInfo.DT_UserInfo'"));
if (UserInfoDataTable)
{
for (auto it : UserInfoDataTable->GetRowMap())
{
FString RowName = (it.Key).ToString(); FSimpleStruct* UserInfo = (FSimpleStruct*)it.Value;
UKismetSystemLibrary::PrintString(GetWorld(), FString::Printf(TEXT("%s %s"), *RowName, *UserInfo->name));
}
}
AddRow
UserInfoDataTable = LoadObject<UDataTable>(this, TEXT("DataTable'/Game/CPPFunction/DataDrive/DT_UserInfo.DT_UserInfo'"));
if (UserInfoDataTable)
{
FSimpleStruct* UserInfo = new FSimpleStruct();
UserInfo->name = TEXT("Lily");
UserInfo->health = 80;
FName RowName = TEXT("Player3");
UserInfoDataTable->AddRow(RowName, *UserInfo);
}
csv 文件
---,name,health,icon
Player4,"马克","200","Texture2D'/Game/FourEvilDragonsHP/Textures/DragonTheUsurper/BlueHPTex.BlueHPTex'"
Player5,"冉冰","90","Texture2D'/Game/FourEvilDragonsHP/Textures/DragonTheSoulEater/BlueHPTex.BlueHPTex'"
Player6,"墨城","150","None"
填充现有的 DataTable
UserInfoDataTable = LoadObject<UDataTable>(this, TEXT("DataTable'/Game/CPPFunction/DataDrive/DT_UserInfo.DT_UserInfo'"));
if (UserInfoDataTable)
{
FString CSVPath = FPaths::ProjectDir() + TEXT("DataDrive/UserInfo.csv");
CSVPath = FPaths::ConvertRelativePathToFull(CSVPath);
if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*CSVPath))
{
UDataTableFunctionLibrary::FillDataTableFromCSVFile(UserInfoDataTable, CSVPath);
}
}
生成 UDataTable
UDataTable* ADataDriveActor::CreateDataTableFromCSV()
{
FString CSVPath = FPaths::ProjectDir() + TEXT("DataDrive/UserInfo.csv");
CSVPath = FPaths::ConvertRelativePathToFull(CSVPath);
if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*CSVPath))
{
UKismetSystemLibrary::PrintString(GetWorld(), *CSVPath);
FString CSVData;
FFileHelper::LoadFileToString(CSVData, *CSVPath);
UDataTable* DT_UserInfo = NewObject<UDataTable>(GetTransientPackage(), FName(TEXT("DT_UserInfo2")));
DT_UserInfo->RowStruct = FSimpleStruct::StaticStruct();
DT_UserInfo->CreateTableFromCSVString(CSVData); return DT_UserInfo;
}
return nullptr;
}
GetTableAsCSV
UserInfoDataTable = LoadObject<UDataTable>(this, TEXT("DataTable'/Game/CPPFunction/DataDrive/DT_UserInfo.DT_UserInfo'"));
if (UserInfoDataTable)
{
FString CSVString = UserInfoDataTable->GetTableAsCSV();
FString CSVPath = FPaths::ProjectDir() + TEXT("DataDrive/UserInfo2.csv");
FFileHelper::SaveStringToFile(CSVString, *CSVPath, FFileHelper::EEncodingOptions::ForceUTF8);
}
省略
手机扫一扫
移动阅读更方便
你可能感兴趣的文章