当前位置:网站首页>UE small knowledge point controller possess pawn process
UE small knowledge point controller possess pawn process
2022-07-01 09:25:00 【[ascetic monk]】
void AController::Possess(APawn* InPawn)
{
if (!bCanPossessWithoutAuthority && !HasAuthority())
{
FMessageLog("PIE").Warning(FText::Format(
LOCTEXT("ControllerPossessAuthorityOnly", "Possess function should only be used by the network authority for {0}"),
FText::FromName(GetFName())
));
UE_LOG(LogController, Warning, TEXT("Trying to possess %s without network authority! Request will be ignored."), *GetNameSafe(InPawn));
return;
}
REDIRECT_OBJECT_TO_VLOG(InPawn, this);
const APawn* CurrentPawn = GetPawn();
// A notification is required when the current assigned pawn is not possessed (i.e. pawn assigned before calling Possess)
const bool bNotificationRequired = (CurrentPawn != nullptr && CurrentPawn->GetController() == nullptr);
// To preserve backward compatibility we keep notifying derived classed for null pawn in case some
// overrides decided to react differently when asked to possess a null pawn.
// Default engine implementation is to unpossess the current pawn.
OnPossess(InPawn);
// Notify when pawn to possess (different than the assigned one) has been accepted by the native class or notification is explicitly required
APawn* NewPawn = GetPawn();
if (NewPawn != CurrentPawn || bNotificationRequired)
{
ReceivePossess(NewPawn);
OnNewPawn.Broadcast(NewPawn);
}
}void APlayerController::OnPossess(APawn* PawnToPossess)
{
if ( PawnToPossess != NULL &&
(PlayerState == NULL || !PlayerState->IsOnlyASpectator()) )
{
const bool bNewPawn = (GetPawn() != PawnToPossess);
if (GetPawn() && bNewPawn)
{
UnPossess();
}
if (PawnToPossess->Controller != NULL)
{
PawnToPossess->Controller->UnPossess();
}
PawnToPossess->PossessedBy(this);
// update rotation to match possessed pawn's rotation
SetControlRotation( PawnToPossess->GetActorRotation() );
SetPawn(PawnToPossess);
check(GetPawn() != NULL);
if (GetPawn() && GetPawn()->PrimaryActorTick.bStartWithTickEnabled)
{
GetPawn()->SetActorTickEnabled(true);
}
INetworkPredictionInterface* NetworkPredictionInterface = GetPawn() ? Cast<INetworkPredictionInterface>(GetPawn()->GetMovementComponent()) : NULL;
if (NetworkPredictionInterface)
{
NetworkPredictionInterface->ResetPredictionData_Server();
}
AcknowledgedPawn = NULL;
// Local PCs will have the Restart() triggered right away in ClientRestart (via PawnClientRestart()), but the server should call Restart() locally for remote PCs.
// We're really just trying to avoid calling Restart() multiple times.
if (!IsLocalPlayerController())
{
GetPawn()->DispatchRestart(false);
}
ClientRestart(GetPawn());
ChangeState( NAME_Playing );
if (bAutoManageActiveCameraTarget)
{
AutoManageActiveCameraTarget(GetPawn());
ResetCameraMode();
}
}
}void AController::UnPossess()
{
APawn* CurrentPawn = GetPawn();
// No need to notify if we don't have a pawn
if (CurrentPawn == nullptr)
{
return;
}
OnUnPossess();
// Notify only when pawn has been successfully unpossessed by the native class.
APawn* NewPawn = GetPawn();
if (NewPawn != CurrentPawn)
{
ReceiveUnPossess(CurrentPawn);
OnNewPawn.Broadcast(NewPawn);
}
}void APlayerController::OnUnPossess()
{
if (GetPawn() != NULL)
{
if (GetLocalRole() == ROLE_Authority)
{
GetPawn()->SetReplicates(true);
}
GetPawn()->UnPossessed();
if (GetViewTarget() == GetPawn())
{
SetViewTarget(this);
}
}
SetPawn(NULL);
}
void APawn::UnPossessed()
{
AController* const OldController = Controller;
ForceNetUpdate();
SetPlayerState(nullptr);
SetOwner(nullptr);
Controller = nullptr;
// Unregister input component if we created one
DestroyPlayerInputComponent();
// dispatch Blueprint event if necessary
if (OldController)
{
ReceiveUnpossessed(OldController);
}
NotifyControllerChanged();
ConsumeMovementInputVector();
}void ACharacter::UnPossessed()
{
Super::UnPossessed();
if (CharacterMovement)
{
CharacterMovement->ResetPredictionData_Client();
CharacterMovement->ResetPredictionData_Server();
}
// We're no longer controlled remotely, resume regular ticking of animations.
if (Mesh)
{
Mesh->bOnlyAllowAutonomousTickPose = false;
}
}void APawn::PossessedBy(AController* NewController)
{
SetOwner(NewController);
AController* const OldController = Controller;
Controller = NewController;
ForceNetUpdate();
if (Controller->PlayerState != nullptr)
{
SetPlayerState(Controller->PlayerState);
}
if (APlayerController* PlayerController = Cast<APlayerController>(Controller))
{
if (GetNetMode() != NM_Standalone)
{
SetReplicates(true);
SetAutonomousProxy(true);
}
}
else
{
CopyRemoteRoleFrom(GetDefault<APawn>());
}
// dispatch Blueprint event if necessary
if (OldController != NewController)
{
ReceivePossessed(Controller);
NotifyControllerChanged();
}
}void APlayerController::ClientRestart_Implementation(APawn* NewPawn)
{
UE_LOG(LogPlayerController, Verbose, TEXT("ClientRestart_Implementation %s"), *GetNameSafe(NewPawn));
ResetIgnoreInputFlags();
AcknowledgedPawn = NULL;
SetPawn(NewPawn);
if ( (GetPawn() != NULL) && GetPawn()->GetTearOff() )
{
UnPossess();
SetPawn(NULL);
AcknowledgePossession(GetPawn());
return;
}
if ( GetPawn() == NULL )
{
// We failed to possess, ask server to verify and potentially resend the pawn
ServerCheckClientPossessionReliable();
return;
}
// Only acknowledge non-null Pawns here. ClientRestart is only ever called by the Server for valid pawns,
// but we may receive the function call before Pawn is replicated over, so it will resolve to NULL.
AcknowledgePossession(GetPawn());
GetPawn()->Controller = this;
GetPawn()->DispatchRestart(true);
if (GetLocalRole() < ROLE_Authority)
{
ChangeState( NAME_Playing );
if (bAutoManageActiveCameraTarget)
{
AutoManageActiveCameraTarget(GetPawn());
ResetCameraMode();
}
}
}void APlayerController::AcknowledgePossession(APawn* P)
{
if (Cast<ULocalPlayer>(Player) != NULL)
{
AcknowledgedPawn = P;
if (P != NULL)
{
P->RecalculateBaseEyeHeight();
}
ServerAcknowledgePossession(P);
}
}边栏推荐
- ESP8266 FreeRTOS开发环境搭建
- 富文本实现插值
- Promise asynchronous programming
- Dspic30f6014a LCD block display
- 2022.02.15_ Daily question leetcode six hundred and ninety
- Leetcode daily question brushing record --540 A single element in an ordered array
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- nacos簡易實現負載均衡
- How to launch circle of friends marketing and wechat group activities
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
猜你喜欢

How to realize the usage of connecting multiple databases in idel

MapReduce programming basics

Which method is good for the management of fixed assets of small and medium-sized enterprises?

【电赛训练】红外光通信装置 2013年电赛真题

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据

Implementation and application of queue

nacos服务配置和持久化配置

Understanding and implementation of AVL tree

2.2 【pytorch】torchvision.transforms

【检测技术课案】简易数显电子秤的设计与制作
随机推荐
Imitation of Baidu search results top navigation bar effect
Pain points and solutions of equipment management in large factories
Learning practice: comprehensive application of cycle and branch structure (II)
Naoqi robot summary 28
2022.02.15_ Daily question leetcode six hundred and ninety
Rich text interpolation
How to launch circle of friends marketing and wechat group activities
Which method is good for the management of fixed assets of small and medium-sized enterprises?
[ESP nanny level tutorial] crazy completion chapter - Case: gy906 infrared temperature measurement access card swiping system based on the Internet of things
Set the type of the input tag to number, and remove the up and down arrows
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
JS prototype inheritance can only inherit instances, not constructors
美团2022年机试
Structure de l'arbre - - - arbre binaire 2 traversée non récursive
Closure implementation iterator effect
LeetCode 344. Reverse string
Differences between JS valueof and toString
【电赛训练】红外光通信装置 2013年电赛真题
Wechat applet WebView prohibits page scrolling without affecting the implementation of overflow scrolling in the business
Mysql 优化