LoginSignup
0
0

More than 5 years have passed since last update.

UnrealEngine4のBindActionにペイロードを乗せる

Last updated at Posted at 2018-05-02

BindAction

↓この関数をキーイベントとしてBindActionしたい。引数InPressはPress/Releaseに対応してる。

hogehoge.cpp
void AHogehoge::MyTrigger( bool InPress );

おっ!どうやらstd::function的にペイロードを乗せるやつがあるっぽい!

InputComponent.h
    template< class DelegateType, class UserClass, typename... VarTypes >
    FInputActionBinding& BindAction( const FName ActionName, const EInputEvent KeyEvent, UserClass* Object, typename DelegateType::template TUObjectMethodDelegate< UserClass >::FMethodPtr Func, VarTypes... Vars )

こんな感じかなと。

hogehoge.cpp
    PlayerInputComponent->BindAction<FInputActionHandlerSignature>("MyTrigger", IE_Pressed, this, &AHogehoge::MyTrigger, true);

さてビルド・・・

outputlog.h
Hogehoge.cpp(111): error C2664: 'FInputActionBinding &UInputComponent::BindAction<FInputActionHandlerSignature,AHogehoge,bool>(const FName,const EInputEvent,UserClass *,void (__cdecl AHogehoge::* )(void),bool)': cannot convert argument 4 from 'void (__cdecl AHogehoge::* )(bool)' to 'void (__cdecl AHogehoge::* )(void)'

ビルドエラー
ペイロードとしては扱われていない
うーん謎い

c++ のtemplate "..." はよくわからん

対策

ワンライナーで書けたらカッコイイけど、自分では解決できなかったのでDelegate::CreateUObjectを使う方法でごまかす。

    {
        FInputActionBinding AB( "MyTrigger", IE_Pressed );
        AB.ActionDelegate = FInputActionHandlerSignature::CreateUObject(this, &AHogehoge::MyTrigger, true );
        PlayerInputComponent->AddActionBinding(AB);
    }
    {
        FInputActionBinding AB("MyTrigger", IE_Released);
        AB.ActionDelegate = FInputActionHandlerSignature::CreateUObject(this, &AHogehoge::MyTrigger, false);
        PlayerInputComponent->AddActionBinding(AB);
    }

まぁいいか。
BindAction の ... 版の使い方を誰か教えておくれ。

UE4.18から使えるスマートかつあんまりトリッキーじゃないやつ

//マクロでデリゲート用の型を定義して
DECLARE_DELEGATE_OneParam(FMyTriggerDelegate, bool);

//テンプレート実引数で指定する
PlayerInputComponent->BindAction<FMyTriggerDelegate>("MyTrigger", IE_Pressed, this, &AHogehoge::MyTrigger, true);

Delegateを直接テンプレート引数に指定してパラメータをペイロードに乗せられます。いいですね!

mvyoshidaさんありがとうございました!!!

0
0
2

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