1.结构体代码
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "MyCharacterStats.generated.h"USTRUCT(BlueprintType)
struct FMyCharacterStats : public FTableRowBase
{ GENERATED_BODY()UPROPERTY(EditAnywhere, BlueprintReadWrite)FString Name;UPROPERTY(EditAnywhere, BlueprintReadWrite)int32 Health;UPROPERTY(EditAnywhere, BlueprintReadWrite)int32 Attack;
};
2.写一个C++蓝图函数库
.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Misc/Paths.h"
#include "Serialization/Csv/CsvParser.h"
#include "Misc/FileHelper.h"
#include "MyCharacterStats.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"/*** */
UCLASS()
class WEBH5_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{GENERATED_BODY()
public:UFUNCTION(BlueprintCallable,Category="Data")static TArray<FMyCharacterStats> LoadCSVData(const FString& CSVFilePath);/*UDataTable* LoadCSVData(const FString& CSVFilePath, TArray<FMyCharacterStats>& DataTable);*/
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "MyBlueprintFunctionLibrary.h"TArray<FMyCharacterStats> UMyBlueprintFunctionLibrary::LoadCSVData(const FString& CSVFilePath)
{TArray<FMyCharacterStats> outStats;FString CSVContent;if(!FFileHelper::LoadFileToString(CSVContent,*CSVFilePath))return outStats;const FCsvParser Parser(CSVContent);const FCsvParser::FRows& Rows = Parser.GetRows();for (int32 RowIndex = 1; RowIndex < Rows.Num(); RowIndex++) {FMyCharacterStats TempStats;TempStats.Name = Rows[RowIndex][0];TempStats.Attack = FCString::Atoi(Rows[RowIndex][1]);TempStats.Health = FCString::Atoi(Rows[RowIndex][2]);outStats.Add(TempStats);}return outStats;
}