看看UE4源码: Pawn中默认InputComponent是控制器还是Pawn的

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的源码中有一处调用,所以可得出开头的结论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值