I made a pool for my enemy NPCs. When returning them to the pool I’m deactivating everything. Everything works except stopping the Behavior Tree. The tree is still simulating, running and showing up in the debug panel. BrainComponent->StopLogic doesn’t seem to be implemented either.
void UEnemyPoolManager::DeactivateEnemy(AEnemy* Enemy)
{
// Stop AI behavior tree
AAIController* AIController = Cast<AAIController>(Enemy->GetController());
if (AIController)
{
UBrainComponent* BrainComp = AIController->GetBrainComponent();
if (BrainComp)
{
UBehaviorTreeComponent* BehaviorComp = Cast<UBehaviorTreeComponent>(BrainComp);
if (BehaviorComp)
{
BehaviorComp->StopTree(EBTStopMode::Forced); // Stop the behavior tree safely
BehaviorComp->Cleanup(); // Clean up the behavior tree component
}
}
// Detach the AI controller from the pawn
AIController->UnPossess();
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Green, TEXT("UnPossessed"));
}
Enemy->SetActorHiddenInGame(true);
Enemy->UnregisterStimulusSource();
// Stop movement and animations
Enemy->GetCharacterMovement()->StopMovementImmediately();
Enemy->GetMesh()->bPauseAnims = true;
Enemy->GetMesh()->bNoSkeletonUpdate = true;
// Disable collision
Enemy->SetActorEnableCollision(false);
// Disable ticking
Enemy->SetActorTickEnabled(false);
// Deactivate components
TArray<UActorComponent*> Components;
Enemy->GetComponents<UActorComponent>(Components);
for (UActorComponent* Component : Components)
{
Component->Deactivate();
}
}
4o doesn’t know the answer either.