#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);
}
}
网友评论