目录
2、ConstructorHelpers::FObjectFinder
七、写入AddRow(RowName, DataStruct)
【UE】UEC++ DataTable数据表
一、结构体
#pragma once
#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "DataStruct.generated.h"
USTRUCT(BlueprintType)
struct FDataStruct : public FTableRowBase
{
GENERATED_BODY()
UPROPERTY(EditAnywhere)
FString Name;
UPROPERTY(EditAnywhere)
float HP;
UPROPERTY(EditAnywhere)
float EXP;
UPROPERTY(EditAnywhere)
float Damage;
};
二、DataTableComponent
声明一个组件,就可以在需要的类上挂载组件,进行对DataTable的处理。
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "DataTableProject/Structs/DataStruct.h"
#include "DataTableComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class DATATABLEPROJECT_API UDataTableComponent : public UActorComponent
{
GENERATED_BODY()
public:
UDataTableComponent();
private:
UPROPERTY()
UDataTable* DataTable;
UPROPERTY(EditAnywhere)
FString DataTablePath;
public:
UFUNCTION()
void SetDataTablePath(FString InDataTablePath);
};
#include "DataTableComponent.h"
#include "DataTableProject/Structs/DataStruct.h"
#include "Engine/DataTable.h"
UDataTableComponent::UDataTableComponent()
{
PrimaryComponentTick.bCanEverTick = false;
}
void UDataTableComponent::SetDataTablePath(FString InDataTablePath)
{
this->DataTablePath = InDataTablePath;
}
DataTableComponent上有一个DataTablePath的属性,这里存储了DataTable的路径,在不同情况下设置不同的路径,就可以对不同的DataTable进行操作。
三、加载
1、LoadObject()
void UDataTableComponent::LoadDataTable()
{
DataTable = LoadObject<UDataTable>(this, *DataTablePath);
}
2、ConstructorHelpers::FObjectFinder
DataTable = ConstructorHelpers::FObjectFinder<UDataTable>(*DataTablePath).Object;
3、StaticLoadObject
DataTable = Cast<UDataTable>(StaticLoadObject(UDataTable::StaticClass(), nullptr, *DataTablePath));
四、读取
1、GetAllRows
void UDataTableComponent::ReadDataByAllRows()
{
if(DataTable)
{
TArray<FDataStruct*> Datas;
DataTable->GetAllRows<FDataStruct>(TEXT("DataStruct"), Datas);
for(FDataStruct* Data : Datas)
{
UE_LOG(LogTemp, Log, TEXT("%s, %f, %f, %f"), *Data->Name, Data->HP, Data->EXP, Data->Damage)
}
}
}
2、FindRow
void UDataTableComponent::ReadDataByFindRow()
{
if(DataTable)
{
for(FName RowName : DataTable->GetRowNames())
{
const FDataStruct* DataStruct = DataTable->FindRow<FDataStruct>(RowName, TEXT("DataStruct"));
UE_LOG(LogTemp, Log, TEXT("%s, %f, %f, %f"), *DataStruct->Name, DataStruct->HP, DataStruct->EXP, DataStruct->Damage)
}
}
}
3、GetRowMap
void UDataTableComponent::ReadDataByRowMap()
{
if(DataTable)
{
for(auto it : DataTable->GetRowMap())
{
const FDataStruct* DataStruct = reinterpret_cast<FDataStruct*>(it.Value);
UE_LOG(LogTemp, Log, TEXT("%s, %f, %f, %f"), *DataStruct->Name, DataStruct->HP, DataStruct->EXP, DataStruct->Damage)
}
}
}
五、移除RemoveRow(RowName)
void UDataTableComponent::OptionDataRemoveRow(FName InRowName)
{
if(DataTable)
DataTable->RemoveRow(InRowName);
}
六、置空EmptyTable()
void UDataTableComponent::OptionDataEmptyTable()
{
if(DataTable)
DataTable->EmptyTable();
}
七、写入AddRow(RowName, DataStruct)
void UDataTableComponent::OptionDataAddRow(FDataStruct InDataStruct)
{
if(DataTable)
DataTable->AddRow(TEXT("a"), InDataStruct);
}
八、最终代码
1、GameMode
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "DataTableGameMode.generated.h"
UCLASS()
class DATATABLEPROJECT_API ADataTableGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
ADataTableGameMode();
};
#include "DataTableGameMode.h"
#include "DataTableController.h"
ADataTableGameMode::ADataTableGameMode()
{
PlayerControllerClass = ADataTableController::StaticClass();
}
2、PlayerController
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "DataTableController.generated.h"
UCLASS()
class DATATABLEPROJECT_API ADataTableController : public APlayerController
{
GENERATED_BODY()
public:
ADataTableController();
virtual void SetupInputComponent() override;
private:
UPROPERTY(EditAnywhere)
class UDataTableComponent* DataTableComponent;
public:
UFUNCTION()
void LoadData();
UFUNCTION()
void ReadData();
UFUNCTION()
void RemoveData();
UFUNCTION()
void EmptyData();
UFUNCTION()
void AddData();
};
#include "DataTableController.h"
#include "DataTableComponent.h"
#include "DataTableProject/Structs/DataStruct.h"
ADataTableController::ADataTableController()
{
DataTableComponent = CreateDefaultSubobject<UDataTableComponent>(TEXT("DataTableComponent"));
DataTableComponent->SetDataTablePath(FString("DataTable'/Game/Data/MyDataTable.MyDataTable'"));
}
void ADataTableController::SetupInputComponent()
{
Super::SetupInputComponent();
InputComponent->BindAction(TEXT("Load"), IE_Pressed,this, &ThisClass::LoadData);
InputComponent->BindAction(TEXT("Read"), IE_Pressed,this, &ThisClass::ReadData);
InputComponent->BindAction(TEXT("Remove"), IE_Pressed,this, &ThisClass::RemoveData);
InputComponent->BindAction(TEXT("Empty"), IE_Pressed,this, &ThisClass::EmptyData);
InputComponent->BindAction(TEXT("Add"), IE_Pressed,this, &ThisClass::AddData);
}
void ADataTableController::LoadData()
{
DataTableComponent->LoadDataTable();
}
void ADataTableController::ReadData()
{
DataTableComponent->ReadDataByAllRows();
//DataTableComponent->ReadDataByFindRow();
//DataTableComponent->ReadDataByRowMap();
}
void ADataTableController::RemoveData()
{
DataTableComponent->OptionDataRemoveRow(FName("2"));
}
void ADataTableController::EmptyData()
{
DataTableComponent->OptionDataEmptyTable();
}
void ADataTableController::AddData()
{
FDataStruct data;
data.Name = "Tom";
data.HP = 200;
data.EXP = 1000;
data.Damage = 300;
DataTableComponent->OptionDataAddRow(data);
}
3、DataTableComponent
#pragma once
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "DataTableProject/Structs/DataStruct.h"
#include "DataTableComponent.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class DATATABLEPROJECT_API UDataTableComponent : public UActorComponent
{
GENERATED_BODY()
public:
UDataTableComponent();
private:
UPROPERTY()
class UDataTable* DataTable;
UPROPERTY(EditAnywhere)
FString DataTablePath;
public:
UFUNCTION()
void SetDataTablePath(FString InDataTablePath);
UFUNCTION()
void LoadDataTable();
UFUNCTION()
void ReadDataByAllRows();
UFUNCTION()
void ReadDataByFindRow();
UFUNCTION()
void ReadDataByRowMap();
UFUNCTION()
void OptionDataRemoveRow(FName InRowName);
UFUNCTION()
void OptionDataEmptyTable();
UFUNCTION()
void OptionDataAddRow(FDataStruct InDataStruct);
};
#include "DataTableComponent.h"
#include "DataTableProject/Structs/DataStruct.h"
#include "Engine/DataTable.h"
UDataTableComponent::UDataTableComponent()
{
PrimaryComponentTick.bCanEverTick = false;
}
void UDataTableComponent::SetDataTablePath(FString InDataTablePath)
{
this->DataTablePath = InDataTablePath;
}
void UDataTableComponent::LoadDataTable()
{
DataTable = LoadObject<UDataTable>(this, *DataTablePath);
if(DataTable)
{
UE_LOG(LogTemp, Log, TEXT("Load Data Table Success"))
}else
{
UE_LOG(LogTemp, Log, TEXT("Load Data Table failed"))
}
}
void UDataTableComponent::ReadDataByAllRows()
{
if(DataTable)
{
TArray<FDataStruct*> Datas;
DataTable->GetAllRows<FDataStruct>(TEXT("DataStruct"), Datas);
for(FDataStruct* Data : Datas)
{
UE_LOG(LogTemp, Log, TEXT("%s, %f, %f, %f"), *Data->Name, Data->HP, Data->EXP, Data->Damage)
}
}
}
void UDataTableComponent::ReadDataByFindRow()
{
if(DataTable)
{
for(FName RowName : DataTable->GetRowNames())
{
const FDataStruct* DataStruct = DataTable->FindRow<FDataStruct>(RowName, TEXT("DataStruct"));
UE_LOG(LogTemp, Log, TEXT("%s, %f, %f, %f"), *DataStruct->Name, DataStruct->HP, DataStruct->EXP, DataStruct->Damage)
}
}
}
void UDataTableComponent::ReadDataByRowMap()
{
if(DataTable)
{
for(auto it : DataTable->GetRowMap())
{
const FDataStruct* DataStruct = reinterpret_cast<FDataStruct*>(it.Value);
UE_LOG(LogTemp, Log, TEXT("%s, %f, %f, %f"), *DataStruct->Name, DataStruct->HP, DataStruct->EXP, DataStruct->Damage)
}
}
}
void UDataTableComponent::OptionDataRemoveRow(FName InRowName)
{
if(DataTable)
DataTable->RemoveRow(InRowName);
}
void UDataTableComponent::OptionDataEmptyTable()
{
if(DataTable)
DataTable->EmptyTable();
}
void UDataTableComponent::OptionDataAddRow(FDataStruct InDataStruct)
{
if(DataTable)
DataTable->AddRow(TEXT("a"), InDataStruct);
}