UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MassObserverNotificationTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "MassEntityHandle.h"
6#include "Misc/TVariant.h"
8#include "MassEntityTypes.h"
9
10#define UE_API MASSENTITY_API
11
12#define UE_CHECK_OWNER_THREADID() checkf(OwnerThreadId == FPlatformTLS::GetCurrentThreadId(), TEXT("%hs: all FObserverLock operations are expected to be run in a single thread"), __FUNCTION__)
13
15namespace UE::Mass
16{
17 struct FProcessingContext;
18 namespace ObserverManager
19 {
20 enum class
21 UE_DEPRECATED(5.7, "The type is no longer being used, replaced by EMassObservedOperation")
23 {
24 Add = static_cast<uint8>(EMassObservedOperation::Add),
25 Remove = static_cast<uint8>(EMassObservedOperation::Remove),
26 Create
27 };
28
39 {
41 {
42 };
48
49 template<typename TComposition>
56
57 template<typename TComposition, typename TEntities>
64
65 template<typename TEntities>
72
79
84
85 void AddHandle(const FMassEntityHandle EntityHandle)
86 {
88 if (StoredCollection == nullptr)
89 {
90 StoredCollection = &ConvertStoredHandleToCollection(TConstArrayView<FMassEntityHandle>());
91 }
92 StoredCollection->AddHandle(EntityHandle);
93 }
94
95 inline void AppendEntities(const FMassEntityHandle EntityHandle)
96 {
97 AddHandle(EntityHandle);
98 }
99
101 {
103 {
104 StoredCollection->AppendHandles(InEntityHandles);
105 }
106 else
107 {
108 ConvertStoredHandleToCollection(InEntityHandles);
109 }
110 }
111
113 {
115 {
117 }
118 else
119 {
120 ConvertStoredHandleToCollection(InEntityHandles);
121 // we're ignoring EntityCollection since the collections will need to be rebuilt anyway,
122 // due to AffectedEntities already containing some data before this call
123 }
124 }
125
126 template<typename T> requires std::is_same_v<typename TDecay<T>::Type, FMassArchetypeEntityCollection>
128 {
130 {
132 }
133 else
134 {
135 ConvertStoredHandleToCollection(Forward<T>(InEntityCollection));
136 }
137 }
138
146
148 {
149 if (A.GetIndex() == B.GetIndex())
150 {
151 switch (A.GetIndex())
152 {
153 case FCompositionDescription::IndexOfType<FEmptyComposition>():
154 return true;
155 case FCompositionDescription::IndexOfType<FMassArchetypeCompositionDescriptor>():
157 case FCompositionDescription::IndexOfType<FMassFragmentBitSet>():
158 return A.Get<FMassFragmentBitSet>() == B.Get<FMassFragmentBitSet>();
159 case FCompositionDescription::IndexOfType<FMassTagBitSet>():
160 return A.Get<FMassTagBitSet>() == B.Get<FMassTagBitSet>();
161 default:
162 return false;
163 }
164 }
165 return false;
166 }
167
168 private:
169 template<typename TEntities>
170 FEntityCollection& ConvertStoredHandleToCollection(TEntities&& InEntities)
171 {
172 // AffectedEntities holds a single handle. We need to extract it and emplace a FEntityCollection instance
177 return StoredEntities;
178 }
179 };
180
183 {
184 bool IsSet() const
185 {
186 return OpIndex != INDEX_NONE;
187 }
188
189 operator int() const
190 {
191 return OpIndex;
192 }
193
194 private:
196
202 uint32 SerialNumber = 0;
203
204 int32 OpIndex = INDEX_NONE;
205 };
206
221 {
222 FObserverLock() = default;
224
226 {
227 return WeakEntityManager;
228 }
229
231 {
232 ensureMsgf(CreationHandle == CreationNotificationIndex, TEXT("Given creation handle doesn't match this Lock's data"));
233 checkf(BufferedNotifications.IsValidIndex(CreationHandle), TEXT("Given CreationHandle doesn't match stored notifications"));
234 BufferedNotifications[CreationHandle].DirtyAffectedEntities();
235 }
236
238 {
239 ensureMsgf(CreationHandle == CreationNotificationIndex, TEXT("Given creation handle doesn't match this Lock's data"));
240 checkf(BufferedNotifications.IsValidIndex(CreationHandle), TEXT("Given CreationHandle doesn't match stored notifications"));
241
242 return BufferedNotifications[CreationHandle];
243 }
244
245 private:
246 UE_API explicit FObserverLock(FMassObserverManager& ObserverManager);
247
248 int32 GetOrCreateCreationNotification()
249 {
251 //if (CreationNotificationIndex.compare_exchange_weak(LocalCreationNotificationIndex, BufferedNotifications.Num()))
252 if (CreationNotificationIndex == INDEX_NONE)
253 {
254 CreationNotificationIndex = BufferedNotifications.Num();
255 BufferedNotifications.Emplace(EMassObservedOperation::CreateEntity
258 }
259 return CreationNotificationIndex;
260 }
261
262 bool ReleaseCreationNotification(FCreationNotificationHandle CreationHandle)
263 {
265 checkf(BufferedNotifications.IsValidIndex(CreationHandle), TEXT("Given CreationHandle doesn't match stored notifications"));
266
267 int32 CreationHandleOpIndex = CreationHandle;
268 if (CreationNotificationIndex == CreationHandleOpIndex)
269 {
270 CreationNotificationIndex = INDEX_NONE;
271 return true;
272 }
273 // else
274 ensureMsgf(CreationHandle == CreationNotificationIndex, TEXT("Given creation handle doesn't match this Lock's data"));
275 return false;
276 }
277
278 int32 AddCreatedEntity(FMassEntityHandle CreatedEntity)
279 {
281 if (CreationNotificationIndex == INDEX_NONE)
282 {
283 CreationNotificationIndex = BufferedNotifications.Num();
284 BufferedNotifications.Emplace(EMassObservedOperation::CreateEntity
285 , FBufferedNotification::FEmptyComposition{}
286 , CreatedEntity);
287 }
288 else
289 {
290 BufferedNotifications[CreationNotificationIndex].AddHandle(CreatedEntity);
291 }
292
293 return CreationNotificationIndex;
294 }
295
297 {
299 if (CreationNotificationIndex == INDEX_NONE)
300 {
301 CreationNotificationIndex = BufferedNotifications.Num();
302 BufferedNotifications.Emplace(EMassObservedOperation::CreateEntity
303 , FBufferedNotification::FEmptyComposition{}
305 }
306 else
307 {
308 BufferedNotifications[CreationNotificationIndex].AppendEntities(InCreatedEntities);
309 }
310
311 return CreationNotificationIndex;
312 }
313
315 {
317 if (CreationNotificationIndex == INDEX_NONE)
318 {
319 CreationNotificationIndex = BufferedNotifications.Num();
320 BufferedNotifications.Emplace(EMassObservedOperation::CreateEntity, FBufferedNotification::FEmptyComposition{}
322 }
323 else
324 {
325 BufferedNotifications[CreationNotificationIndex].AppendEntities(InCreatedEntities, Forward<FMassArchetypeEntityCollection>(InEntityCollection));
326 }
327
328 return CreationNotificationIndex;
329 }
330
331 template<typename T> requires std::is_same_v<typename TDecay<T>::Type, FMassArchetypeEntityCollection>
332 int32 AddCreatedEntitiesCollection(T&& InEntityCollection)
333 {
335 if (CreationNotificationIndex == INDEX_NONE)
336 {
337 CreationNotificationIndex = BufferedNotifications.Num();
338 BufferedNotifications.Emplace(EMassObservedOperation::CreateEntity
339 , FBufferedNotification::FEmptyComposition{}
341 }
342 else
343 {
344 BufferedNotifications[CreationNotificationIndex].AppendEntities<T>(InEntityCollection);
345 }
346
347 return CreationNotificationIndex;
348 }
349
350 template<typename TEntities>
351 void AddNotification(const EMassObservedOperation OperationType
352 , TEntities&& Entities
355 {
361 : FBufferedNotification::FCompositionDescription(TInPlaceType<FMassArchetypeCompositionDescriptor>(), FMassArchetypeCompositionDescriptor(MoveTemp(FragmentOverlap), MoveTemp(TagOverlap), {}, {}, {}));
362
363 if (BufferedNotifications.Num()
364 && BufferedNotifications.Last().Type == OperationType
365 && FBufferedNotification::AreCompositionsEqual(BufferedNotifications.Last().CompositionChange, CompositionChange))
366 {
367 BufferedNotifications.Last().AppendEntities(Forward<TEntities>(Entities));
368 }
369 else
370 {
371 BufferedNotifications.Emplace(OperationType, MoveTemp(CompositionChange), Forward<TEntities>(Entities));
372 }
373 }
374
377 UE_API void ForceUpdateCurrentThreadID();
378
383 uint32 OwnerThreadId;
384
385 int32 CreationNotificationIndex = INDEX_NONE;
386
387 TArray<FBufferedNotification> BufferedNotifications;
388
390
392 TWeakPtr<FMassEntityManager> WeakEntityManager;
393
394 #if WITH_MASSENTITY_DEBUG
396 #endif // WITH_MASSENTITY_DEBUG
397 };
398
404 {
406
409
411
413
414 UE_DEPRECATED(5.6, "Use the other GetEntityCollections flavor insteand")
416 UE_DEPRECATED(5.6, "Functionality no longer available")
417 UE_API int32 GetSpawnedNum() const;
418 UE_DEPRECATED(5.6, "Do not use, internal use only")
419 UE_API bool IsDirty() const;
420 UE_DEPRECATED(5.6, "Manually adding entities directly to the creation context is not longer supported and is not taking place automatically")
422 UE_DEPRECATED(5.6, "Manually adding entities directly to the creation context is not longer supported and is not taking place automatically")
424 UE_DEPRECATED(5.5, "This constructor is now deprecated and defunct. Use one of the others instead.")
425 UE_API explicit FCreationContext(const int32);
426 UE_DEPRECATED(5.5, "This function is now deprecated since FEntityCreationContext can contain more than a single collection now. Use GetEntityCollections instead.")
428
432 UE_DEPRECATED_FORGAME(5.6, "Do not use, internal use only")
434 {
435 Lock->MarkCreationNotificationDirty(CreationHandle);
436 }
437
438 private:
440
443 {
444 }
445 FCreationContext(const TSharedRef<FObserverLock>& InLock)
446 : Lock(InLock)
447 {
448 }
449
450 TSharedRef<FObserverLock> GetObserverLock() const
451 {
452 return Lock;
453 }
454
455 bool IsValid() const
456 {
457 return CreationHandle.IsSet();
458 }
459
462 FCreationNotificationHandle CreationHandle;
463 };
464 } // namespace UE::Mass::ObserverManager
465} // namespace UE::Mass
466
467#undef UE_CHECK_OWNER_THREADID
468
469#undef UE_API
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define UE_DEPRECATED_FORGAME
Definition CoreMiscDefines.h:377
@ INDEX_NONE
Definition CoreMiscDefines.h:150
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#define TEXT(x)
Definition Platform.h:1272
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
@ Composition
Definition HairStrandsComposition.cpp:43
EMassObservedOperation
Definition MassEntityTypes.h:579
#define UE_CHECK_OWNER_THREADID()
Definition MassObserverNotificationTypes.h:12
#define UE_API
Definition SColorGradingComponentViewer.h:12
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Array.h:670
Definition SharedPointer.h:153
void Emplace(ArgTypes &&... Args)
Definition TVariant.h:211
U & Get() UE_LIFETIMEBOUND
Definition TVariant.h:146
U * TryGet() UE_LIFETIMEBOUND
Definition TVariant.h:174
Definition SharedPointer.h:1295
EObservedOperationNotification
Definition MassObserverNotificationTypes.h:23
Definition MassArchetypeData.h:21
Definition MassEntityTypes.h:74
Definition MassArchetypeTypes.h:93
EDuplicatesHandling
Definition MassArchetypeTypes.h:147
@ NoDuplicates
Definition MassArchetypeTypes.h:148
Definition MassEntityHandle.h:13
Definition MassEntityManager.h:96
Definition MassObserverManager.h:67
Definition Decay.h:44
Definition TVariant.h:13
Definition MassEntityCollection.h:26
MASSENTITY_API void AddHandle(FMassEntityHandle Handle)
Definition MassEntityCollection.cpp:64
Definition MassObserverNotificationTypes.h:41
Definition MassObserverNotificationTypes.h:39
void DirtyAffectedEntities()
Definition MassObserverNotificationTypes.h:139
FCompositionDescription CompositionChange
Definition MassObserverNotificationTypes.h:45
FBufferedNotification(const EMassObservedOperation InType, TComposition &&Composition, FEntitiesContainer &&Entities)
Definition MassObserverNotificationTypes.h:50
static bool AreCompositionsEqual(const FCompositionDescription &A, const FCompositionDescription &B)
Definition MassObserverNotificationTypes.h:147
void AppendEntities(const TConstArrayView< FMassEntityHandle > InEntityHandles)
Definition MassObserverNotificationTypes.h:100
void AppendEntities(const TConstArrayView< FMassEntityHandle > InEntityHandles, FMassArchetypeEntityCollection &&EntityCollection)
Definition MassObserverNotificationTypes.h:112
EMassObservedOperation Type
Definition MassObserverNotificationTypes.h:43
void AppendEntities(const FMassEntityHandle EntityHandle)
Definition MassObserverNotificationTypes.h:95
FBufferedNotification(const EMassObservedOperation InType, TComposition &&Composition, TEntities &&Entities)
Definition MassObserverNotificationTypes.h:58
TVariant< FEmptyComposition, FMassArchetypeCompositionDescriptor, FMassFragmentBitSet, FMassTagBitSet > FCompositionDescription
Definition MassObserverNotificationTypes.h:44
bool IsCreationNotification() const
Definition MassObserverNotificationTypes.h:80
void AppendEntities(T &&InEntityCollection)
Definition MassObserverNotificationTypes.h:127
void AddHandle(const FMassEntityHandle EntityHandle)
Definition MassObserverNotificationTypes.h:85
FBufferedNotification(const EMassObservedOperation InType, FCompositionDescription &&Composition, const FMassArchetypeEntityCollection &Entities)
Definition MassObserverNotificationTypes.h:73
FBufferedNotification(const EMassObservedOperation InType, FCompositionDescription &&Composition, TEntities &&Entities)
Definition MassObserverNotificationTypes.h:66
FEntitiesContainer AffectedEntities
Definition MassObserverNotificationTypes.h:47
Definition MassObserverNotificationTypes.h:404
UE_API void AppendEntities(const TConstArrayView< FMassEntityHandle >)
Definition MassObserverManager.cpp:949
UE_API const FMassArchetypeEntityCollection & GetEntityCollection() const
Definition MassObserverManager.cpp:961
static UE_API TSharedRef< FCreationContext > DebugCreateDummyCreationContext()
Definition MassObserverNotificationTypes.cpp:70
UE_API bool DebugAreEntityCollectionsUpToDate() const
Definition MassObserverNotificationTypes.cpp:101
UE_API bool IsDirty() const
Definition MassObserverManager.cpp:944
UE_API ~FCreationContext()
Definition MassObserverNotificationTypes.cpp:59
UE_API TConstArrayView< FMassArchetypeEntityCollection > GetEntityCollections() const
Definition MassObserverManager.cpp:934
UE_API int32 GetSpawnedNum() const
Definition MassObserverManager.cpp:939
void MarkDirty()
Definition MassObserverNotificationTypes.h:433
Definition MassObserverNotificationTypes.h:183
bool IsSet() const
Definition MassObserverNotificationTypes.h:184
Definition MassObserverNotificationTypes.h:221
TWeakPtr< FMassEntityManager > GetWeakEntityManager() const
Definition MassObserverNotificationTypes.h:225
UE_API ~FObserverLock()
Definition MassObserverNotificationTypes.cpp:32
const FBufferedNotification & GetCreationNotification(FCreationNotificationHandle CreationHandle) const
Definition MassObserverNotificationTypes.h:237
void MarkCreationNotificationDirty(FCreationNotificationHandle CreationHandle)
Definition MassObserverNotificationTypes.h:230