Is it actually possible to stop the behavior tree from C++?

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.

Try

add include

#include "Blueprint/AIBlueprintHelperLibrary.h" 

and call:

AAIController* AIController = UAIBlueprintHelperLibrary::GetAIController(Enemy);

instead of:

AAIController* AIController = Cast<AAIController>(Enemy->GetController());

Thanks for the quick reply but it’s the same result. I don’t get it, everything else works. Running the tree works. All the casts are successful. But the tree keeps going. The only strange observation, when the enemies are retuned to the pool, the behavior tree does not go over the root node, only the first selector and the wait node.

Are you sure it’s pointing towards the correct enemy when debugging? Perhaps you are debugging another instance?

In my version of the project the behavior tree stops as intended.

You can also debug the closest AI with the " key during runtime and see it’s current behavior tree running.
It should empty out once you call deactivate enemy.

Well, it looks like the display for the behavior tree asset is not getting updated. Because, after returning the NPCs back to the pool and stopping the, if I then close and open the behavior tree asset, all the trees are shown as inactive. Even the debugger needs to be switched off and on again to update. Lesson learned, the hard way.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.