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

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的更多相关文章
- 用NPOI实现导入导出csv、xls、xlsx数据功能
用NPOI实现导入导出csv.xls.xlsx数据功能 直接上代码 首先定义一个接口 如果需要直接操作文件的话,就自己在封装一次 然后定义csv类的具体实现 这个需要引入命名空间LumenWo ...
- Mysql 导入导出csv 中文乱码
这篇文章介绍了Mysql 导入导出csv 中文乱码问题的解决方法,有需要的朋友可以参考一下 导入csv: load data infile '/test.csv' into table table ...
- 学习 MySQL中导入 导出CSV
学习 MySQL中导入 导出CSV http://blog.csdn.net/sara_yhl/article/details/6850107 速度是很快的 导出 select * from t ...
- mysql SQLyog导入导出csv文件
1.选择数据库表 --> 右击属性 --> 备份/导出 --> 导出表数据作为 --> 选择cvs --> 选择下面的“更改” --> 字段 --> 变量长度 ...
- PHP导入导出csv文件 Summer-CSV
2017年11月9日09:25:56 根据项目实践总结的一个类文件, mac/win下没乱码 简体中文 默认从gb2312转到utf-8 https://gitee.com/myDcool/PHP-C ...
- [转]PL/SQL Developer 导入导出csv文件
PL/SQL Developer 可以导入或者导出CSV文件. 导入CSV文件步骤: 1.选择tools->text importer.... 2.选择第二个Data to oracle选项卡, ...
- Bash中使用MySQL导入导出CSV格式数据[转]
转自: http://codingstandards.iteye.com/blog/604541 MySQL中导出CSV格式数据的SQL语句样本如下: select * from test_inf ...
- MySQL 导入导出 CSV 文件
导入 导出 清空表中的所有数据 注意事项 常见问题 ERROR 1290 (HY000): The MySQL server is running with the --secure-file-pri ...
- mysql导入导出.csv格式数据
window下导入数据: LOAD DATA INFILE "C:\\1.csv" REPLACE INTO TABLE demo CHARACTER SET gb2312 FIE ...
随机推荐
- Linux常用命令 - nl命令详解
21篇测试必备的Linux常用命令,每天敲一篇,每次敲三遍,每月一循环,全都可记住!! https://www.cnblogs.com/poloyy/category/1672457.html 显示行 ...
- struts2执行流程和架构图
一.struts2执行流程 二.架构图 只需要编写黄色部分的代码:
- windows中对文件进行排序
右键->排序方式->更多->选择需要的项目
- 【C++周报】第一期2021-8-1
[C++周报]第一期 2021-8-1 这一期我们来看这道题目:https://vijos.org/p/1058 这道题是一道非常好的模拟题.题目如下: 描述 我们用文本处理器来处理一个特殊的文本文件 ...
- Shell系列(26)- 条件判断之两个文件比较
两个文件之间进行比较 测试选项 作用 文件1 -net 文件2 判断文件1的修改时间是否比文件2的新(如果新则为真) 文件1 -ot 文件2 判断文件1的修改时间是否比文件2的旧(如果旧则为真) 文件 ...
- 显式等待until传入自定义方法
WebDriverWait(driver,10).until(expected_conditions.element_to_be_clickable(ele)) 通过追踪代码,可以发现上面的eleme ...
- qt5 打包exe执行文件
1.pyinstaller 安装 :pip install pyinstaller 执行:pyinstaller -F -w --icon=logo.ico xx.py 打包后的文件 在 dist 下 ...
- prometheus+grafana实现服务监控
一.安装prometheus: 下载相应的版本 :https://prometheus.io/download/ 解压: Linux:tar -zxvf XXX.tar.gz windows:直接下载 ...
- SpringSecurity系列学习(一):初识SpringSecurity
SpringSecurity Spring Security是spring采用AOP思想,基于servlet过滤器实现的安全框架.它提供了完善的认证机制和方法级的 授权功能.是一款非常优秀的权限管理框 ...
- bootstrap inputfile 使用-上传,回显
近期用bootstrap 做前端的上传,功能涉及到上传时就是召网上的教程随便弄一搜一大把,但是做到修改页面时候不知道页面该如何回显,折腾了一阵子才完成遂记录下来希望能给看到的小伙伴有点启发吧. 首先是 ...