Pawn中的InputComponent是谁的?
结论:默认情况下,如果Pawn首次被PlayerController控制,则会在Pawn中创建一个InputComponent,写在Pawn中SetupPlayerInputComponent的绑定也是绑定到这个新建的InputComponent中
平时在ue4中新建一个继承pawn的c++类时,一般会在下面这个函数里写输入绑定
void SetupPlayerInputComponent(UinputComponent *PlayerInputcomponent)
也没想过这个函数的参数是从哪来的,之后听到了一种说法是
PlayerController会把自己的InputComponent “借给” 它控制的Pawn。
由于一个PlayerController同时只能绑定一个Pawn,那在某处把自己的InputComponent借给自己控制的Pawn听起来也有点合理?那么假设一个PlayerController控制一个新创建的Pawn
1.SetupPlayerInputComponent()
既然绑定是在 SetupPlayerInputComponent()
这个函数里进行的,那么就从它在源码中被调用过的地方着手,经过查找发现只有一处:
void APawn::PawnClientRestart(){
···
// 如果Pawn中没有InputComponent
if (InputComponent == nullptr)
{
// 新建一个InputComponent
InputComponent = CreatePlayerInputComponent();
if (InputComponent)
{
SetupPlayerInputComponent(InputComponent);
···
}
}
}
所以这个InputComponent是从
CreatePlayerInputComponent()
获取的,继续看:
2.CreatePlayerInputComponent()
UInputComponent* APawn::CreatePlayerInputComponent()
{
static const FName InputComponentName(TEXT("PawnInputComponent0"));
// 直接新建一个InputComponent,默认名字为PawnInputComponent0
return NewObject<UInputComponent>(this, UInputSettings::GetDefaultInputComponentClass(), InputComponentName);
}
此函数也仅在Pawn的源码中有一处调用,所以可得出开头的结论。