天天看點

Unreal Engine 4:學習筆記(十八)Table

目錄

一、DataTable

1、建立方式

2、使用方式

二、CurveTable

1、建立方式

2、使用方式

 三、StringTable

1、建立方式

2、使用方式

一、DataTable

1、建立方式

首先準備一個struct定義,比如LevelUpData.h,其中AchievementIcon導入後可轉化為TSoftObjectPtr<UTexture>類型:

/** Structure that defines a level up table entry */
#include "CoreMinimal.h"
#include "Engine/DataTable.h"
#include "LevelUpData.generated.h"

USTRUCT(BlueprintType)
struct FLevelUpData : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()
public:
	FLevelUpData()
		: XPtoLvl(0)
		, AdditionalHP(0)
		, Achievement(TEXT(""))
	{}

	/** The 'Name' column is the same as the XP Level */

	/** XP to get to the given level from the previous level */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LevelUp")
		int32 XPtoLvl;
	/** Extra HitPoints gained at this level */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LevelUp")
		int32 AdditionalHP;
	/** Achievement name at this level */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LevelUp")
		FString Achievement;
	/** Icon to use for Achivement */
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "LevelUp")
		TSoftObjectPtr<UTexture> AchievementIcon;
};
           

建立DataTable有如下兩種方式:

a.Content Brower中點選右鍵菜單,選擇Miscellaneous -> DataTable,選擇LevelUpData,再手工輸入。

b.導入的csv檔案:example.csv,其中第一行第一列的名字必須為Name:

Name,XPtoLvl,AdditionalHP,Achievement,AchievementIcon

1,0,0,"Achievement1",Texture2d'/Game/UI/Textures/AchievementIcon1.AchievementIcon1'

2,1000,9,"Achievement2",Texture2d'/Game/UI/Textures/AchievementIcon2.AchievementIcon2'

3,1000,10,"Achievement3",Texture2D'/Game/UI/Textures/AchievementIcon3.AchievementIcon3'

4,1500,12,"Achievement4",Texture2D'/Game/UI/Textures/AchievementIcon4.AchievementIcon4'

5,2000,14,"Achievement5",Texture2D'/Game/UI/Textures/AchievementIcon5.AchievementIcon5'

生成後的結果:

Unreal Engine 4:學習筆記(十八)Table

2、使用方式

static const FString ContextString(TEXT("GENERAL"));
static ConstructorHelpers::FObjectFinder<UDataTable> LevelUpDataDataTable_BP(TEXT("DataTable'/Game/UI/example.example'"));

UDataTable* LevelUpDataDataTable = LevelUpDataDataTable_BP.Object;
FLevelUpData* Row = LevelUpDataDataTable->FindRow<FLevelUpData>(*FString::Printf(TEXT("%d"), 1), ContextString);

GEngine->AddOnScreenDebugMessage(-1, 60.f, FColor::Red, FString::Printf(TEXT("Achievement=%s"), *Row->Achievement));
           

二、CurveTable

1、建立方式

需要導入的csv檔案:example1.csv,其中第一行第一列的名字必須為Name:

Name,0,1,2,3

Melee_Damage,15,20,25,30

Melee_KnockBack,1,2,4,8

Melee_KnockBackAngle,10,45,60,65

Melee_StunTime,0,1,5,7

 curve interpolation type上課選如下三種值:

Cubic:三次插值

Constant:常數内插法

Linear:線性插值

 導入後的結果:

Unreal Engine 4:學習筆記(十八)Table

2、使用方式

 使用方式與CurveTable的使用方式相同

 三、StringTable

1、建立方式

a.使用C++建立

// Create and populate a string table using only C++
LOCTABLE_NEW("CodeStringTable", "CodeStringTable");
LOCTABLE_SETSTRING("CodeStringTable", "HelloWorld", "Hello World!");
LOCTABLE_SETMETA("CodeStringTable", "HelloWorld", "Comment", "This is a comment about hello world");
LOCTABLE_SETSTRING("CodeStringTable", "GoodbyeWorld", "Goodbye World!");
LOCTABLE_SETMETA("CodeStringTable", "GoodbyeWorld", "Comment", "This is a comment about goodbye world");
           

b.使用csv檔案建立

使用LOCTABLE_FROMFILE_X加載CSV檔案

若X=ENGINE,表示CSV檔案存放于相對于Engine的content目錄的位置;

若X=GAME,表示CSV檔案存放于相對于項目content目錄的位置;

例子如下:

Key,SourceString,Comment

"HelloWorld","Hello World!","This is a comment about hello world"

"GoodbyeWorld","Goodbye World!","This is a comment about goodbye world"

代碼如下:

// Create and populate a string table from a CSV file
LOCTABLE_FROMFILE_GAME("CSVStringTable", "CSVStringTable", "StringTableCSV/TestStrings.csv");
           

c.建立String Table Asset

Content Browser (Add > Miscellaneous > String Table)

2、使用方式

a.從C++代碼中作用

LOCTABLE(ID, KEY)

FText::FromStringTable(const FName InTableId, const FString& InKey, const EStringTableLoadingPolicy InLoadingPolicy)
           

b.從INI檔案中使用

使用LOCTABLE宏

c.從FText屬性忡使用

d.從Blueprint中使用

比如使用Make Literal Text節點

參考文檔

Documentation Home -> Programming Guide -> Data Driven Gameplay Elements

Documentation Home -> Gameplay Guide -> Localization -> String Tables

Using excel to store gameplay data - DataTables

Driving Gameplay with Data from Excel

[UE4]引擎自身提供的一種誇平台讀寫Excel的元件:DataTable

繼續閱讀