码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2021-05-24 02:00:47      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:rate   sts   行数据   turn   结构   string   omcs   where   options   

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 结构体

    技术图片

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

省略

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

标签:rate   sts   行数据   turn   结构   string   omcs   where   options   

原文地址:https://www.cnblogs.com/shiroe/p/14745783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!