LoginSignup
2
3

More than 5 years have passed since last update.

[UE4]UnrealC++でオペレーターとかPrintStringに繋いだ時に自動でToString呼出しとか

Posted at

はじめに

少し気になったので

  • UnrealC++でのオペレーターはどうするのか
  • Blueprint上でPrintStringにUnrealC++で宣言した構造体を繋いだ時に自動でToStringを出す方法

とか調べました

前提

バージョン:UE4.14.1

BlankPluginを生成してそのなかで試しました

ヘッダーファイルに全部書いてるのは見逃してください!

作ったPluginのソース

下記のソース以外は生成されたものそのままです

Hoge.h
#pragma once

#include "OperatorTestPluginPrivatePCH.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Hoge.generated.h"


USTRUCT(BlueprintType)
struct FHoge
{
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    float X;

    FHoge()
    {
        X = 0;
    }

    FHoge(float _X)
    {
        X = _X;
    }

    bool operator==(const FHoge& A) const
    {
        return X == A.X;
    }

    FString ToString() const
    {
        return FString::Printf(TEXT("X=%3.3f"), X);
    }

};

UCLASS()
class UExBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{

    GENERATED_BODY()

public:

    UFUNCTION(BlueprintPure, meta = (DisplayName = "Equal (Hoge)", CompactNodeTitle = "==", Keywords = "== equal"), Category = "ExMath|Hoge")
    static bool EqualEqual_HogeHoge(FHoge A, FHoge B);

    UFUNCTION(BlueprintPure, meta = (DisplayName = "ToString (Hoge)", CompactNodeTitle = "->", BlueprintAutocast), Category = "ExUtilities|String")
    static FString Conv_HogeToString(FHoge A);

private:

};

bool UExBlueprintFunctionLibrary::EqualEqual_HogeHoge(FHoge A, FHoge B)
{
    return A == B;
}

FString UExBlueprintFunctionLibrary::Conv_HogeToString(FHoge A)
{
    return A.ToString();
}

UnrealC++でのオペレーターはどうするのか

bool operator==(const FHoge& A) const
{
    return X == A.X;
}

普通のC++と変わりないみたい

下記のようにしてBlueprintに公開する時に分かったのですが

UFUNCTION(BlueprintPure, meta = (DisplayName = "Equal (Hoge)", CompactNodeTitle = "==", Keywords = "== equal"), Category = "ExMath|Hoge")
static bool EqualEqual_HogeHoge(FHoge A, FHoge B);

CompactNodeTitleを使えば

equalname.PNG

みたいな感じでノードがコンパクトになりました

Blueprint上でPrintStringにUnrealC++で宣言した構造体を繋いだ時に自動でToStringを出す方法

UFUNCTION(BlueprintPure, meta = (DisplayName = "ToString (Hoge)", CompactNodeTitle = "->", BlueprintAutocast), Category = "ExUtilities|String")
static FString Conv_HogeToString(FHoge A);

BlueprintAutocastを使えば自動で呼び出せるようになります(複数候補がある場合はどうなるんだろう…)

あとCompactNodeTitleを->にすると

tostring.PNG

みたいになります。こういう別の文字や記号に変わるものもあるみたいですね

おわりに

4.12くらいまでPrivatePCHに#include "Kismet/BlueprintFunctionLibrary.h"しても大丈夫だったのに今回怒られました。何か忘れてるのかもしれませんがちょっと分かりませんでした。

2
3
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
2
3