UE4动态加载数据表

 

.1.创建一个数据表

需要改成UTF-8编码

2.创建一个Actor

.h

UFUNCTION(BlueprintCallable, Category = "MySocket")
		TMap<int, FString> GetCsvContents(FString csvPath, int LieNum);

.cpp

#include "Runtime/Core/Public/Serialization/Csv/CsvParser.h"
#include "Runtime/Core/Public/Misc/Paths.h"
TMap<int, FString> AMyActor::GetCsvContents(FString csvPath, int LieNum)
{
	FString csvFile = FPaths::ProjectContentDir() + csvPath;
	TMap<int, FString> csvContent;

	if (FPaths::FileExists(csvFile))
	{
		FString FileContent;
		//Read the csv file  
		FFileHelper::LoadFileToString(FileContent, *csvFile);
		FCsvParser* csvfiles = new FCsvParser(FileContent);
		TArray<FString> t_str;
		TArray<TArray<const TCHAR*>> content = csvfiles->GetRows();
		for (TArray<const TCHAR*>& ite : content)
		{
			for (const TCHAR*& chr : ite)
			{
				//aa.Append(chr);
				t_str.Add(chr);
			}
		}

		/*for (int i = 0; i < t_str.Num(); i++)
		{
			UE_LOG(LogTemp, Warning, TEXT("Info= %s"), *t_str[i]);
		}*/

		int key = 0;
		for (int i = 0; i < t_str.Num(); i++)
		{
			if (i % LieNum == 0)
			{
				FString rowValue = t_str[i];
				for (int j = 1; j < LieNum; j++)
				{
					rowValue.Append("," + t_str[i+j]);
				}				
				csvContent.Add(key, rowValue);
				key++;
			}
		}
		return csvContent;

	}
	else
		return csvContent;
}

datatable

USTRUCT(BlueprintType)
struct  FAssetAttribute : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()
public:
	FAssetAttribute()
		: AssetName(LOCTEXT("CylinderBuilderName", "Cylinder"))
		, Category("123")
		, AssetDataPath(TEXT(StaticMesh'/Game/model/aa.aa'))
		, ICONpath(TEXT("AssetToolEdMode.BoxBrush"))
		, ToolTip(LOCTEXT("CylinderBuilderName", "Cylinder"))
		, Type(AssetType::A_StaticMesh)
		, netPath(TEXT("AssetToolEdMode.BoxBrush"))
		{
		}
public:
		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			FText AssetName;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			FString Category;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			FString AssetDataPath;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			FString ICONpath;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			FText ToolTip;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			AssetType Type;

		UPROPERTY(BlueprintReadWrite, EditAnywhere)
			FString netPath;
};
	FString tablePath = "DataTable'/MyAsset/DataAsset.DataAsset'";
	UDataTable* pDataTable = LoadObject<UDataTable>(NULL, *tablePath);
	TArray<FName> RowNames;
	FString ContextString;
	RowNames = pDataTable->GetRowNames();
	for (auto& name : RowNames)
	{
		FAssetAttribute* pRow = pDataTable->FindRow<FAssetAttribute>(name, ContextString);
		if (pRow)
		{
			//LogDebug("read by row name --- RowName:%s, ID:%d, Name:%s, HP:%d", *(name.ToString()), pRow->ID, *pRow->Name, pRow->HP);
		}
	}

 

 

### 在 Unreal Engine 5 中实现动态写入数据表的方法 在 Unreal Engine (UE) 的开发过程中,数据表(DataTable)是一种非常方便的数据存储结构。然而,默认情况下,Unreal Engine 并不支持运行时修改或保存 DataTable 数据到磁盘上[^1]。这是因为 DataTable 被设计为静态配置文件的一部分,在编译阶段加载并用于读取只读数据。 如果需要实现在运行时动态更新 DataTable,则可以通过以下几种方法来完成: #### 方法一:内存中的临时更改 可以创建一个自定义类继承 `UObject` 或其他合适的基类,并将其作为容器用来存储动态生成的数据。这些数据可以在游戏中实时更新而不影响原始的 DataTable 文件。当程序结束或者特定条件触发时,可以选择性地将此对象序列化成 JSON 字符串或其他格式以便后续处理。 ```cpp // MyDynamicData.h #include "CoreMinimal.h" #include "GameFramework/Actor.h" #include "MyDynamicDataRow.generated.h" USTRUCT(BlueprintType) struct FMyDynamicDataRow : public FTableRowBase { GENERATED_BODY() public: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Custom Data") FString Name; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Custom Data") int32 Value; }; UCLASS() class MYPROJECT_API AMyDynamicData : public AActor { GENERATED_BODY() public: UFUNCTION(BlueprintCallable, Category="Dynamic Data") void AddOrUpdateRow(FName RowName, const FMyDynamicDataRow& NewRow); }; ``` 上述代码片段展示了如何定义一个新的行类型以及关联的操作函数[^2]。 #### 方法二:利用 Save Game System 存储变化后的表格内容 另一种解决方案是采用 UE 提供的游戏存档机制——Save Game system 来持久化那些被改动过的记录项。每当有新的条目加入或是已有条目的属性发生变化的时候就调用相应的 API 将其转化为易于解析的形式比如数组形式再储存起来;等到下次重新启动应用后再依据之前保留下来的信息重建整个数据集。 需要注意的是这种方法不会真正意义上改变项目目录下的 .csv/.asset files ,而是另外开辟空间单独存放差异部分而已 。因此即使发生错误也不会破坏原有的素材资源链接关系等问题 。 --- #### 注意事项 由于涉及到复杂的逻辑控制流程再加上可能存在的性能开销等因素考虑 , 开发者应当谨慎评估实际需求场景下是否真的有必要引入这样的扩展能力进去 。 同时也要记得做好充分测试验证工作以确保最终效果满足预期目标要求 。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值