【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 ...
随机推荐
- .NetCore 项目在服务器打包失败解决
错误描述:NuGet警告 NU3037 NU3028 原因:Nuget无法访问到json所在的网络 2021年1月31日更新:更好的方法 把自动生成的Dockerfile内的AS build 替换成官 ...
- Django的模板文件的路径设置
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS' ...
- Mysql常用sql语句(6)- limit 限制查询结果的条数
测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 实际工作中,我们的数据表数据肯定都是万级别的,如 ...
- MongoDB 常见问题 - 解决找不到 mongo、mongod 命令的问题
问题背景 成功安装 MongoDB 后执行 Mongo.MongoD 显示找不到命令 解决方案 echo 'export PATH="/usr/local/opt/mongodb-commu ...
- php-fpm进程数控制
一.名词解释 CGI是Common Gateway Interface(通用网管协议),用于让交互程序和Web服务器通信的协议.负责处理URL的请求,启动一个进程,将客户端发送的数据作为输入,有Web ...
- RMI源码调试
看RMI漏洞时候,对其漏洞原理并不是很理解,所以简单调试了下源码加强下漏洞理解 由于要调试到RegistryImpl_Stub这种动态类,刚开始用的源码版本是JDK8u141,后来发现源码有些地方进行 ...
- HDU1548 Building Roads
A strange lift Description There is a strange lift.The lift can stop can at every floor as you want, ...
- PHP中使用DOMDocument来处理HTML、XML文档
其实从PHP5开始,PHP就为我们提供了一个强大的解析和生成XML相关操作的类,也就是我们今天要讲的 DOMDocument 类.不过我估计大部分人在爬取网页时还是会喜欢用正则去解析网页内容,学了今天 ...
- Docker系列(15)- Commit镜像
docker commit 提交容器成为一个新的副本,有点像套娃 # 命令和git原理类似 docker commit -m="提交的描述信息" -a="作者" ...
- Jmeter系列(21)- Jmeter录制手机App请求
前置知识点 Jmeter HTTP代理服务器每次点击启动录制,会往Jmeter的bin目录下生成相关证书,证书有效期是7天 录制前需要先看下证书过期没有,过期了,删除bin目录下的证书,即Apache ...