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

#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);
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值