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. Python - 面向对象编程 - __init__() 构造方法

    什么是构造方法 在创建类时, 可手动添加一个   __init__() 方法,称为构造方法,这是一个实例方法 构造方法用于创建实例对象时使用,每当创建一个类的实例对象时,Python 解释器都会自动调 ...

  2. Python - 面向对象编程 - 小实战(3)

    需求 房子(House)有户型.总面积.家具名称列表:新房子没有任何的家具 家具(HouseItem)有名字.占地面积 席梦思(bed) 占地 4 平米 衣柜(bed) 占地 2 平米 餐桌(bed) ...

  3. JDK7u21反序列化详解

    目录 前言 环境 倒序分析 TemplatesImpl AnnotationInvocationHandler HashMap 总结 前言 听说jdk7u21的反序列化涉及的知识量很多,很难啃,具体来 ...

  4. 再谈java线程

    线程状态 描述 当线程被创建并启动之后,它既不是已启动就进入到了执行状态,也不是一直处于执行状态.在线程的声明周期中有六中状态. java api中java.lang.Thread.State这个枚举 ...

  5. 详解C3P0(数据库连接池)

    详解C3P0(数据库连接池) 快速索引 一.基本定义 二.使用C3P0(数据库连接池)的必要性 1.JDBC传统模式开发存在的主要问题 三.数据库连接池的详细说明 四.使用连接池的明显优势 1.资源的 ...

  6. xshell与小键盘问题

    有些程序员的键盘是带有小数字键的,在使用xshell中文版时就可能出现一些小状况,本集就同大家分析一下使用数字键盘出现乱码的情况怎么办. 图1:使用数字小键盘出现乱码 问题描述: 在xshell上用v ...

  7. Docker 安装 MySQL5.6

    方法一.docker pull mysql查找Docker Hub上的mysql镜像 #docker search mysql 这里我们拉取官方的镜像,标签为5.6 #docker pull mysq ...

  8. Powershell 命令行安装 Windows 作业系统

    使用 powershell 完全安装或重灌 windows 作业系统的正确姿势 note:完全使用 powershell 指令,绝非在 powershell 终端下键入传统的 cmd 指令使用传统的 ...

  9. iNeuLink硬件网关与iNeuOS工业互联网操作系统互联互通应用案例

    目       录 1.      应用概述... 2 2.      模拟硬件设备配置... 2 3.      iNeuLink硬件网关配置... 4 3.1           硬件介绍... ...

  10. Shell系列(38)- 数组操作→取值、遍历、替换、删除

    引言 在Linux平台上工作,我们经常需要使用shell来编写一些有用.有意义的脚本程序.有时,会经常使用shell数组.那么,shell中的数组是怎么表现的呢,又是怎么定义的呢?接下来逐一的进行讲解 ...