UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
BlackboardComponent.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
13#pragma once
14
15#include "CoreMinimal.h"
18#include "InputCoreTypes.h"
21#include "EngineDefines.h"
23#include "AISystem.h"
25#include "BlackboardComponent.generated.h"
26
27class UBrainComponent;
28
39
40
41UCLASS(ClassGroup = AI, meta = (BlueprintSpawnableComponent), hidecategories = (Sockets, Collision), MinimalAPI)
42class UBlackboardComponent : public UActorComponent
43{
45
46public:
48
50 AIMODULE_API virtual void InitializeComponent() override;
51 AIMODULE_API virtual void UninitializeComponent() override;
55 AIMODULE_API FName GetKeyName(FBlackboard::FKey KeyID) const;
56
58 AIMODULE_API FBlackboard::FKey GetKeyID(const FName& KeyName) const;
59
62
64 AIMODULE_API bool IsKeyInstanceSynced(FBlackboard::FKey KeyID) const;
65
67 AIMODULE_API int32 GetNumKeys() const;
68
70 bool HasValidAsset() const;
71
74
77
80
83
86
88 AIMODULE_API UBrainComponent* GetBrainComponent() const;
89
91 AIMODULE_API UBlackboardData* GetBlackboardAsset() const;
92
95
97 AIMODULE_API bool InitializeBlackboard(UBlackboardData& NewAsset);
98
100 AIMODULE_API virtual bool IsCompatibleWith(const UBlackboardData* TestAsset) const;
101
102 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
103 AIMODULE_API UObject* GetValueAsObject(const FName& KeyName) const;
104
105 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
106 AIMODULE_API UClass* GetValueAsClass(const FName& KeyName) const;
107
108 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
109 AIMODULE_API uint8 GetValueAsEnum(const FName& KeyName) const;
110
111 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
112 AIMODULE_API int32 GetValueAsInt(const FName& KeyName) const;
113
114 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
115 AIMODULE_API float GetValueAsFloat(const FName& KeyName) const;
116
117 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
118 AIMODULE_API bool GetValueAsBool(const FName& KeyName) const;
119
120 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
121 AIMODULE_API FString GetValueAsString(const FName& KeyName) const;
122
123 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
124 AIMODULE_API FName GetValueAsName(const FName& KeyName) const;
125
126 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
127 AIMODULE_API FVector GetValueAsVector(const FName& KeyName) const;
128
129 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
130 AIMODULE_API FRotator GetValueAsRotator(const FName& KeyName) const;
131
132 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
133 AIMODULE_API void SetValueAsObject(const FName& KeyName, UObject* ObjectValue);
134
135 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
136 AIMODULE_API void SetValueAsClass(const FName& KeyName, UClass* ClassValue);
137
138 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
139 AIMODULE_API void SetValueAsEnum(const FName& KeyName, uint8 EnumValue);
140
141 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
142 AIMODULE_API void SetValueAsInt(const FName& KeyName, int32 IntValue);
143
144 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
145 AIMODULE_API void SetValueAsFloat(const FName& KeyName, float FloatValue);
146
147 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
148 AIMODULE_API void SetValueAsBool(const FName& KeyName, bool BoolValue);
149
150 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
151 AIMODULE_API void SetValueAsString(const FName& KeyName, FString StringValue);
152
153 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
154 AIMODULE_API void SetValueAsName(const FName& KeyName, FName NameValue);
155
156 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
158
159 UFUNCTION(BlueprintCallable, Category = "AI|Components|Blackboard")
161
162 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard", Meta=(
163 Tooltip="If the vector value has been set (and not cleared), this function returns true (indicating that the value should be valid). If it's not set, the vector value is invalid and this function will return false. (Also returns false if the key specified does not hold a vector.)"))
164 AIMODULE_API bool IsVectorValueSet(const FName& KeyName) const;
166
168 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
169 AIMODULE_API bool GetLocationFromEntry(const FName& KeyName, FVector& ResultLocation) const;
171
173 UFUNCTION(BlueprintCallable, Category="AI|Components|Blackboard")
176
177 UFUNCTION(BlueprintCallable, Category = "AI|Components|Blackboard")
178 AIMODULE_API void ClearValue(const FName& KeyName);
179 AIMODULE_API void ClearValue(FBlackboard::FKey KeyID);
180
183
184 template<class TDataClass>
185 bool IsKeyOfType(const FName& KeyName) const;
186
187 template<class TDataClass>
188 bool IsKeyOfType(FBlackboard::FKey KeyID) const;
189
190 template<class TDataClass>
191 bool SetValue(const FName& KeyName, typename TDataClass::FDataType Value);
192
193 template<class TDataClass>
194 bool SetValue(FBlackboard::FKey KeyID, typename TDataClass::FDataType Value);
195
196 template<class TDataClass>
197 typename TDataClass::FDataType GetValue(const FName& KeyName) const;
198
199 template<class TDataClass>
200 typename TDataClass::FDataType GetValue(FBlackboard::FKey KeyID) const;
201
203 inline uint8* GetKeyRawData(const FName& KeyName) { return GetKeyRawData(GetKeyID(KeyName)); }
204 inline uint8* GetKeyRawData(FBlackboard::FKey KeyID) { return ValueMemory.Num() && ValueOffsets.IsValidIndex(KeyID) ? (ValueMemory.GetData() + ValueOffsets[KeyID]) : NULL; }
205
206 inline const uint8* GetKeyRawData(const FName& KeyName) const { return GetKeyRawData(GetKeyID(KeyName)); }
207 inline const uint8* GetKeyRawData(FBlackboard::FKey KeyID) const { return ValueMemory.Num() && ValueOffsets.IsValidIndex(KeyID) ? (ValueMemory.GetData() + ValueOffsets[KeyID]) : NULL; }
208
209 PRAGMA_DISABLE_DEPRECATION_WARNINGS // re BlackboardAsset
210 inline bool IsValidKey(FBlackboard::FKey KeyID) const { check(BlackboardAsset); return BlackboardAsset->IsValidKey(KeyID); }
212
215
216 AIMODULE_API FString GetDebugInfoString(EBlackboardDescription::Type Mode) const;
217
219 AIMODULE_API FString DescribeKeyValue(const FName& KeyName, EBlackboardDescription::Type Mode) const;
221
222#if ENABLE_VISUAL_LOG
224 AIMODULE_API virtual void DescribeSelfToVisLog(struct FVisualLogEntry* Snapshot) const;
225#endif
226
227protected:
228
232
236 UPROPERTY(EditDefaultsOnly, Category = AI)
238
240 UE_DEPRECATED_FORGAME(4.26, "Directly accessing BlackboardAsset is not longer supported. Use DefaultBlackboardAsset or InitializeBlackboard to set it and GetBlackboardAsset to retrieve it")
242 TObjectPtr<UBlackboardData> BlackboardAsset;
243
246
249
253
256 {
258 : DelegateHandle(InDelegateHandle)
259 {
260 }
261
263 {
264 return DelegateHandle.GetHandle();
265 }
266
267 FOnBlackboardChangeNotification DelegateHandle;
268 bool bToBeRemoved = false;
269 };
270
271
274
276 mutable int32 ObserversToRemoveCount = 0;
277
280
283
286
289
292
295
298
301
304
305 AIMODULE_API bool ShouldSyncWithBlackboard(UBlackboardComponent& OtherBlackboardComponent) const;
306
307 friend UBlackboardKeyType;
308};
309
311// Inlines
312
313PRAGMA_DISABLE_DEPRECATION_WARNINGS // re BlackboardAsset
314
315inline bool UBlackboardComponent::HasValidAsset() const
316{
317 return BlackboardAsset && BlackboardAsset->IsValid();
318}
319
320template<class TDataClass>
321bool UBlackboardComponent::IsKeyOfType(const FName& KeyName) const
322{
323 const FBlackboard::FKey KeyID = GetKeyID(KeyName);
324 return IsKeyOfType<TDataClass>(KeyID);
325}
326
327template<class TDataClass>
328bool UBlackboardComponent::IsKeyOfType(FBlackboard::FKey KeyID) const
329{
330 const FBlackboardEntry* EntryInfo = BlackboardAsset ? BlackboardAsset->GetKey(KeyID) : nullptr;
331 return (EntryInfo != nullptr) && (EntryInfo->KeyType != nullptr) && (EntryInfo->KeyType->GetClass() == TDataClass::StaticClass());
332}
333
334template<class TDataClass>
335bool UBlackboardComponent::SetValue(const FName& KeyName, typename TDataClass::FDataType Value)
336{
337 const FBlackboard::FKey KeyID = GetKeyID(KeyName);
338 return SetValue<TDataClass>(KeyID, Value);
339}
340
341template<class TDataClass>
342bool UBlackboardComponent::SetValue(FBlackboard::FKey KeyID, typename TDataClass::FDataType Value)
343{
344 const FBlackboardEntry* EntryInfo = BlackboardAsset ? BlackboardAsset->GetKey(KeyID) : nullptr;
345 if ((EntryInfo == nullptr) || (EntryInfo->KeyType == nullptr) || (EntryInfo->KeyType->GetClass() != TDataClass::StaticClass()))
346 {
347 return false;
348 }
349
350 const uint16 DataOffset = EntryInfo->KeyType->HasInstance() ? sizeof(FBlackboardInstancedKeyMemory) : 0;
351 uint8* RawData = GetKeyRawData(KeyID) + DataOffset;
352 if (RawData)
353 {
354 UBlackboardKeyType* KeyOb = EntryInfo->KeyType->HasInstance() ? KeyInstances[KeyID] : EntryInfo->KeyType;
355 const bool bChanged = TDataClass::SetValue((TDataClass*)KeyOb, RawData, Value);
356 if (bChanged)
357 {
358 NotifyObservers(KeyID);
359 if (BlackboardAsset->HasSynchronizedKeys() && IsKeyInstanceSynced(KeyID))
360 {
361 UAISystem* AISystem = UAISystem::GetCurrentSafe(GetWorld());
362 for (auto Iter = AISystem->CreateBlackboardDataToComponentsIterator(*BlackboardAsset); Iter; ++Iter)
363 {
364 UBlackboardComponent* OtherBlackboard = Iter.Value();
366 {
367 UBlackboardData* const OtherBlackboardAsset = OtherBlackboard->GetBlackboardAsset();
370 {
371 UBlackboardKeyType* OtherKeyOb = EntryInfo->KeyType->HasInstance() ? OtherBlackboard->KeyInstances[OtherKeyID] : EntryInfo->KeyType;
372 uint8* OtherRawData = OtherBlackboard->GetKeyRawData(OtherKeyID) + DataOffset;
373
374 TDataClass::SetValue((TDataClass*)OtherKeyOb, OtherRawData, Value);
375 OtherBlackboard->NotifyObservers(OtherKeyID);
376 }
377 }
378 }
379 }
380 }
381
382 return true;
383 }
384
385 return false;
386}
387
388template<class TDataClass>
389typename TDataClass::FDataType UBlackboardComponent::GetValue(const FName& KeyName) const
390{
391 const FBlackboard::FKey KeyID = GetKeyID(KeyName);
392 return GetValue<TDataClass>(KeyID);
393}
394
395template<class TDataClass>
396typename TDataClass::FDataType UBlackboardComponent::GetValue(FBlackboard::FKey KeyID) const
397{
398 const FBlackboardEntry* EntryInfo = BlackboardAsset ? BlackboardAsset->GetKey(KeyID) : nullptr;
399 if ((EntryInfo == nullptr) || (EntryInfo->KeyType == nullptr) || (EntryInfo->KeyType->GetClass() != TDataClass::StaticClass()))
400 {
401 return TDataClass::InvalidValue;
402 }
403
404 UBlackboardKeyType* KeyOb = EntryInfo->KeyType->HasInstance() ? KeyInstances[KeyID] : EntryInfo->KeyType;
405 const uint16 DataOffset = EntryInfo->KeyType->HasInstance() ? sizeof(FBlackboardInstancedKeyMemory) : 0;
406
407 const uint8* RawData = GetKeyRawData(KeyID) + DataOffset;
408 return RawData ? TDataClass::GetValue((TDataClass*)KeyOb, RawData) : TDataClass::InvalidValue;
409}
410
411PRAGMA_ENABLE_DEPRECATION_WARNINGS // re BlackboardAsset
412
438template<typename TBlackboardKey>
440{
441private:
442 FBlackboard::FKey BBKey;
443 typedef typename TBlackboardKey::FDataType FStoredType;
444 FStoredType CachedValue;
445public:
446 FBBKeyCachedAccessor() : BBKey(FBlackboard::InvalidKey), CachedValue(TBlackboardKey::InvalidValue)
447 {}
448
450 {
452 if (ensure(BBComponent.IsKeyOfType<TBlackboardKey>(InBBKey)))
453 {
454 BBKey = InBBKey;
455 CachedValue = BBComponent.GetValue<TBlackboardKey>(InBBKey);
456 }
457 }
458
459 template<typename T2>
460 inline bool SetValue(UBlackboardComponent& BBComponent, const T2 InValue)
461 {
462 return SetValue(BBComponent, FStoredType(InValue));
463 }
464
466 inline bool SetValue(UBlackboardComponent& BBComponent, const FStoredType InValue)
467 {
469 if (InValue != CachedValue)
470 {
471 CachedValue = InValue;
472 BBComponent.SetValue<TBlackboardKey>(BBKey, InValue);
473 return true;
474 }
475 return false;
476 }
477
478 inline const FStoredType& Get() const
479 {
481 return CachedValue;
482 }
483
484 template<typename T2>
485 inline T2 Get() const
486 {
488 return (T2)CachedValue;
489 }
490
491 bool IsValid() const { return BBKey != FBlackboard::InvalidKey; }
492};
#define NULL
Definition oodle2base.h:134
#define check(expr)
Definition AssertionMacros.h:314
#define ensure( InExpression)
Definition AssertionMacros.h:464
#define UE_DEPRECATED_FORGAME
Definition CoreMiscDefines.h:377
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
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define GENERATED_BODY(...)
Definition ObjectMacros.h:765
#define UFUNCTION(...)
Definition ObjectMacros.h:745
#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 IDelegateInstance.h:14
Definition NameTypes.h:617
Definition UObjectGlobals.h:1292
static COREUOBJECT_API FObjectInitializer & Get()
Definition UObjectGlobals.cpp:5001
Definition Sockets.Build.cs:6
Definition Array.h:670
Definition DelegateSignatureImpl.inl:310
Definition SubclassOf.h:30
Definition AISystem.h:27
static UAISystem * GetCurrentSafe(UWorld *World)
Definition AISystem.h:174
AIMODULE_API FBlackboardDataToComponentsIterator CreateBlackboardDataToComponentsIterator(class UBlackboardData &BlackboardAsset)
Definition AISystem.cpp:267
Definition ActorComponent.h:152
virtual ENGINE_API void InitializeComponent()
Definition ActorComponent.cpp:1589
virtual ENGINE_API void UninitializeComponent()
Definition ActorComponent.cpp:1597
Definition BlackboardData.h:46
Definition BlackboardKeyType.h:24
Definition BrainComponent.h:118
Definition Class.h:3793
Definition Object.h:95
IAnalyticsPropertyStore::EStatusCode SetValue(TGetter &&GetterFn, TSetter &&SetterFn, const T &ProposedValue, TCompare &&ConditionFn)
Definition AnalyticsPropertyStore.cpp:34
Type
Definition BlackboardKeyEnums.h:10
Definition BlackboardComponent.h:30
Type
Definition BlackboardComponent.h:32
@ OnlyValue
Definition BlackboardComponent.h:33
@ Full
Definition BlackboardComponent.h:36
@ DetailedKeyWithValue
Definition BlackboardComponent.h:35
@ KeyWithValue
Definition BlackboardComponent.h:34
Category
Definition DiffResults.h:63
Definition BlackboardKey.h:8
constexpr FKey InvalidKey
Definition BlackboardKey.h:33
T::FDataType GetValue(const UBlackboardComponent &Blackboard, const FName &Name, FBlackboard::FKey &InOutCachedKey, const typename T::FDataType &DefaultValue)
Definition ValueOrBBKey.h:51
UTransformableHandle * GetHandle(UObject *InObject, const FName &InSocketName)
Definition TransformConstraint.cpp:65
@ BlueprintCallable
This function can be called from blueprint code and should be exposed to the user of blueprint editin...
Definition ObjectMacros.h:987
Definition BlackboardComponent.h:440
bool SetValue(UBlackboardComponent &BBComponent, const FStoredType InValue)
Definition BlackboardComponent.h:466
FBBKeyCachedAccessor()
Definition BlackboardComponent.h:446
bool IsValid() const
Definition BlackboardComponent.h:491
bool SetValue(UBlackboardComponent &BBComponent, const T2 InValue)
Definition BlackboardComponent.h:460
FBBKeyCachedAccessor(const UBlackboardComponent &BBComponent, FBlackboard::FKey InBBKey)
Definition BlackboardComponent.h:449
T2 Get() const
Definition BlackboardComponent.h:485
const FStoredType & Get() const
Definition BlackboardComponent.h:478
Definition BlackboardData.h:15
TObjectPtr< UBlackboardKeyType > KeyType
Definition BlackboardData.h:31
Definition BlackboardKeyType.h:17
Definition BlackboardKey.h:18
Definition VisualLoggerTypes.h:205
Definition ObjectPtr.h:488