3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unreal Engine 4メモ:DataTableのTips

Last updated at Posted at 2018-02-21
1 / 8

パラメータの用途をエディタ上で明示的にする1

Datatableの構造体自体にDisplayNameを追加

  • テーブルデータを新規追加時にDisplayNameで指定した文字列でプルダウンメニューに表示される。
  • 日本語にすると逆に分かりにくいかもしれないが、注釈を加える程度は役に立つかもしれない。
Sample.cpp
USTRUCT(BlueprintType, DisplayName = "FShopListParam:商品リストに関するデータ")
struct FShopListParam : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()
};

パラメータの用途をエディタ上で明示的にする2

  • パラメータにDisplayNameを設定すると、アセットを開いた時にカラム名を自由に設定でき可読性が上がる。
  • データ入力ウインドウではDisplayName、Categoryを設定するとその他のアセットのインスペクタと同じように表示される。
Sample.cpp
USTRUCT(BlueprintType)
struct FShopParam : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, DisplayName = "店舗ID", Category = "店舗番号を入力する。")
	uint32 ShopID;
};

UDataTableを入れ子にする

  • リレーションしたいときはコレ。但し、csvからインポートして使う場合はパスの指定方法に注意。エディタからだと選んで登録するだけ。
  • もちろんロードすれば、ShopListDataはすぐに使える状態になっている。
Sample.cpp
USTRUCT(BlueprintType)
struct FShopParam : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, DisplayName = "ショップ商品リスト情報のDatatableアセット", Category = "(テキストでの入力例)「DataTable'/Game/ShopList_TEST1.ShopList_TEST1'」")
	UDataTable* ShopListData;
};

デフォルト値を設定する

  • コンストラクタでメンバイニシャライザを書くと、エディタ上で新しいレコードを追加した時にその値で初期化してくれる。「0」以外を初期値にしたい場合は便利。
Sample.cpp
USTRUCT(BlueprintType)
struct FShopParam : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, DisplayName = "店舗ID", Category = "店舗番号を入力する。")
	uint32 ShopID;

	FShopParam() : ShopID(-1) {} // 初めは無効値にしておく
};

インポートされたレコードを解析、加工する

  • FTableRowBase::OnPostDataImportはカスタム修正や解析などを目的としたインタフェース。
  • Post処理なので、まさにインポートする時に加工して代入ができる訳ではないが、文字列で登録させといて内部的に時間パラメータにパースする等の変則的なコードなら書けそう1
  • また、解析した結果問題のあるデータが見つかった場合はOutCollectedImportProblemsに文字列を登録するとログに出る(未確認・・・)。

DataTable.h
	/** 
	 * Can be overridden by subclasses; Called whenever the owning data table is imported or re-imported.
	 * Allows for custom fix-ups, parsing, etc. after initial data is read in.
	 * 
	 * @param InDataTable					The data table that owns this row
	 * @param InRowName						The name of the row we're performing fix-up on
	 * @param OutCollectedImportProblems	List of problems accumulated during import; Can be added to via this method
	 */
	virtual void OnPostDataImport(const UDataTable* InDataTable, const FName InRowName, TArray<FString>& OutCollectedImportProblems) {}

Sample.cpp
USTRUCT(BlueprintType)
struct FBestRecordParam : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

public:
	UPROPERTY(EditAnywhere)
		FString RecordString;
	FTimespan Record;

	void OnPostDataImport(const UDataTable* InDataTable, const FName InRowName, TArray<FString>& OutCollectedImportProblems) override final
	{
		auto* tmp = UDataParameter::FindRowByName<FBestRecordParam>(InDataTable, InRowName);
		bool bTryparse = FTimespan::Parse(tmp->RecordString, Record);
		if (!bTryparse) UE_LOG(LogTemp, Warning, L"時間のパースに失敗");
	}
};
  1. UE4メモ:DataTableのFTimespan型はインポート出来ない

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?