void FBehaviorTreeInstance::Cleanup(UBehaviorTreeComponent& OwnerComp, EBTMemoryClear::Type CleanupType) { FBehaviorTreeInstanceId& Info = OwnerComp.KnownInstances[InstanceIdIndex]; if (Info.FirstNodeInstance >= 0) { const int32 MaxAllowedIdx = OwnerComp.NodeInstances.Num(); const int32 LastNodeIdx = OwnerComp.KnownInstances.IsValidIndex(InstanceIdIndex + 1) ? FMath::Min(OwnerComp.KnownInstances[InstanceIdIndex + 1].FirstNodeInstance, MaxAllowedIdx) : MaxAllowedIdx; for (int32 Idx = Info.FirstNodeInstance; Idx < LastNodeIdx; Idx++) { OwnerComp.NodeInstances[Idx]->OnInstanceDestroyed(OwnerComp); } } CleanupNodes(OwnerComp, *RootNode, CleanupType); // remove memory when instance is destroyed - it will need full initialize anyway if (CleanupType == EBTMemoryClear::Destroy) { Info.InstanceMemory.Empty(); } else { Info.InstanceMemory = InstanceMemory; } }
void FBehaviorTreeInstance::CleanupNodes(UBehaviorTreeComponent& OwnerComp, UBTCompositeNode& Node, EBTMemoryClear::Type CleanupType) { for (int32 ServiceIndex = 0; ServiceIndex < Node.Services.Num(); ServiceIndex++) { Node.Services[ServiceIndex]->CleanupInSubtree(OwnerComp, Node.Services[ServiceIndex]->GetNodeMemory<uint8>(*this), CleanupType); } Node.CleanupInSubtree(OwnerComp, Node.GetNodeMemory<uint8>(*this), CleanupType); for (int32 ChildIndex = 0; ChildIndex < Node.Children.Num(); ChildIndex++) { FBTCompositeChild& ChildInfo = Node.Children[ChildIndex]; for (int32 DecoratorIndex = 0; DecoratorIndex < ChildInfo.Decorators.Num(); DecoratorIndex++) { ChildInfo.Decorators[DecoratorIndex]->CleanupInSubtree(OwnerComp, ChildInfo.Decorators[DecoratorIndex]->GetNodeMemory<uint8>(*this), CleanupType); } if (ChildInfo.ChildComposite) { CleanupNodes(OwnerComp, *(ChildInfo.ChildComposite), CleanupType); } else if (ChildInfo.ChildTask) { for (int32 ServiceIndex = 0; ServiceIndex < ChildInfo.ChildTask->Services.Num(); ServiceIndex++) { ChildInfo.ChildTask->Services[ServiceIndex]->CleanupInSubtree(OwnerComp, ChildInfo.ChildTask->Services[ServiceIndex]->GetNodeMemory<uint8>(*this), CleanupType); } ChildInfo.ChildTask->CleanupInSubtree(OwnerComp, ChildInfo.ChildTask->GetNodeMemory<uint8>(*this), CleanupType); } } }
void FBehaviorTreeInstance::Cleanup(UBehaviorTreeComponent& OwnerComp, EBTMemoryClear::Type CleanupType) { FBehaviorTreeInstanceId& Info = OwnerComp.KnownInstances[InstanceIdIndex]; if (Info.FirstNodeInstance >= 0) { const int32 MaxAllowedIdx = OwnerComp.NodeInstances.Num(); const int32 LastNodeIdx = OwnerComp.KnownInstances.IsValidIndex(InstanceIdIndex + 1) ? FMath::Min(OwnerComp.KnownInstances[InstanceIdIndex + 1].FirstNodeInstance, MaxAllowedIdx) : MaxAllowedIdx; for (int32 Idx = Info.FirstNodeInstance; Idx < LastNodeIdx; Idx++) { OwnerComp.NodeInstances[Idx]->OnInstanceDestroyed(OwnerComp); } } CleanupNodes(OwnerComp, *RootNode, CleanupType); Info.InstanceMemory = InstanceMemory; }