美文网首页UE4 日积月累
UE4使用c++ 加载DataTable数据表

UE4使用c++ 加载DataTable数据表

作者: 我真的不知道该起什么名字了 | 来源:发表于2019-04-20 23:13 被阅读0次
    #pragma once
    #include "Engine/DataTable.h"
    #include "CoreMinimal.h"
    #include "BPLibrary.generated.h"
    DECLARE_LOG_CATEGORY_EXTERN(DemoLog, Log, All);
    #define LogDebug(format, ...) UE_LOG(DemoLog, Log, TEXT(format), ##__VA_ARGS__)
    #define LogWarning(format, ...) UE_LOG(DemoLog, Warning, TEXT(format), ##__VA_ARGS__)
    #define LogError(format, ...) UE_LOG(DemoLog, Error, TEXT(format), ##__VA_ARGS__)
    #define LogFatal(format, ...) UE_LOG(DemoLog, Fatal, TEXT(format), ##__VA_ARGS__)
    // This class is a base class for any function libraries exposed to blueprints.
    // Methods in subclasses are expected to be static, and no methods should be added to this base class.
    UCLASS(BlueprintType)
    class UBPLibrary : public UBlueprintFunctionLibrary
    {
        GENERATED_BODY()
    public:
        UFUNCTION(BlueprintCallable, Category = Character)
            static void TestLoadTable();
    };
    USTRUCT(BlueprintType)
    struct FDataTableTestData : public FTableRowBase
    {
        GENERATED_BODY()
    public:
        FDataTableTestData()
            : HP(0)
        {}
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DataTable Test")
            int32 ID;
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DataTable Test")
            FString Name;
        UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "DataTable Test")
            int32 HP;
    };
    
    #include "BPLibrary.h"
    DEFINE_LOG_CATEGORY(DemoLog);
    void UBPLibrary::TestLoadTable()
    {
        UDataTable *pDataTable = LoadObject<UDataTable>(NULL, UTF8_TO_TCHAR("DataTable'/Game/MyTable.MyTable'"));
        FString ContextString;
        TArray<FName> RowNames;
        RowNames = pDataTable->GetRowNames();
        for (auto& name : RowNames)
        {
            FDataTableTestData* pRow = pDataTable->FindRow<FDataTableTestData>(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);
            }
        }
        for (auto it : pDataTable->GetRowMap())
        {
            // it.Key has the key from first column of the CSV file
            // it.Value has a pointer to a struct of data. You can safely cast it to your actual type, e.g FMyStruct* data = (FMyStruct*)(it.Value);
            FString rowName = (it.Key).ToString();
            FDataTableTestData* pRow = (FDataTableTestData*)it.Value;
            LogDebug("read by traversal --- RowName:%s, ID:%d, Name:%s, HP:%d", *rowName, pRow->ID, *pRow->Name, pRow->HP);
        }
    }
    

    相关文章

      网友评论

        本文标题:UE4使用c++ 加载DataTable数据表

        本文链接:https://www.haomeiwen.com/subject/ivmrgqtx.html