• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

UE4 C++ 动态读写Datable

武飞扬头像
飞起的猪
帮助1

序言:

         DataTable数据表在UE4中是一类重要的资产(Asset),引擎内置的函数并不支持运行时(Runtime)修改DataTable表,所以仿照源码取了相同的函数名称,但以下蓝图节点在打包之后仍然可以调用。

        从ue4 源码DataTable.h和DataTableFunctionLibrary.h二个类中不难发现,DataTable支持动态读写操作的,其中UDataTable::CreateTableFromCSVString()和UDataTable :: CreateTableFromJSONString()二个函数尤其值得注意,这二个函数并未被WITH_EDITOR宏包裹,也就是在非编辑器模式下,仍可被调用。而二种函数中的输入参数CSVSting和JSONString既可以通过本地磁盘文件获得,也可以使用HTTP网络传输的数据来获得。所以在获得CSVString/JSONString之后,调用这二个函数,即可实现向DataTable中写入数据。

       顾名思义,数据表就是以有意义且有用的方式将各种相关的数据归类的表格, 其中,数据字段可以是任何有效的 UObject 属性,包括资产引用。在设计师将 CSV 文件导入数据表前,程序员必须创建行容器以指示引擎如何解释数据。 这些数据表包含了列名,这些列名和基于代码的UStruct结构以及它的(子)变量一个一个地对应, 这个UStruct的结构必须继承自FTableRowBase才可以被导入器辨识。

     一般我们在设计游戏的时候都会用到大量的数据,如果在UE4里面直接修改会比较麻烦,这个时候就出现了数据表格,能够通过导入数据表格的形式来修改参数。

一、前期准备

创建数据表格,放入到Content目录下面命名为DataTableFile(后面会用到),根据你们自己来就行,,Datable格式为CSV(不懂可以查看百度)

学新通

数据类型与下面自定义的数据类型相同

学新通

二、自定义结构体

C 创建一个UObject的类,命名一个结构体,参数类型和名称可自己根据数据表格来命名。我们定义的结构体需要继承自FTableRowBase,因为只有这样虚幻才能识别数据表格。

  1.  
    // Fill out your copyright notice in the Description page of Project Settings.
  2.  
     
  3.  
    #pragma once
  4.  
     
  5.  
    #include "CoreMinimal.h"
  6.  
    #include "UObject/NoExportTypes.h"
  7.  
    #include"Engine/Classes/Engine/DataTable.h"
  8.  
    #include "MyObject.generated.h"
  9.  
     
  10.  
    /**
  11.  
    *
  12.  
    */
  13.  
     
  14.  
    USTRUCT(BlueprintType)
  15.  
    struct FYourCppStruct : public FTableRowBase
  16.  
    {
  17.  
    GENERATED_USTRUCT_BODY()
  18.  
     
  19.  
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "YourCppStruct")
  20.  
    int32 IntegerValue;
  21.  
     
  22.  
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "YourCppStruct")
  23.  
    float FloatValue;
  24.  
     
  25.  
    UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "YourCppStruct")
  26.  
    FString StingValue;
  27.  
     
  28.  
    };
  29.  
    UCLASS()
  30.  
    class DASD_API UMyObject : public UObject
  31.  
    {
  32.  
    GENERATED_BODY()
  33.  
     
  34.  
    };

三、导入Datatable,选择自定义的数据结构

       选择刚刚创建的自定义结构体YourCppStruct 

学新通

 四、C 创建一个BlueprintLibrary类

   创建蓝图库,以便能够全局调用

  1.  
    #include "CoreMinimal.h"
  2.  
    #include "Kismet/BlueprintFunctionLibrary.h"
  3.  
    #include "Engine/DataTable.h"
  4.  
    #include "HAL/PlatformFilemanager.h"
  5.  
    #include "Misc/FileHelper.h"
  6.  
    #include "Misc/Paths.h"
  7.  
    #include "Engine/DataTable.h"
  8.  
    #include "GenericArrayLibrary.generated.h"
  9.  
     
  10.  
     
  11.  
    //动态读取DataTable数据表格......
  12.  
    UFUNCTION(BlueprintCallable, DisplayName = "Fill Data Table from CSV String", Category = "DataTable")
  13.  
    static bool FillDataTableFromCSVString(UDataTable* DataTable, const FString& CSVString);
  14.  
     
  15.  
    UFUNCTION(BlueprintCallable, DisplayName = "Fill Data Table from CSV File", Category = "DataTable")
  16.  
    static bool FillDataTableFromCSVFile(UDataTable* DataTable, const FString& CSVFilePath);
  17.  
     
  18.  
    UFUNCTION(BlueprintCallable, DisplayName = "Fill Data Table from JSON String", Category = "DataTable")
  19.  
    static bool FillDataTableFromJSONString(UDataTable* DataTable, const FString& JSONString);
  20.  
     
  21.  
    UFUNCTION(BlueprintCallable, DisplayName = "Fill Data Table from JSON File", Category = "DataTable")
  22.  
    static bool FillDataTableFromJSONFile(UDataTable* DataTable, const FString& JSONFilePath);
  23.  
     
  24.  
    UFUNCTION(BlueprintCallable, DisplayName = "Get Table As CSV String", Category = "DataTable")
  25.  
    static void GetDataTableAsCSVString(UDataTable* DataTable, FString& CSVString);
  26.  
     
  27.  
    UFUNCTION(BlueprintCallable, DisplayName = "Get Table As CSV File", Category = "DataTable")
  28.  
    static void GetDataTableAsCSVFile(UDataTable* DataTable, const FString& CSVFilePath);
  1.  
    bool UGenericArrayLibrary::FillDataTableFromCSVString(UDataTable* DataTable, const FString& CSVString)
  2.  
    {
  3.  
    if (!DataTable || (CSVString.Len() == 0))
  4.  
    {
  5.  
     
  6.  
    return false;
  7.  
    }
  8.  
    // Call bulit-in function
  9.  
    TArray<FString> Errors = DataTable->CreateTableFromCSVString(CSVString);
  10.  
    if (Errors.Num())
  11.  
    {
  12.  
    // It has some error message
  13.  
    for (const FString& Error : Errors)
  14.  
    {
  15.  
    UE_LOG(LogTemp, Warning, TEXT("error1"));
  16.  
    }
  17.  
    return false;
  18.  
    }
  19.  
    UE_LOG(LogTemp, Warning, TEXT("true"));
  20.  
    return true;
  21.  
     
  22.  
    }
  23.  
     
  24.  
    bool UGenericArrayLibrary::FillDataTableFromCSVFile(UDataTable* DataTable, const FString& CSVFilePath)
  25.  
    {
  26.  
    FString CSVString;
  27.  
    if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*CSVFilePath))
  28.  
    {
  29.  
    // Supports all combination of ANSI/Unicode files and platforms.
  30.  
    FFileHelper::LoadFileToString(CSVString, *CSVFilePath);
  31.  
    }
  32.  
    else
  33.  
    {
  34.  
    UE_LOG(LogTemp, Warning, TEXT("error2"));
  35.  
    return false;
  36.  
    }
  37.  
    return UGenericArrayLibrary::FillDataTableFromCSVString(DataTable, CSVString);
  38.  
    UE_LOG(LogTemp, Warning, TEXT("success"));
  39.  
    }
  40.  
     
  41.  
    bool UGenericArrayLibrary::FillDataTableFromJSONString(UDataTable* DataTable, const FString& JSONString)
  42.  
    {
  43.  
    if (!DataTable || (JSONString.Len() == 0))
  44.  
    {
  45.  
     
  46.  
    return false;
  47.  
    }
  48.  
    // Call bulit-in function
  49.  
    TArray<FString> Errors = DataTable->CreateTableFromJSONString(JSONString);
  50.  
     
  51.  
    if (Errors.Num())
  52.  
    {
  53.  
    // It has some error message
  54.  
    for (const FString& Error : Errors)
  55.  
    {
  56.  
     
  57.  
    }
  58.  
    return false;
  59.  
    }
  60.  
     
  61.  
    return true;
  62.  
    }
  63.  
     
  64.  
    bool UGenericArrayLibrary::FillDataTableFromJSONFile(UDataTable* DataTable, const FString& JSONFilePath)
  65.  
    {
  66.  
    FString JSONString;
  67.  
    if (FPlatformFileManager::Get().GetPlatformFile().FileExists(*JSONFilePath))
  68.  
    {
  69.  
    // Supports all combination of ANSI/Unicode files and platforms.
  70.  
    FFileHelper::LoadFileToString(JSONString, *JSONFilePath);
  71.  
    }
  72.  
    else
  73.  
    {
  74.  
     
  75.  
    return false;
  76.  
    }
  77.  
    return UGenericArrayLibrary::FillDataTableFromJSONString(DataTable, JSONString);
  78.  
    }
  79.  
     
  80.  
    void UGenericArrayLibrary::GetDataTableAsCSVString(UDataTable* DataTable, FString& CSVString)
  81.  
    {
  82.  
    CSVString = FString();
  83.  
     
  84.  
    if (!DataTable || (DataTable->RowStruct == nullptr))
  85.  
    {
  86.  
     
  87.  
    return;
  88.  
    }
  89.  
     
  90.  
    // First build array of properties
  91.  
    TArray<FProperty*> StructProps;
  92.  
    for (TFieldIterator<FProperty> It(DataTable->RowStruct); It; It)
  93.  
    {
  94.  
    FProperty* Prop = *It;
  95.  
    check(Prop != nullptr);
  96.  
    StructProps.Add(Prop);
  97.  
    }
  98.  
     
  99.  
    // First row, column titles, taken from properties
  100.  
    CSVString = TEXT("---");
  101.  
    for (int32 PropIdx = 0; PropIdx < StructProps.Num(); PropIdx )
  102.  
    {
  103.  
    CSVString = TEXT(",");
  104.  
    CSVString = StructProps[PropIdx]->GetName();
  105.  
    }
  106.  
    CSVString = TEXT("\n");
  107.  
     
  108.  
    // Now iterate over rows
  109.  
    for (auto RowIt = DataTable->GetRowMap().CreateConstIterator(); RowIt; RowIt)
  110.  
    {
  111.  
    FName RowName = RowIt.Key();
  112.  
    CSVString = RowName.ToString();
  113.  
     
  114.  
    uint8* RowData = RowIt.Value();
  115.  
    for (int32 PropIdx = 0; PropIdx < StructProps.Num(); PropIdx )
  116.  
    {
  117.  
    CSVString = TEXT(",");
  118.  
    CSVString = DataTableUtils::GetPropertyValueAsString(StructProps[PropIdx], RowData, EDataTableExportFlags::None);
  119.  
    }
  120.  
    CSVString = TEXT("\n");
  121.  
    }
  122.  
    }
  123.  
     
  124.  
    void UGenericArrayLibrary::GetDataTableAsCSVFile(UDataTable* DataTable, const FString& CSVFilePath)
  125.  
    {
  126.  
    FString CSVString;
  127.  
    UGenericArrayLibrary::GetDataTableAsCSVString(DataTable, CSVString);
  128.  
    if (CSVString.Len() == 0)
  129.  
    {
  130.  
    return;
  131.  
    }
  132.  
    FFileHelper::SaveStringToFile(CSVString, *CSVFilePath, FFileHelper::EEncodingOptions::ForceUTF8);
  133.  
    }

四、蓝图实现

学新通

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhfbcj
系列文章
更多 icon
同类精品
更多 icon
继续加载