1
0

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 3 years have passed since last update.

Unreal Engineでイベントグラフのノードの実行ピンを複数にして処理を分岐させる。

Posted at

C++でカスタムノードを作成した際、内部での処理の結果に応じて実行ルートを分けたい場合が多々あります。
例えば、キャラクターのライフ値を回復させる処理で、既に満タンだった場合と、満タンではなく回復を実行した場合で、イベントグラフの処理を分岐させたい場合を考えてみます。

まず、出力側の実行ピンを複数に分けるため、ピンの名前をenum(列挙型)として定義します。

UENUM(BlueprintType)
enum class BranchLife : uint8
{
    Regained,
    AlreadyMax
};

このenumの定義は関数の宣言の前に入れてください。順番が違うとエラーになります。

次に、ノードとなる関数を宣言します。

UFUNCTION(BlueprintCallable,meta = (ExpandEnumAsExecs = "Branch"))
    bool RegainLife(int Delta,BranchLife& Branch);

重要なのは、meta = (ExpandEnumAsExecs = "Branch")で引数を実行ピンとして指定するのと、実行ピンにする引数を参照(&)にすると出力ピンになるところです。

次に関数を実装します。

bool APawnHasLife::RegainLife(int Delta,BranchLife& Branch)
{
    if(Life<MaxLife){
        Life += Delta;
        if(Life>MaxLife){
            Life = MaxLife;
        }
        Branch = BranchLife::Regained;
        return true;
    }else{
        Life = MaxLife;
        Branch = BranchLife::AlreadyMax;
        return false;
    }
}

実行ピンを出したい場所で、実行ピンとして指定した引数に定義したenumを代入してください。
Branch = BranchLife::Regained;

Branch = BranchLife::AlreadyMax;
のところです。

これで、Regainedという名前の実行ピンとAlreadyMaxという名前の実行ピンが作られます。

Regainedに回復時のヴィジュアルエフェクトを実行する処理を接続して、AlreadyMaxに既に満タンだった旨のメッセージを表示する処理を接続する、といったものが考えられます。

参考文献

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?