UE4动态加载数据表

时间:2024-03-27 10:28:31

1.创建一个数据表

UE4动态加载数据表

2.新建C++,DataTable

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "MyDataTable.generated.h"


/** Example Data */
USTRUCT(BlueprintType)
struct FExampleData : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

public:

	/** Name */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = LevelUp)
		FString name;

	/** Extra HitPoints gained at this level */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = LevelUp)
		FString age;


};


/**
 * 
 */
UCLASS()
class SERVER_API UMyDataTable : public UDataTable
{
	GENERATED_BODY()
	
	
public:


	
};

3.创建一个Actor

.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "DataTableActor.generated.h"

UCLASS()
class SERVER_API ADataTableActor : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ADataTableActor();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

	UFUNCTION(BlueprintCallable, Category = "MySocket")
		UDataTable* ReadCsvFile(FString csvPath);
	

	class UDataTable* mydatatable;
};

.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "DataTableActor.h"
#include "../../../../Epic Games/UE_4.18/Engine/Source/Runtime/Core/Public/Misc/Paths.h"
#include "../../../../Epic Games/UE_4.18/Engine/Source/Runtime/Core/Public/Misc/FileHelper.h"
#include "MyDataTable.h"
#include "../../../../Epic Games/UE_4.18/Engine/Source/Runtime/CoreUObject/Public/UObject/ConstructorHelpers.h"
#include "../../../../Epic Games/UE_4.18/Engine/Source/Runtime/Engine/Classes/Engine/Engine.h"


// Sets default values
ADataTableActor::ADataTableActor()
{
 	// Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	static ConstructorHelpers::FObjectFinder<UDataTable> mydatatable_BP(TEXT("DataTable'/Game/datatable/t2.t2'"));

	if (mydatatable_BP.Object)
	{
		UE_LOG(LogTemp, Warning, TEXT("get datatable"));
		//GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, FString::Printf(TEXT("get datatable")));
		mydatatable = mydatatable_BP.Object;
	}
	
}

// Called when the game starts or when spawned
void ADataTableActor::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ADataTableActor::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);

}

UDataTable* ADataTableActor::ReadCsvFile(FString csvPath)
{
	FString csvFile = FPaths::ProjectContentDir() + csvPath;
	if (FPaths::FileExists(csvFile))
	{
		FString FileContent;
		//Read the csv file  
		FFileHelper::LoadFileToString(FileContent, *csvFile);
		TArray<FString> problems = mydatatable->CreateTableFromCSVString(FileContent);
	
		if (problems.Num() > 0)
		{
			for (int32 ProbIdx = 0; ProbIdx < problems.Num(); ProbIdx++)
			{
				//Log the errors  
				UE_LOG(LogTemp, Warning, TEXT("Problem with reimport!"));
			}
		}
		else
		{
			//Updated Successfully  
			UE_LOG(LogTemp, Warning, TEXT("Successful reimport!"));
		}
	}
	return mydatatable;
}

4

UE4动态加载数据表

UE4动态加载数据表

UE4动态加载数据表