【UE4 C++】 Datatable 读写、导入导出 CSV/Json

Datatable 读取行数据

1. 创建结构体

  • 继承自 FTableRowBase

    USTRUCT(BlueprintType)
    struct  FSimpleStruct :public FTableRowBase
    {
    	GENERATED_USTRUCT_BODY()
    public:
    
    	UPROPERTY(EditAnywhere)
    		FString name;
    
    	UPROPERTY(EditAnywhere)
    		int32 health;
    
    	UPROPERTY(EditAnywhere)
    		UTexture2D* icon;
    };
    

2. 创建 Datatable

  • 选择 FSimpleStruct 结构体

    image

3. 读取行数据

  • 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));
    	}
    }
    

Datatable 写入行数据

  • 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

  • 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;
    }
    

导出 CSV

  • 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);
    }
    
  • 原博客地址

导入导出Json

省略

posted @ 2021-05-08 17:13  砥才人  阅读(5841)  评论(4编辑  收藏  举报