在 UE4.22.3 及更高版本中将用户输入绑定到 UActorComponent 方法
Bind user input to UActorComponent methods in UE4.22.3 and Up
我正在尝试创建 UActorComponent 来处理一些用户输入。
我是这样做的:
void MyComponent::BeginPlay()
{
Super::BeginPlay();
this->InputComponent = GetOwner() ->FindComponentByClass<UInputComponent>();
if (this->InputComponent != nullptr) {
UE_LOG(LogTemp, Display, TEXT("%s InputComponent Found"), *(GetReadableName()));
this->InputComponent->BindAction("MyAction", IE_Pressed, this, &MyComponent::ActionStart);
this->InputComponent->BindAction("MyAction", IE_Released, this, &MyComponent:: ActionEnd);
UE_LOG(LogTemp, Display, TEXT("%s InputComponent Binding done"), *(GetReadableName()));
}
}
但是从未调用组件的方法。我发现调用 Pawns SetupPlayerInputComponent
方法后所有绑定都消失了。如果我在 SetupPlayerInputComponent
内进行所有绑定,所有绑定都可以正常工作。
那么在 UActorComponent 中处理用户输入的最佳方式是什么,或者这根本不是好的做法?
正如您提到的,如果您在 SetupPlayerInputComponent
中调用函数,绑定就会起作用,所以最简单的解决方案是在您的组件中创建一个名为 SetupInput
的函数,然后从您的 pawn 的 SetupPlayerInputComponent
函数。
是的,在我看来,绑定应该由受控 pawn 或玩家控制器处理,因为 ue4 游戏框架就是这样工作的。
如果您没有任何特定的 pawn 可以控制,或者如果它只是一个相机,我会将我的所有输入移动到控制器内部。
在 SetupPlayerInputComponent 内部
InputComponent->BindAction("MyAction", IE_Pressed, MyComponentRef, &MyComponent::ActionStart);
至于它是否是好的做法,它有它的好处,主要是为了减少混乱或在 APawn 的几个不相关的子类中重用输入处理程序。
我正在尝试创建 UActorComponent 来处理一些用户输入。 我是这样做的:
void MyComponent::BeginPlay()
{
Super::BeginPlay();
this->InputComponent = GetOwner() ->FindComponentByClass<UInputComponent>();
if (this->InputComponent != nullptr) {
UE_LOG(LogTemp, Display, TEXT("%s InputComponent Found"), *(GetReadableName()));
this->InputComponent->BindAction("MyAction", IE_Pressed, this, &MyComponent::ActionStart);
this->InputComponent->BindAction("MyAction", IE_Released, this, &MyComponent:: ActionEnd);
UE_LOG(LogTemp, Display, TEXT("%s InputComponent Binding done"), *(GetReadableName()));
}
}
但是从未调用组件的方法。我发现调用 Pawns SetupPlayerInputComponent
方法后所有绑定都消失了。如果我在 SetupPlayerInputComponent
内进行所有绑定,所有绑定都可以正常工作。
那么在 UActorComponent 中处理用户输入的最佳方式是什么,或者这根本不是好的做法?
正如您提到的,如果您在 SetupPlayerInputComponent
中调用函数,绑定就会起作用,所以最简单的解决方案是在您的组件中创建一个名为 SetupInput
的函数,然后从您的 pawn 的 SetupPlayerInputComponent
函数。
是的,在我看来,绑定应该由受控 pawn 或玩家控制器处理,因为 ue4 游戏框架就是这样工作的。 如果您没有任何特定的 pawn 可以控制,或者如果它只是一个相机,我会将我的所有输入移动到控制器内部。
在 SetupPlayerInputComponent 内部
InputComponent->BindAction("MyAction", IE_Pressed, MyComponentRef, &MyComponent::ActionStart);
至于它是否是好的做法,它有它的好处,主要是为了减少混乱或在 APawn 的几个不相关的子类中重用输入处理程序。