Pawn,Controller和PawnMovementComponent Tick顺序

执行顺序:

注意, UMovementComponent.bTickBeforeOwner默认为true.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void AController::AddPawnTickDependency(APawn* NewPawn)
{
if (NewPawn != NULL)
{
bool bNeedsPawnPrereq = true;
UPawnMovementComponent* PawnMovement = NewPawn->GetMovementComponent();
if (PawnMovement && PawnMovement->PrimaryComponentTick.bCanEverTick)
{
PawnMovement->PrimaryComponentTick.AddPrerequisite(this, this->PrimaryActorTick);

// Don't need a prereq on the pawn if the movement component already sets up a prereq.
if (PawnMovement->bTickBeforeOwner || NewPawn->PrimaryActorTick.GetPrerequisites().Contains(FTickPrerequisite(PawnMovement, PawnMovement->PrimaryComponentTick)))
{
bNeedsPawnPrereq = false;
}
}

if (bNeedsPawnPrereq)
{
NewPawn->PrimaryActorTick.AddPrerequisite(this, this->PrimaryActorTick);
}
}
}

void UMovementComponent::RegisterComponentTickFunctions(bool bRegister)
{
Super::RegisterComponentTickFunctions(bRegister);

// Super may start up the tick function when we don't want to.
UpdateTickRegistration();

// If the owner ticks, make sure we tick first
AActor* Owner = GetOwner();
if (bTickBeforeOwner && bRegister && PrimaryComponentTick.bCanEverTick && Owner && Owner->CanEverTick())
{
Owner->PrimaryActorTick.AddPrerequisite(this, PrimaryComponentTick);
}
}

从代码上可以看出, Controller Tick始终排在PawnTickPawnMovementTick之前. 如果UMovementComponent.bTickBeforeOwnertrue, 则先执行PawnMovementTick再执行PawnTick, 否则PawnTick先执行, PawnMovementTick后执行.

ActorComponent的Tick注册顺序. 当UMovementComponent.bTickBeforeOwnerfalse时, PawnTick先执行, PawnMovementTick后执行.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Pawn,Controller和PawnMovementComponent Tick顺序
--AActor.BeginPlay
|--// 注册Actor自身tick
|--AActor.RegisterAllActorTickFunctions()
| |--AActor.RegisterActorTickFunctions()
| | |--PrimaryActorTick.SetTickFunctionEnable()
| | |--PrimaryActorTick.RegisterTickFunction()
|--// 针对所有Components, 都执行RegisterTick
|--UActorComponent.RegisterAllComponentTickFunctions()
| |--UCharacterMovementComponent.RegisterComponentTickFunctions()
| | |--UMovementComponent.RegisterComponentTickFunctions()
| | | |--UActorComponent.RegisterComponentTickFunctions()
| | | | |--UActorComponent.SetupActorComponentTickFunction()
| | | | | |--TickFunction->SetTickFunctionEnable()
| | | | | |--TickFunction->RegisterTickFunction()
| |--Component->BeginPlay();