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. 移动端ios上下滑动翻页事件失效

    移动端开发过程中,在添加上下滑动事件时候,引入了最常用的移动端库zepto.js及其touch模块,有一种现象,安卓的手机没有问题,上下滑动翻页很正常 :但是到了ios上面,好啊,上下滑动会出现弹性滚 ...

  2. 洛谷P3768 简单的数学题解题报告

    $$\begin{eqnarray}&\sum_{i=1}^{n}\sum_{j=1}^{n}ij\gcd(i,j)\\&\sum_{d=1}^{n}\sum_{i=1}^{n}\su ...

  3. [考试总结]noip模拟44

    这个真的是一个 \(nb\) 题. 考试快要结束的时候,在机房中只能听到此起彼伏的撕吼. 啊---------- 然后人们预测这自己的得分. \(\color{red}{\huge{0}}\) \(\ ...

  4. Openswan支持的算法及参数信息:

    数据封装加密算法: algorithm ESP encrypt: id=2, name=ESP_DES, ivlen=8, keysizemin=64, keysizemax=64 algorithm ...

  5. vue项目 'node-sass'问题

    Cannot find module 'node-sass' 解决办法: 运行命令:cnpm install node-sass@latest 即可解决,( 网络差的同学可以选择重新下载no-modu ...

  6. SpringBoot异步使用@Async原理及线程池配置

    前言 在实际项目开发中很多业务场景需要使用异步去完成,比如消息通知,日志记录,等非常常用的都可以通过异步去执行,提高效率,那么在Spring框架中应该如何去使用异步呢 使用步骤 完成异步操作一般有两种 ...

  7. ECMAScript 2021(ES12)新特性简介

    简介 ES12是ECMA协会在2021年6月发行的一个版本,因为是ECMAScript的第十二个版本,所以也称为ES12. ES12发行到现在已经有一个月了,那么ES12有些什么新特性和不一样的地方呢 ...

  8. 【PHP数据结构】线性查找与二分查找

    欢迎来到查找的世界,在学习完各种数据结构之后,总算走到了这一步,不知道大家有什么感想呢?反正我是边学边忘,现在让我去说说图的那几个算法还是在蒙圈的状态中.不过学习嘛,就是一步一步的来,暂时搞不懂的东西 ...

  9. 深入HTML5第二天

    sub(subscripted下标标签)和sup(superscripted上标标签) 内联元素:inline element  span(范围标签):内联元素inline element 特性:没有 ...

  10. 3. Go并发编程--数据竞争

    目录 1.前言 2.数据竞争 2.1 示例一 2.1.1 测试 2.1.2 data race 检测 2.1.3 data race 配置 2.2 循环中使用goroutine引用临时变量 2.3 引 ...