UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BehaviorTreeComponent.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
7#include "EngineDefines.h"
10#include "AITypes.h"
11#include "BrainComponent.h"
13#include "BehaviorTreeComponent.generated.h"
14
15class FBehaviorTreeDebugger;
16class UBehaviorTree;
19class UBTDecorator;
20class UBTNode;
23class UBTTaskNode;
24
50
52{
55
58
61
63 bool IsSet() const { return (NextTask || bOutOfNodes) && !bLocked; }
64 bool IsLocked() const { return bLocked; }
65
66 void Lock() { bLocked = true; }
67 void Unlock() { bLocked = false; }
68};
69
80
102
103UCLASS(ClassGroup = AI, meta = (BlueprintSpawnableComponent), MinimalAPI)
105{
107
108 // UActorComponent overrides
109 AIMODULE_API virtual void RegisterComponentTickFunctions(bool bRegister) override;
110 AIMODULE_API virtual void SetComponentTickEnabled(bool bEnabled) override;
111
112 // Begin UBrainComponent overrides
113 AIMODULE_API virtual void StartLogic() override;
114 AIMODULE_API virtual void RestartLogic() override;
115 AIMODULE_API virtual void StopLogic(const FString& Reason) override;
116 AIMODULE_API virtual void PauseLogic(const FString& Reason) override;
117 AIMODULE_API virtual EAILogicResuming::Type ResumeLogic(const FString& Reason) override;
118
120 AIMODULE_API bool TreeHasBeenStarted() const;
121
122public:
125
126 AIMODULE_API virtual bool IsRunning() const override;
127 AIMODULE_API virtual bool IsPaused() const override;
128 AIMODULE_API virtual void Cleanup() override;
129 AIMODULE_API virtual void HandleMessage(const FAIMessage& Message) override;
130 // End UBrainComponent overrides
131
132 // Begin UActorComponent overrides
133 AIMODULE_API virtual void UninitializeComponent() override;
134 // End UActorComponent overrides
135
138
141
147
149 AIMODULE_API void RequestExecution(const UBTCompositeNode* RequestedOn, const int32 InstanceIdx,
151 const EBTNodeResult::Type ContinueWithResult, bool bStoreForDebugger = true);
152
154 void RequestExecution(const UBTDecorator* RequestedBy) { check(RequestedBy); RequestBranchEvaluation(*RequestedBy); }
155
157 void RequestExecution(EBTNodeResult::Type ContinueWithResult) { RequestBranchEvaluation(ContinueWithResult); }
158
160 UE_DEPRECATED(5.0, "This function is deprecated. Please use RequestBranchDeactivation instead.")
161 AIMODULE_API void RequestUnregisterAuxNodesInBranch(const UBTCompositeNode* Node);
162
164 AIMODULE_API void RequestBranchEvaluation(EBTNodeResult::Type ContinueWithResult);
165
167 AIMODULE_API void RequestBranchEvaluation(const UBTDecorator& RequestedBy);
168
170 AIMODULE_API void RequestBranchActivation(const UBTDecorator& RequestedBy, const bool bRequestEvenIfExecuting);
171
173 AIMODULE_API void RequestBranchDeactivation(const UBTDecorator& RequestedBy);
174
176 AIMODULE_API void OnTaskFinished(const UBTTaskNode* TaskNode, EBTNodeResult::Type TaskResult);
177
179 AIMODULE_API void RegisterMessageObserver(const UBTTaskNode* TaskNode, FName MessageType);
180 AIMODULE_API void RegisterMessageObserver(const UBTTaskNode* TaskNode, FName MessageType, FAIRequestID MessageID);
181
183 AIMODULE_API void UnregisterMessageObserversFrom(const UBTTaskNode* TaskNode);
184 AIMODULE_API void UnregisterMessageObserversFrom(const FBTNodeIndex& TaskIdx);
185
187 AIMODULE_API void RegisterParallelTask(const UBTTaskNode* TaskNode);
188
190 AIMODULE_API void UnregisterParallelTask(const UBTTaskNode* TaskNode, uint16 InstanceIdx);
191
193 AIMODULE_API void UnregisterAuxNodesUpTo(const FBTNodeIndex& Index);
194
196 AIMODULE_API void UnregisterAuxNodesInRange(const FBTNodeIndex& FromIndex, const FBTNodeIndex& ToIndex);
197
199 AIMODULE_API void UnregisterAuxNodesInBranch(const UBTCompositeNode* Node, bool bApplyImmediately = true);
200
202 AIMODULE_API virtual void TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override;
206 AIMODULE_API void ScheduleNextTick(float NextDeltaTime);
207
209 AIMODULE_API void ProcessExecutionRequest();
210
212 AIMODULE_API void ScheduleExecutionUpdate();
213
215 AIMODULE_API int32 FindInstanceContainingNode(const UBTNode* Node) const;
216
218 AIMODULE_API UBTNode* FindTemplateNode(const UBTNode* Node) const;
219
221 UBehaviorTree* GetCurrentTree() const;
222
224 UBehaviorTree* GetRootTree() const;
225
227 const UBTNode* GetActiveNode() const;
228
230 uint16 GetActiveInstanceIdx() const;
231
233 AIMODULE_API uint8* GetNodeMemory(const UBTNode* Node, int32 InstanceIdx) const;
234
236 bool IsRestartPending() const;
237
239 bool IsAbortPending() const;
240
242 AIMODULE_API bool IsExecutingBranch(const UBTNode* Node, int32 ChildIndex = -1) const;
243
245 AIMODULE_API bool IsAuxNodeActive(const UBTAuxiliaryNode* AuxNode) const;
246 AIMODULE_API bool IsAuxNodeActive(const UBTAuxiliaryNode* AuxNodeTemplate, int32 InstanceIdx) const;
247
249 bool IsInstanceStackEmpty() const
250 {
251 return (InstanceStack.Num() == 0);
252 }
253
256 {
257 return AccumulatedTickDeltaTime;
258 }
259
261 AIMODULE_API EBTTaskStatus::Type GetTaskStatus(const UBTTaskNode* TaskNode) const;
262
263 AIMODULE_API virtual FString GetDebugInfoString() const override;
264 AIMODULE_API virtual FString DescribeActiveTasks() const;
265 AIMODULE_API virtual FString DescribeActiveTrees() const;
266
268 UFUNCTION(BlueprintCallable, Category = "AI|Logic")
269 AIMODULE_API double GetTagCooldownEndTime(FGameplayTag CooldownTag) const;
270
272 UFUNCTION(BlueprintCallable, Category = "AI|Logic")
273 AIMODULE_API void AddCooldownTagDuration(FGameplayTag CooldownTag, float CooldownDuration, bool bAddToExistingDuration);
274
276 UFUNCTION(BlueprintCallable, Category="AI|Logic")
277 AIMODULE_API virtual void SetDynamicSubtree(FGameplayTag InjectTag, UBehaviorTree* BehaviorAsset);
278
281
283 AIMODULE_API void ForEachChildTask(TFunctionRef<void (UBTTaskNode&, const FBehaviorTreeInstance&, int32 InstanceIndex)> Functor);
284
286 AIMODULE_API void ForEachChildTask(UBTCompositeNode& StartNode, int32 InstanceIndex, TFunctionRef<void(UBTTaskNode&, const FBehaviorTreeInstance&, int32 InstanceIndex)> Functor);
287// Code for timing BT Search for FramePro
288#if !UE_BUILD_SHIPPING
289 static AIMODULE_API void EndFrame();
290#endif
291
292#if ENABLE_VISUAL_LOG
293 AIMODULE_API virtual void DescribeSelfToVisLog(struct FVisualLogEntry* Snapshot) const override;
294#endif
295
296#if CSV_PROFILER_STATS
299#endif
300
301 static AIMODULE_API void AddReferencedObjects(UObject* InThis, FReferenceCollector& Collector);
302
303protected:
306
309
312 TArray<TObjectPtr<UBTNode>> NodeInstances;
313
316
318 FBTNodeExecutionInfo ExecutionRequest;
319
321 FBTPendingExecutionInfo PendingExecution;
322
341
342 /* Type of suspended branch action */
344
347
350
353
356
357#if USE_BEHAVIORTREE_DEBUGGER
363
366
369#endif
370
371 // Code for timing BT Search for FramePro
372#if !UE_BUILD_SHIPPING
376#endif
377
380
383
386
389
392
395
398
401 AIMODULE_API bool PushInstance(UBehaviorTree& TreeAsset);
402
404 AIMODULE_API uint8 UpdateInstanceId(UBehaviorTree* TreeAsset, const UBTNode* OriginNode, int32 OriginInstanceIdx);
405
407 AIMODULE_API void RemoveAllInstances();
408
410 AIMODULE_API void CopyInstanceMemoryToPersistent();
411
413 AIMODULE_API void CopyInstanceMemoryFromPersistent();
414
416 AIMODULE_API virtual void OnTreeFinished();
417
419 AIMODULE_API void ApplySearchData(UBTNode* NewActiveNode);
420
422 AIMODULE_API void ApplyDiscardedSearch();
423
425 UE_DEPRECATED(5.4, "This function is deprecated. Please use ApplyAllSearchUpdates instead.")
426 AIMODULE_API void ApplySearchUpdates(const TArray<FBehaviorTreeSearchUpdate>& UpdateList, int32 NewNodeExecutionIndex, bool bPostUpdate = false);
427
433 AIMODULE_API void ApplyAllSearchUpdates(const TArray<FBehaviorTreeSearchUpdate>& UpdateList, int32 NewNodeExecutionIndex, bool bDoPostUpdate = true, bool bAllowTaskUpdates = true);
434
436 AIMODULE_API void AbortCurrentTask();
437
439 AIMODULE_API void ExecuteTask(UBTTaskNode* TaskNode);
440
442 AIMODULE_API bool DeactivateUpTo(const UBTCompositeNode* Node, uint16 NodeInstanceIdx, EBTNodeResult::Type& NodeResult, int32& OutLastDeactivatedChildIndex);
443
445 AIMODULE_API bool TrackPendingLatentAborts();
446
448 AIMODULE_API void TrackNewLatentAborts();
449
451 AIMODULE_API bool HasActiveLatentAborts() const;
452
454 AIMODULE_API void ProcessPendingExecution();
455
458
460 AIMODULE_API void RollbackSearchChanges();
461
463 AIMODULE_API void StoreDebuggerExecutionStep(EBTExecutionSnap::Type SnapType);
464
467 AIMODULE_API void StoreDebuggerRemovedInstance(uint16 InstanceIdx) const;
468
470 AIMODULE_API void StoreDebuggerSearchStep(const UBTNode* Node, uint16 InstanceIdx, EBTNodeResult::Type NodeResult) const;
471 AIMODULE_API void StoreDebuggerSearchStep(const UBTNode* Node, uint16 InstanceIdx, bool bPassed) const;
472
474 AIMODULE_API void StoreDebuggerRestart(const UBTNode* Node, uint16 InstanceIdx, bool bAllowed);
475
477 AIMODULE_API void StoreDebuggerBlackboard(TMap<FName, FString>& BlackboardValueDesc) const;
478
480 AIMODULE_API void StoreDebuggerRuntimeValues(TArray<FString>& RuntimeDescriptions, UBTNode* RootNode, uint16 InstanceIdx) const;
481
483 AIMODULE_API void UpdateDebuggerAfterExecution(const UBTTaskNode* TaskNode, uint16 InstanceIdx) const;
484
486 static AIMODULE_API bool IsDebuggerActive();
487
489 AIMODULE_API EBTNodeRelativePriority CalculateRelativePriority(const UBTNode* NodeA, const UBTNode* NodeB) const;
490
492 AIMODULE_API void EvaluateBranch(EBTNodeResult::Type LastResult);
493
495 AIMODULE_API void EvaluateBranch(const UBTDecorator& RequestedBy);
496
498 AIMODULE_API void ActivateBranch(const UBTDecorator& RequestedBy, bool bRequestEvenIfNotExecuting);
499
501 AIMODULE_API void DeactivateBranch(const UBTDecorator& RequestedBy);
502
504 AIMODULE_API void SuspendBranchActions(EBTBranchAction BranchActions = EBTBranchAction::All);
505
507 AIMODULE_API void ResumeBranchActions();
508
509 UE_DEPRECATED(5.1, "This function is deprecated. Please use SuspendBranchActions instead.")
510 void SuspendBranchDeactivation() { SuspendBranchActions(); }
511
512 UE_DEPRECATED(5.1, "This function is deprecated. Please use ResumeBranchActions instead.")
513 void ResumeBranchDeactivation() { ResumeBranchActions(); }
514
528
529 void TickNewlyAddedAuxNodesHelper();
530
531 UE_DEPRECATED(5.1, "This struct is deprecated. Please use FBTSuspendBranchActionsScoped instead.")
533
539 friend FBehaviorTreeDebugger;
541
542private:
544 void ApplySearchUpdatesImpl(const TArray<FBehaviorTreeSearchUpdate>& UpdateList, int32 NewNodeExecutionIndex, bool bPostUpdate, bool bAllowTaskUpdates);
545
548 UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = AI)
549 TObjectPtr<UBehaviorTree> DefaultBehaviorTreeAsset;
550
552 bool bTickedOnce = false;
553
555 float NextTickDeltaTime = 0.f;
556
558 float AccumulatedTickDeltaTime = 0.f;
559
561 float CurrentFrameDeltaTime = 0.f;
562
564 double LastRequestedDeltaTimeGameTime = 0;
565
566#if CSV_PROFILER_STATS
568 const char* CSVTickStatName = "BehaviorTreeTick";
569#endif
570};
571
573// Inlines
574
576{
577 return InstanceStack.Num() ? KnownInstances[InstanceStack[ActiveInstanceIdx].InstanceIdIndex].TreeAsset : NULL;
578}
579
581{
582 return InstanceStack.Num() ? KnownInstances[InstanceStack[0].InstanceIdIndex].TreeAsset : NULL;
583}
584
586{
587 return InstanceStack.Num() ? InstanceStack[ActiveInstanceIdx].ActiveNode : NULL;
588}
589
594
599
OODEFFUNC typedef const int const char * function
Definition oodle2.h:710
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define NULL
Definition oodle2base.h:134
#define check(expr)
Definition AssertionMacros.h:314
EBTBranchAction
Definition BehaviorTreeComponent.h:82
@ DecoratorActivate_EvenIfExecuting
@ DecoratorActivate_IfNotExecuting
EBTRestartMode
Definition BehaviorTreeTypes.h:118
EBTNodeRelativePriority
Definition BehaviorTreeTypes.h:224
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
ELevelTick
Definition EngineBaseTypes.h:70
#define ENUM_CLASS_FLAGS(Enum)
Definition EnumClassFlags.h:6
return true
Definition ExternalRpcRegistry.cpp:601
FORCEINLINE uint32 ToIndex(FHairStrandsTiles::ETileType Type)
Definition HairStrandsData.h:93
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define UFUNCTION(...)
Definition ObjectMacros.h:745
#define GENERATED_UCLASS_BODY(...)
Definition ObjectMacros.h:768
#define UCLASS(...)
Definition ObjectMacros.h:776
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition NameTypes.h:617
Definition UnrealType.h:3087
Definition UObjectGlobals.h:2492
Definition ObjectMacros.h:180
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
Definition EnumAsByte.h:22
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition BTAuxiliaryNode.h:31
Definition BTCompositeNode.h:88
Definition BTDecorator.h:38
Definition BTNode.h:48
Definition BTTaskNode.h:34
Definition BTTask_RunBehaviorDynamic.h:22
Definition BTTask_RunBehavior.h:24
Definition BehaviorTreeComponent.h:105
EBTBranchAction SuspendedBranchActions
Definition BehaviorTreeComponent.h:343
float GetAccumulatedTickDeltaTime() const
Definition BehaviorTreeComponent.h:255
TArray< FBehaviorTreeInstanceId > KnownInstances
Definition BehaviorTreeComponent.h:308
uint16 ActiveInstanceIdx
Definition BehaviorTreeComponent.h:379
FBTTreeStartInfo TreeStartInfo
Definition BehaviorTreeComponent.h:349
static AIMODULE_API int32 NumSearchTimeCalls
Definition BehaviorTreeComponent.h:375
void RequestExecution(const UBTDecorator *RequestedBy)
Definition BehaviorTreeComponent.h:154
TArray< FBranchActionInfo > PendingBranchActionRequests
Definition BehaviorTreeComponent.h:346
uint8 bLoopExecution
Definition BehaviorTreeComponent.h:382
UBehaviorTree * GetCurrentTree() const
Definition BehaviorTreeComponent.h:575
uint8 bIsRunning
Definition BehaviorTreeComponent.h:394
static AIMODULE_API bool bAddedEndFrameCallback
Definition BehaviorTreeComponent.h:373
uint8 bIsPaused
Definition BehaviorTreeComponent.h:397
bool IsAbortPending() const
Definition BehaviorTreeComponent.h:600
TMap< FGameplayTag, double > CooldownTagsMap
Definition BehaviorTreeComponent.h:355
const UBTNode * GetActiveNode() const
Definition BehaviorTreeComponent.h:585
uint8 bRequestedStop
Definition BehaviorTreeComponent.h:391
bool IsRestartPending() const
Definition BehaviorTreeComponent.h:595
uint8 bRequestedFlowUpdate
Definition BehaviorTreeComponent.h:388
uint8 bWaitingForLatentAborts
Definition BehaviorTreeComponent.h:385
UBehaviorTree * GetRootTree() const
Definition BehaviorTreeComponent.h:580
uint16 GetActiveInstanceIdx() const
Definition BehaviorTreeComponent.h:590
FBTPendingExecutionInfo PendingExecution
Definition BehaviorTreeComponent.h:321
TMultiMap< FBTNodeIndex, FAIMessageObserverHandle > TaskMessageObservers
Definition BehaviorTreeComponent.h:352
void RequestExecution(EBTNodeResult::Type ContinueWithResult)
Definition BehaviorTreeComponent.h:157
FBTNodeExecutionInfo ExecutionRequest
Definition BehaviorTreeComponent.h:318
static AIMODULE_API double FrameSearchTime
Definition BehaviorTreeComponent.h:374
TArray< FBehaviorTreeInstance > InstanceStack
Definition BehaviorTreeComponent.h:305
Definition BehaviorTree.h:16
Definition BrainComponent.h:118
Definition Object.h:95
Type
Definition AITypes.h:116
Definition BehaviorTreeTypes.h:100
Type
Definition BehaviorTreeTypes.h:102
@ Looped
Definition BehaviorTreeTypes.h:104
Definition BehaviorTreeTypes.h:206
Definition BehaviorTreeTypes.h:84
Type
Definition BehaviorTreeTypes.h:87
@ Succeeded
Definition BehaviorTreeTypes.h:89
Type
Definition BehaviorTreeTypes.h:111
@ Safe
Definition BehaviorTreeTypes.h:112
Type
Definition BehaviorTreeTypes.h:171
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
Definition BrainComponent.h:27
Definition AITypes.h:403
Definition EngineBaseTypes.h:571
Definition BehaviorTreeComponent.h:26
FBTNodeIndex SearchEnd
Definition BehaviorTreeComponent.h:31
uint8 bIsRestart
Definition BehaviorTreeComponent.h:46
TObjectPtr< const UBTCompositeNode > ExecuteNode
Definition BehaviorTreeComponent.h:34
FBTNodeIndex SearchStart
Definition BehaviorTreeComponent.h:28
TEnumAsByte< EBTNodeResult::Type > ContinueWithResult
Definition BehaviorTreeComponent.h:40
uint8 bTryNextChild
Definition BehaviorTreeComponent.h:43
uint16 ExecuteInstanceIdx
Definition BehaviorTreeComponent.h:37
FBTNodeExecutionInfo()
Definition BehaviorTreeComponent.h:48
Definition BehaviorTreeTypes.h:450
Definition BehaviorTreeComponent.h:52
TObjectPtr< UBTTaskNode > NextTask
Definition BehaviorTreeComponent.h:54
FBTPendingExecutionInfo()
Definition BehaviorTreeComponent.h:62
bool IsLocked() const
Definition BehaviorTreeComponent.h:64
void Unlock()
Definition BehaviorTreeComponent.h:67
uint32 bLocked
Definition BehaviorTreeComponent.h:60
uint32 bOutOfNodes
Definition BehaviorTreeComponent.h:57
bool IsSet() const
Definition BehaviorTreeComponent.h:63
void Lock()
Definition BehaviorTreeComponent.h:66
Definition BehaviorTreeComponent.h:71
FBTTreeStartInfo()
Definition BehaviorTreeComponent.h:76
bool HasPendingInitialize() const
Definition BehaviorTreeComponent.h:78
UBehaviorTree * Asset
Definition BehaviorTreeComponent.h:72
uint8 bPendingInitialize
Definition BehaviorTreeComponent.h:74
bool IsSet() const
Definition BehaviorTreeComponent.h:77
EBTExecutionMode::Type ExecuteMode
Definition BehaviorTreeComponent.h:73
Definition BehaviorTreeTypes.h:232
Definition BehaviorTreeTypes.h:320
Definition BehaviorTreeTypes.h:534
Definition BehaviorTreeTypes.h:497
Definition GameplayTagContainer.h:45
Definition VisualLoggerTypes.h:205
Definition ObjectPtr.h:488
Definition BehaviorTreeComponent.h:516
FBTSuspendBranchActionsScoped(UBehaviorTreeComponent &InBTComp, EBTBranchAction BranchActions=EBTBranchAction::All)
Definition BehaviorTreeComponent.h:517
UBehaviorTreeComponent & BTComp
Definition BehaviorTreeComponent.h:526
~FBTSuspendBranchActionsScoped()
Definition BehaviorTreeComponent.h:522
Definition BehaviorTreeComponent.h:324
FBranchActionInfo(const EBTBranchAction InAction)
Definition BehaviorTreeComponent.h:325
FBranchActionInfo(const UBTNode *InNode, const EBTBranchAction InAction)
Definition BehaviorTreeComponent.h:328
FBranchActionInfo(const UBTNode *InNode, EBTNodeResult::Type InContinueWithResult, const EBTBranchAction InAction)
Definition BehaviorTreeComponent.h:332
EBTBranchAction Action
Definition BehaviorTreeComponent.h:339