AI StopTree & StopLogic cause crash

Hi,

I’m trying to shut down my bot’s AI after the player character is dead. I tried using both StopTree() and BrainComponent->StopLogic(""). The second one doesn’t work because the BrainComponent is NULL. The first one gives me the following error.

Unknown exception - code 00000001 (first/second chance not available)

Assertion failed: (Index >= 0) & (Index < ArrayNum) [File:D:\BuildFarm\buildmachine_++depot+UE4-Releases+4.7\Engine\Source\Runtime\Core\Public\Containers\Array.h] [Line: 679] 
Array index out of bounds: 0 from an array of size 0

KERNELBASE + 35740 bytes
UE4Editor_Core!FOutputDeviceWindowsError::Serialize() + 292 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\windows\windowsplatformoutputdevices.cpp:95]
UE4Editor_Core!FOutputDevice::Logf__VA() + 248 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\misc\outputdevice.cpp:144]
UE4Editor_Core!FDebug::AssertFailed() + 1079 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\misc\outputdevice.cpp:224]
UE4Editor_AIModule!UBehaviorTreeComponent::TickComponent() + 1127 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\aimodule\private\behaviortree\behaviortreecomponent.cpp:845]
UE4Editor_Engine!UActorComponent::ConditionalTickComponent() + 199 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\actorcomponent.cpp:1042]
UE4Editor_Engine!FActorComponentTickFunction::ExecuteTick() + 1001 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\actorcomponent.cpp:602]
UE4Editor_Engine!FTickTaskSequencer::FTickFunctionTask::DoTask() + 214 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\ticktaskmanager.cpp:322]
UE4Editor_Engine!TGraphTask<FTickTaskSequencer::FTickFunctionTask>::ExecuteTask() + 445 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\public\async\taskgraphinterfaces.h:671]
UE4Editor_Core!FTaskThread::ProcessTasks() + 3125 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\async\taskgraph.cpp:428]
UE4Editor_Core!FTaskThread::ProcessTasksUntilQuit() + 77 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\async\taskgraph.cpp:271]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() + 511 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\core\private\async\taskgraph.cpp:984]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() + 917 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\ticktaskmanager.cpp:204]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() + 1182 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\ticktaskmanager.cpp:722]
UE4Editor_Engine!UWorld::RunTickGroup() + 102 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\leveltick.cpp:696]
UE4Editor_Engine!UWorld::Tick() + 3024 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\engine\private\leveltick.cpp:1119]
UE4Editor_UnrealEd!UEditorEngine::Tick() + 5618 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\editor.cpp:1329]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() + 22 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\editor\unrealed\private\unrealedengine.cpp:347]
UE4Editor!FEngineLoop::Tick() + 4179 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launchengineloop.cpp:2257]
UE4Editor!GuardedMain() + 1404 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() + 26 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() + 249 bytes [d:\buildfarm\buildmachine_++depot+ue4-releases+4.7\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__tmainCRTStartup() + 329 bytes [f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c:618]

Can someone tell what I am doing wrong?

Something’s seriously wrong here. First of all, if you’re running behavior tree then your BrainComponent should never be null. How do you make your AI use a specified BT asset? If you’re using RunBehaviorTree then it means you manually instantiate BehaviorTreeComponent for your AI, like in a constructor for example. That’s fine, but if you do that then please make sure to set AIController.BrainComponent to that component as well. And I’ll make a change to RunBehaviorTree to do the same, as a fallback :slight_smile:

The crash you’ve encountered looks like a bug. I’ll patch it up.

So, if you set your brain component to the BT component like I’ve suggested above the StopLogic call should work. Also you can try PauseLogic as well.

Cheers,

Hello ,

In my Bot I have this UBehaviorTree* BotBehavior that I set up inside the editor. Inside my AI Controller I have
UBlackboardComponent* BlackboardComp and UBehaviorTreeComponent* BehaviorComp. This is the rest of the code that shows how they are initialized:

AThirdPersonShooterBotController::AThirdPersonShooterBotController(const FObjectInitializer& ObjectInitializer) 
	: Super(ObjectInitializer)
{
	BlackboardComp = ObjectInitializer.CreateDefaultSubobject<UBlackboardComponent>(this, TEXT("BlackboardComp"));
	BehaviorComp = ObjectInitializer.CreateDefaultSubobject<UBehaviorTreeComponent>(this, TEXT("BehaviorComp"));
}

void AThirdPersonShooterBotController::Possess(class APawn* InPawn)
{
	Super::Possess(InPawn);
	Bot = Cast<AThirdPersonShooterBot>(InPawn);
	if (Bot && Bot->BotBehavior)
	{
		BlackboardComp->InitializeBlackboard(*(Bot->BotBehavior->BlackboardAsset));
	
		EnemyKeyID			= BlackboardComp->GetKeyID("Enemy");
		DestinationID		= BlackboardComp->GetKeyID("Destination");
		IsAttackedID		= BlackboardComp->GetKeyID("IsAttacked");
		IsLowHealthID		= BlackboardComp->GetKeyID("IsLowHealth");
		UseMeleeAttackID	= BlackboardComp->GetKeyID("UseMeleeAttack");
	}
}

void AThirdPersonShooterBotController::Activate()
{
	if (Bot->BotBehavior)
	{
		BehaviorComp->StartTree(*Bot->BotBehavior);
	}
}

Just a quick edit:

I changed a line in my constructor so that I also set the Brain Component. It now looks like this:

BrainComponent = BehaviorComp = ObjectInitializer.CreateDefaultSubobject(this, TEXT("BehaviorComp"));

Basically, it’s the same setup as the one in the Shooter Game. This also allows me to call StopLogic() on the Brain Component without issues. Thanks for your help!