I understand that BeginPlay of the controller is being called before the actual possession, but why?
I know I can resolve this thing in some way but I want to understand why, The cronological order in which Unreal call the functions is a big mystery for me.
Hi Kehel, thanks!
Now it’s all clear, what workaround do you suggest keeping the code in the controller? Maybe some other method I can use? I’m thinking to add custom event or call the code from the Pawn BeginPlay at this point.
I was stuck on this for a few days! I had a NavMesh and verified active with “P”, my placed actor would move, and I was calling SpawnDefaultController() and my behavior tree was running but my spawned actor wouldn’t move. Noticed a warning I had in my logs about not having the pawn after BeginPlay was called.
This helped me understand why I had no pawn on my AI controller subclass in BeginPlay when I would spawn the actor. Didn’t realize the start sequence order would be different. I overrode OnPossess and only called my init function after both BeginPlay and OnPossess occurred to cover both the placed actor and spawned actor cases consistently.
Initially my code was designed to work on actors placed by the level designer and not by spawning.
This response made me aware that I should move my logic from void AShooterAIController::BeginPlay() to void AShooterAIController::OnPossess(APawn* InPawn)
can anyone help me i have solve possessing’s issue but have kind of prroblem my PlayerPawn is valid in beginplay it brokes in ticks that is my spawning ai doesnt have valid aicontoller when i set my ai to posses ai controller place or spawn in the wolrd here is the codes am i missin somethig please point out void AAIShooterController::Tick(float DeltaSeconds)
{
Super::Tick(DeltaSeconds);
APawn* PlayerPawn =Cast( UGameplayStatics::GetPlayerPawn(GetWorld(), 0));
if (!PlayerPawn)
{
UE_LOG(LogTemp, Warning, TEXT("PlayerPawn is valid."));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("PlayerPawn is NULL."));
return; // Exit early to prevent further issues if PlayerPawn is NULL
} here is my spawn actor AAIShooterController* NewPoliceController = GetWorld()->SpawnActor<AAIShooterController>(AAIShooterController::StaticClass(), FTransform::Identity);
if (NewPoliceController)
{
NewPoliceController->Possess(NewPolice);
NewPoliceController->RunBehaviorTree(PoliceBehaviorTree);