//----------------------------------------------------------------------// // FBehaviorTreeInstance //----------------------------------------------------------------------// void FBehaviorTreeInstance::Initialize(UBehaviorTreeComponent& OwnerComp, UBTCompositeNode& Node, int32& InstancedIndex, EBTMemoryInit::Type InitType) { for (int32 ServiceIndex = 0; ServiceIndex < Node.Services.Num(); ServiceIndex++) { Node.Services[ServiceIndex]->InitializeInSubtree(OwnerComp, Node.Services[ServiceIndex]->GetNodeMemory<uint8>(*this), InstancedIndex, InitType); } uint8* NodeMemory = Node.GetNodeMemory<uint8>(*this); Node.InitializeInSubtree(OwnerComp, NodeMemory, InstancedIndex, InitType); UBTCompositeNode* InstancedComposite = Cast<UBTCompositeNode>(Node.GetNodeInstance(OwnerComp, NodeMemory)); if (InstancedComposite) { InstancedComposite->InitializeComposite(Node.GetLastExecutionIndex()); } for (int32 ChildIndex = 0; ChildIndex < Node.Children.Num(); ChildIndex++) { FBTCompositeChild& ChildInfo = Node.Children[ChildIndex]; for (int32 DecoratorIndex = 0; DecoratorIndex < ChildInfo.Decorators.Num(); DecoratorIndex++) { UBTDecorator* DecoratorOb = ChildInfo.Decorators[DecoratorIndex]; uint8* DecoratorMemory = DecoratorOb->GetNodeMemory<uint8>(*this); DecoratorOb->InitializeInSubtree(OwnerComp, DecoratorMemory, InstancedIndex, InitType); UBTDecorator* InstancedDecoratorOb = Cast<UBTDecorator>(DecoratorOb->GetNodeInstance(OwnerComp, DecoratorMemory)); if (InstancedDecoratorOb) { InstancedDecoratorOb->InitializeDecorator(DecoratorOb->GetChildIndex()); } } if (ChildInfo.ChildComposite) { Initialize(OwnerComp, *(ChildInfo.ChildComposite), InstancedIndex, InitType); } else if (ChildInfo.ChildTask) { ChildInfo.ChildTask->InitializeInSubtree(OwnerComp, ChildInfo.ChildTask->GetNodeMemory<uint8>(*this), InstancedIndex, InitType); } } }
static void InitializeNodeHelper(UBTCompositeNode* ParentNode, UBTNode* NodeOb, uint8 TreeDepth, uint16& ExecutionIndex, TArray<FNodeInitializationData>& InitList, UBehaviorTree& TreeAsset, UObject* NodeOuter) { // special case: subtrees UBTTask_RunBehavior* SubtreeTask = Cast<UBTTask_RunBehavior>(NodeOb); if (SubtreeTask) { ExecutionIndex += SubtreeTask->GetInjectedNodesCount(); } InitList.Add(FNodeInitializationData(NodeOb, ParentNode, ExecutionIndex, TreeDepth, NodeOb->GetInstanceMemorySize(), NodeOb->GetSpecialMemorySize())); NodeOb->InitializeFromAsset(TreeAsset); ExecutionIndex++; UBTCompositeNode* CompositeOb = Cast<UBTCompositeNode>(NodeOb); if (CompositeOb) { for (int32 ServiceIndex = 0; ServiceIndex < CompositeOb->Services.Num(); ServiceIndex++) { if (CompositeOb->Services[ServiceIndex] == NULL) { UE_LOG(LogBehaviorTree, Warning, TEXT("%s has missing service node! (parent: %s)"), *TreeAsset.GetName(), *UBehaviorTreeTypes::DescribeNodeHelper(CompositeOb)); CompositeOb->Services.RemoveAt(ServiceIndex, 1, false); ServiceIndex--; continue; } UBTService* Service = Cast<UBTService>(StaticDuplicateObject(CompositeOb->Services[ServiceIndex], NodeOuter, TEXT("None")));; CompositeOb->Services[ServiceIndex] = Service; InitList.Add(FNodeInitializationData(Service, CompositeOb, ExecutionIndex, TreeDepth, Service->GetInstanceMemorySize(), Service->GetSpecialMemorySize())); Service->InitializeFromAsset(TreeAsset); ExecutionIndex++; } for (int32 ChildIndex = 0; ChildIndex < CompositeOb->Children.Num(); ChildIndex++) { FBTCompositeChild& ChildInfo = CompositeOb->Children[ChildIndex]; for (int32 DecoratorIndex = 0; DecoratorIndex < ChildInfo.Decorators.Num(); DecoratorIndex++) { if (ChildInfo.Decorators[DecoratorIndex] == NULL) { UE_LOG(LogBehaviorTree, Warning, TEXT("%s has missing decorator node! (parent: %s, branch: %d)"), *TreeAsset.GetName(), *UBehaviorTreeTypes::DescribeNodeHelper(CompositeOb), ChildIndex); ChildInfo.Decorators.RemoveAt(DecoratorIndex, 1, false); DecoratorIndex--; continue; } UBTDecorator* Decorator = Cast<UBTDecorator>(StaticDuplicateObject(ChildInfo.Decorators[DecoratorIndex], NodeOuter, TEXT("None"))); ChildInfo.Decorators[DecoratorIndex] = Decorator; InitList.Add(FNodeInitializationData(Decorator, CompositeOb, ExecutionIndex, TreeDepth, Decorator->GetInstanceMemorySize(), Decorator->GetSpecialMemorySize())); Decorator->InitializeFromAsset(TreeAsset); Decorator->InitializeDecorator(ChildIndex); ExecutionIndex++; } UBTNode* ChildNode = NULL; if (ChildInfo.ChildComposite) { ChildInfo.ChildComposite = Cast<UBTCompositeNode>(StaticDuplicateObject(ChildInfo.ChildComposite, NodeOuter, TEXT("None"))); ChildNode = ChildInfo.ChildComposite; } else if (ChildInfo.ChildTask) { ChildInfo.ChildTask = Cast<UBTTaskNode>(StaticDuplicateObject(ChildInfo.ChildTask, NodeOuter, TEXT("None"))); ChildNode = ChildInfo.ChildTask; } if (ChildNode) { InitializeNodeHelper(CompositeOb, ChildNode, TreeDepth + 1, ExecutionIndex, InitList, TreeAsset, NodeOuter); } } CompositeOb->InitializeComposite(ExecutionIndex - 1); } }
void UBTTask_RunBehavior::InjectNodes(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, int32& InstancedIndex) const { if (BehaviorAsset == NULL || BehaviorAsset->RootDecorators.Num() == 0) { return; } const int32 NumInjectedDecorators = BehaviorAsset->RootDecorators.Num(); FBTInstancedNodeMemory* NodeMemoryHeader = (FBTInstancedNodeMemory*)NodeMemory; int32 FirstNodeIdx = NodeMemoryHeader->NodeIdx; uint8* InjectedMemoryBase = NodeMemory + sizeof(FBTInstancedNodeMemory); // initialize on first access if (!OwnerComp.NodeInstances.IsValidIndex(InstancedIndex)) { TArray<uint16> MemoryOffsets; int32 MemorySize = 0; UBehaviorTreeManager::InitializeMemoryHelper(BehaviorAsset->RootDecorators, MemoryOffsets, MemorySize); const int32 AlignedInstanceMemorySize = UBehaviorTreeManager::GetAlignedDataSize(sizeof(FBTInstancedNodeMemory)); // prepare dummy memory block for nodes that won't require instancing and offset it by special data size // InitializeInSubtree will read it through GetSpecialNodeMemory function, which moves pointer back FBTInstancedNodeMemory DummyMemory; uint8* RawDummyMemory = ((uint8*)&DummyMemory) + AlignedInstanceMemorySize; // newly created nodes = full init EBTMemoryInit::Type InitType = EBTMemoryInit::Initialize; NodeMemoryHeader->NodeIdx = InstancedIndex; FirstNodeIdx = InstancedIndex; // create nodes for (int32 Idx = 0; Idx < NumInjectedDecorators; Idx++) { UBTDecorator* DecoratorOb = BehaviorAsset->RootDecorators[Idx]; const bool bUsesInstancing = DecoratorOb->HasInstance(); if (bUsesInstancing) { DecoratorOb->InitializeInSubtree(OwnerComp, InjectedMemoryBase + MemoryOffsets[Idx], InstancedIndex, InitType); } else { DecoratorOb->ForceInstancing(true); DecoratorOb->InitializeInSubtree(OwnerComp, RawDummyMemory, InstancedIndex, InitType); DecoratorOb->ForceInstancing(false); UBTDecorator* InstancedOb = Cast<UBTDecorator>(OwnerComp.NodeInstances.Last()); InstancedOb->InitializeMemory(OwnerComp, InjectedMemoryBase + MemoryOffsets[Idx], InitType); } } // setup their properties uint16 NewExecutionIdx = GetExecutionIndex() - GetInjectedNodesCount(); for (int32 Idx = 0; Idx < NumInjectedDecorators; Idx++) { UBTDecorator* InstancedOb = Cast<UBTDecorator>(OwnerComp.NodeInstances[FirstNodeIdx + Idx]); InstancedOb->InitializeNode(GetParentNode(), NewExecutionIdx, GetMemoryOffset() + MemoryOffsets[Idx], GetTreeDepth() - 1); InstancedOb->MarkInjectedNode(); NewExecutionIdx++; } } else { // restoring existing nodes = partial init EBTMemoryInit::Type InitType = EBTMemoryInit::RestoreSubtree; // initialize memory for injected nodes for (int32 Idx = 0; Idx < NumInjectedDecorators; Idx++) { UBTDecorator* DecoratorOb = BehaviorAsset->RootDecorators[Idx]; UBTDecorator* InstancedOb = Cast<UBTDecorator>(OwnerComp.NodeInstances[FirstNodeIdx + Idx]); const bool bUsesInstancing = DecoratorOb->HasInstance(); if (bUsesInstancing) { InstancedOb->OnInstanceCreated(OwnerComp); } else { uint8* InjectedNodeMemory = InjectedMemoryBase + (InstancedOb->GetMemoryOffset() - GetMemoryOffset()); InstancedOb->InitializeMemory(OwnerComp, InjectedNodeMemory, InitType); } } InstancedIndex += NumInjectedDecorators; } // inject nodes if (GetParentNode()) { const int32 ChildIdx = GetParentNode()->GetChildIndex(*this); if (ChildIdx != INDEX_NONE) { FBTCompositeChild& LinkData = GetParentNode()->Children[ChildIdx]; // check if link already has injected decorators bool bAlreadyInjected = false; for (int32 Idx = 0; Idx < LinkData.Decorators.Num(); Idx++) { if (LinkData.Decorators[Idx] && LinkData.Decorators[Idx]->IsInjected()) { bAlreadyInjected = true; break; } } // add decorators to link const int32 NumOriginalDecorators = LinkData.Decorators.Num(); for (int32 Idx = 0; Idx < NumInjectedDecorators; Idx++) { UBTDecorator* InstancedOb = Cast<UBTDecorator>(OwnerComp.NodeInstances[FirstNodeIdx + Idx]); InstancedOb->InitializeFromAsset(*BehaviorAsset); InstancedOb->InitializeDecorator(ChildIdx); if (!bAlreadyInjected) { LinkData.Decorators.Add(InstancedOb); } } // update composite logic operators if (!bAlreadyInjected && (LinkData.DecoratorOps.Num() || BehaviorAsset->RootDecoratorOps.Num())) { const int32 NumOriginalOps = LinkData.DecoratorOps.Num(); if (NumOriginalDecorators > 0) { // and operator for two groups of composites: original and new one FBTDecoratorLogic MasterAndOp(EBTDecoratorLogic::And, LinkData.DecoratorOps.Num() ? 2 : (NumOriginalDecorators + 1)); LinkData.DecoratorOps.Insert(MasterAndOp, 0); if (NumOriginalOps == 0) { // add Test operations, original link didn't have composite operators for (int32 Idx = 0; Idx < NumOriginalDecorators; Idx++) { FBTDecoratorLogic TestOp(EBTDecoratorLogic::Test, Idx); LinkData.DecoratorOps.Add(TestOp); } } } // add injected operators if (BehaviorAsset->RootDecoratorOps.Num() == 0) { FBTDecoratorLogic InjectedAndOp(EBTDecoratorLogic::And, NumInjectedDecorators); LinkData.DecoratorOps.Add(InjectedAndOp); for (int32 Idx = 0; Idx < NumInjectedDecorators; Idx++) { FBTDecoratorLogic TestOp(EBTDecoratorLogic::Test, NumOriginalDecorators + Idx); LinkData.DecoratorOps.Add(TestOp); } } else { for (int32 Idx = 0; Idx < BehaviorAsset->RootDecoratorOps.Num(); Idx++) { FBTDecoratorLogic InjectedOp = BehaviorAsset->RootDecoratorOps[Idx]; if (InjectedOp.Operation == EBTDecoratorLogic::Test) { InjectedOp.Number += NumOriginalDecorators; } LinkData.DecoratorOps.Add(InjectedOp); } } } #if USE_BEHAVIORTREE_DEBUGGER if (!bAlreadyInjected) { // insert to NextExecutionNode list for debugger UBTNode* NodeIt = GetParentNode(); while (NodeIt && NodeIt->GetNextNode() != this) { NodeIt = NodeIt->GetNextNode(); } if (NodeIt) { NodeIt->InitializeExecutionOrder(OwnerComp.NodeInstances[FirstNodeIdx]); NodeIt = NodeIt->GetNextNode(); for (int32 Idx = 1; Idx < NumInjectedDecorators; Idx++) { NodeIt->InitializeExecutionOrder(OwnerComp.NodeInstances[FirstNodeIdx + Idx]); NodeIt = NodeIt->GetNextNode(); } NodeIt->InitializeExecutionOrder((UBTNode*)this); } } #endif } } }