Pawn类
2023年11月5日 2023年11月21日
玩家所操作的角色,提供键位绑定
由PlayerController控制,可在多个Pawn之间进行切换
APawn
构成
APawn::SetupPlayerInputComponent
配置用户输入: 在该函数内绑定键位
APawn::StaticClass
静态成员
返回指向UClass的指针
在GENERATED_BODY宏中定义
UObject也有StaticClass,UObjectBaseUtility没有
控制器
访问Controller对象
APawn::GetController
获取指向AController的指针
可以是PlayerController,也可以是AIController, 自行转换
APawn::Controller
public
使用指向AController的指针
APawn::GetController<T>
获取指定类型控制器
1// Returns controller for this actor cast to the template type. May return NULL is the cast fails 2template < class T > 3T* GetController() const 4{ 5 return Cast<T>(GetController()); 6}
控制状态
APawn::IsControlled
是否被PlayerController接管
APawn::IsPawnControlled
是否被Controller接管: AIController或PlayerController
受控于AI控制器
APawn::AIControllerClass
设置AIController类型
1// Default class to use when pawn is controlled by AI 2UPROPERTY(EditAnywhere, BlueprintReadWrite, meta=(DisplayName="AI Controller Class"), Category=Pawn) 3TSubclassOf<AController> AIControllerClass;
APawn::AutoPossessAI
设置Pawn被创建和被AI控制器接管的条件
1// * 2// * Determines when the Pawn creates and is possessed by an AI Controller (on level start, when spawned, etc). 3// * Only possible if AIControllerClassRef is set, and ignored if AutoPossessPlayer is enabled. 4// * @see AutoPossessPlayer 5// * 6UPROPERTY(EditAnywhere, Category=Pawn) 7EAutoPossessAI AutoPossessAI;
APawn::Reset
对基类函数进行了覆写
1virtual void Reset() override;
发生以下任意情形, 调用Destroy销毁Pawn
- 上级控制器不存在
- 控制器拥有PlayerState
AIController默认无PlayerState
1void APawn::Reset() 2{ 3 if ( (Controller == nullptr) || (Controller->PlayerState != nullptr) ) 4 { 5 DetachFromControllerPendingDestroy(); 6 Destroy(); 7 } 8 else 9 { 10 Super::Reset(); 11 } 12}
APawn::TurnOff
使Pawn静止
1/** Freeze pawn - stop sounds, animations, physics, weapon firing */ 2virtual void TurnOff();
- 第一部分和网络编程有关
- 关闭碰撞
- 去使能运动组件
- 关闭物理仿真
1void APawn::TurnOff() 2{ 3 if (GetLocalRole() == ROLE_Authority) 4 { 5 SetReplicates(true); 6 } 7 8 // do not block anything, just ignore 9 SetActorEnableCollision(false); 10 11 UPawnMovementComponent* MovementComponent = GetMovementComponent(); 12 if (MovementComponent) 13 { 14 MovementComponent->StopMovementImmediately(); 15 MovementComponent->SetComponentTickEnabled(false); 16 } 17 18 DisableComponentsSimulatePhysics(); 19}
APawn::DisableInput
关闭输入
覆写AActor::DisableInput
1virtual void DisableInput(APlayerController* PlayerController) override;
只要传入的控制器指针为空就可以达到效果
1void APawn::DisableInput(class APlayerController* PlayerController) 2{ 3 if (PlayerController == Controller || PlayerController == nullptr) 4 { 5 bInputEnabled = false; 6 } 7 else 8 { 9 UE_LOG(LogPawn, Error, TEXT("DisableInput can only be specified on a Pawn for its Controller")); 10 } 11}