Unreal Engine AIController does not reset behavior upon possessing new pawn

So, I’m using a single AI Controller that can change which Pawn it is possessing at a given time.

Upon possession, the first pawn behaves as expected, but after unpossessing it and moving to the second Pawn it seems that the Behavior Tree is not reset. I.e, it does not start again from the Root node.

What I’ve tried so far:

  • Setting the following AIController variables in constructor:
AMyAIController::AMyAIController()

{

  bStartAILogicOnPossess = true;

  bStopAILogicOnUnposses = true;

}
  • Trying to reset the Brain Component / BT Component in OnPossess
void AMyAIController::OnPossess(APawn* InPawn)
{
  Super::OnPossess(InPawn);
  
  if(BrainComponent)
  {
    BrainComponent->StopLogic(TEXT("Possessing a new pawn"));
    BrainComponent->Cleanup();
    BrainComponent->RestartLogic();
  }

  RunBehaviorTree(BehaviorTree);
}

What am I missing? How can I make the Controller reset the BT execution upon possessing a new Pawn?

I found a solution for this problem:

In my OnPossess override, I make sure to first reset the Behavior Tree using the RestartTree function with the EBTRestartMode::CompleteRestart parameter:

void MyAIController::OnPossess(APawn* InPawn)
{
	Super::OnPossess(InPawn);

	if(UBehaviorTreeComponent* BTComponent = Cast<UBehaviorTreeComponent>(BrainComponent))
	{
		BTComponent->RestartTree(EBTRestartMode::CompleteRestart);
	}

	RunBehaviorTree(BehaviorTree);
}

Hope this helps anyone struggling with this!