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的更多相关文章

  1. 用NPOI实现导入导出csv、xls、xlsx数据功能

    用NPOI实现导入导出csv.xls.xlsx数据功能   直接上代码 首先定义一个接口   如果需要直接操作文件的话,就自己在封装一次 然后定义csv类的具体实现 这个需要引入命名空间LumenWo ...

  2. Mysql 导入导出csv 中文乱码

    这篇文章介绍了Mysql 导入导出csv 中文乱码问题的解决方法,有需要的朋友可以参考一下   导入csv: load data infile '/test.csv' into table table ...

  3. 学习 MySQL中导入 导出CSV

    学习 MySQL中导入 导出CSV http://blog.csdn.net/sara_yhl/article/details/6850107    速度是很快的 导出 select * from t ...

  4. mysql SQLyog导入导出csv文件

    1.选择数据库表 --> 右击属性 --> 备份/导出 --> 导出表数据作为 --> 选择cvs --> 选择下面的“更改” --> 字段 --> 变量长度 ...

  5. PHP导入导出csv文件 Summer-CSV

    2017年11月9日09:25:56 根据项目实践总结的一个类文件, mac/win下没乱码 简体中文 默认从gb2312转到utf-8 https://gitee.com/myDcool/PHP-C ...

  6. [转]PL/SQL Developer 导入导出csv文件

    PL/SQL Developer 可以导入或者导出CSV文件. 导入CSV文件步骤: 1.选择tools->text importer.... 2.选择第二个Data to oracle选项卡, ...

  7. Bash中使用MySQL导入导出CSV格式数据[转]

    转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下:   select * from test_inf ...

  8. MySQL 导入导出 CSV 文件

    导入 导出 清空表中的所有数据 注意事项 常见问题 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-pri ...

  9. mysql导入导出.csv格式数据

    window下导入数据: LOAD DATA INFILE "C:\\1.csv" REPLACE INTO TABLE demo CHARACTER SET gb2312 FIE ...

随机推荐

  1. Django的form组件基本使用——生成标签

    from django.contrib import admin from django.urls import path from app01 import views urlpatterns = ...

  2. QT程序打包成多平台可执行文件

    一.简述 QT项目开发完成后,需要打包发布程序,在实际生产中不可能把源码发给别人,所以需要将源码打包正可执行文件或者安装程序. 二.设置应用图标 把 ico 文件放到源代码目录下,在QT项目中的'.p ...

  3. matlab纹理映射之地球

    %地球 cla reset; load topo; [x,y,z] = sphere(45); s = surface(x,y,z,'facecolor','texturemap','cdata',t ...

  4. Redis集群的搭建及与SpringBoot的整合

    1.概述 之前聊了Redis的哨兵模式,哨兵模式解决了读的并发问题,也解决了Master节点单点的问题. 但随着系统越来越庞大,缓存的数据越来越多,服务器的内存容量又成了问题,需要水平扩容,此时哨兵模 ...

  5. 网络层协议、ARP攻击

    一.IP数据包格式 二.ICMP协议介绍 PING命令 三.ARP协议介绍 四.ARP攻击原理 一.IP数据包格式 网络层的功能 定义了基于IP协议的逻辑地址 连接不同的媒介类型 选择数据通过网络的最 ...

  6. php 圆角图片处理

    /** * 把图片转换成圆角 * @param string $imgpath * @param int $radius * @return resource */ public function r ...

  7. MapReduce原理深入理解(一)

    1.MapReduce概念 1)MapReduce是一种分布式计算模型,由Google提出,主要用于搜索领域,解决海量数据的计算问题. 2)MapReduce是分布式运行的,由两个阶段组成:Map和R ...

  8. 【PHP数据结构】线性表?顺序表?链表?别再傻傻分不清楚

    遵从所有教材以及各类数据结构相关的书书籍,我们先从线性表开始入门.今天这篇文章更偏概念,是关于有线性表的一个知识点的汇总. 上文说过,物理结构是用于确定数据以何种方式存储的.其他的数据结构(树.图). ...

  9. 动态查看及加载PHP扩展

    在编译并完成 php.ini 的配置之后,我们就成功的安装了一个 PHP 的扩展.不过, PHP 也为我们提供了两个在动态运行期间可以查看扩展状态以及加载未在 php.ini 中进行配置的扩展的函数. ...

  10. pandas学习小记

    pandas操作整理 导入数据: pd.read_csv(filename):从CSV文件导入数据 pd.read_table(filename):从限定分隔符的文本文件导入数据 pd.read_ex ...