将用户输入绑定到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
函数调用该函数。
是的,在我看来,绑定应该由受控棋子或玩家控制器处理,因为这就是ue4游戏框架的工作方式。如果您没有任何特定的棋子要控制,或者如果它只是一个摄像机,我会在控制器内移动我的所有输入,将其移动到棋子本身中。
在
SetupPlayerInputComponent 中
InputComponent->BindAction("MyAction", IE_Pressed, MyComponentRef, &MyComponent::ActionStart);
至于它是否是好的做法,它可以有它的好处,主要是减少混乱或在 APawn 的几个不相关的子类中重用输入处理程序。
相关文章:
- VS2017 版本 15.8.3 成功编译内联方法,而不返回所需值
- 获取Windows版本的正确方法
- 将用户输入绑定到UE4.22.3及更高版本中的UActorComponent方法
- 设置用G 构建的二进制版本的任何方法
- 拥有相同方法的静态和非静态版本是不是设计不好
- 使用 C++17 或更高版本对向量中的元素对求和的最'functional'方法?
- 在C 11或更高版本中,是否可以通过Lambda来实现单方法纯Virtual C 接口
- 最新版本的 Boost asio 库中缺少方法的替代方法
- 使用其范围版本从容器中删除元素的最佳方法
- 使用 C# 获取 OpenGL 版本的最简单方法
- 建议在我的C 库中管理版本的方法
- 为什么较新版本的C++不允许在类声明中定义纯方法?
- 当模板参数为std::vector时,如何创建此方法的模板版本
- 有哪些好方法可以对二进制打包的结构消息进行版本化
- C++ 是在每个实例化上创建的模板类中每个方法的新版本
- 设计方法以读/编写同一配置文件的不同版本
- 无论CRT版本如何,这都是在库中提供STL函数的有效方法吗
- C/CPP 宏或预处理器,用于处理方法的多个版本
- DialogBoxParam()方法未调用IE 9或更高版本中的GUI
- C++堆栈实现 - 两个版本的 top 方法