UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MassCommandBuffer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "MassEntityTypes.h"
6#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_6
7#include "MassEntityManager.h"
8#endif // UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_6
11#include "MassEntityUtils.h"
13#include "MassCommands.h"
14// required for UE::Mass::Deprecation
15#include <type_traits>
17
19
20//@TODO: Consider debug information in case there is an assert when replaying the command buffer
21// (e.g., which system added the command, or even file/line number in development builds for the specific call via a macro)
22
23#define COMMAND_PUSHING_CHECK() \
24checkf(IsFlushing() == false, TEXT("Trying to push commands is not supported while the given buffer is being flushed")); \
25checkf(OwnerThreadId == FPlatformTLS::GetCurrentThreadId(), TEXT("Commands can be pushed only in the same thread where the command buffer was created."))
26
27// The following is a temporary construct to help users update their code after FMassBatchedCommand::Execute deprecation
28// @todo remove by 5.9
30{
32 template <typename TCommand>
33 constexpr bool IsExecuteOverridden()
34 {
35 using TResolved = decltype(&TCommand::Execute);
36 using TBase = decltype(&FMassBatchedCommand::Execute);
37 return !std::is_same_v<TResolved, TBase>;
38 }
39
40 template <typename TCommand>
41 requires std::is_base_of_v<FMassBatchedCommand, TCommand>
42 struct TOverridesExecute : std::bool_constant<IsExecuteOverridden<TCommand>()>
43 {
44 };
46}
47
48#define ASSERT_EXECUTE_DEPRECATION(CommandType) \
49 static_assert(!UE::Mass::Deprecation::TOverridesExecute<CommandType>::value, "Mass Commands: CONST Execute function is deprecated in 5.7 and will be removed by 5.9. Use Run instead.");
50
52{
53
54 template<typename TCommand, typename... Args>
56 requires(Args&&... args) {
57 { TCommand::CheckBreakpoints(Forward<Args>(args)...) }
59 };
60
61 template<typename TCommand, typename... Args>
63 requires(const FMassEntityHandle Entity, Args&&... args) {
64 { TCommand::CheckBreakpoints(Entity, Forward<Args>(args)...) }
66 };
67
68 template<typename TCommand, typename... TArgs>
69 void CallCheckBreakpoints(TArgs&&... InArgs)
70 {
71#if WITH_MASSENTITY_DEBUG
72 if constexpr (HasCheckBreakpoints<TCommand, TArgs...>)
73 {
74 if (TCommand::CheckBreakpoints(Forward<TArgs>(InArgs)...))
75 {
76 UE::Mass::Debug::FBreakpoint::DebugBreak();
77 }
78 }
79#endif //WITH_MASSENTITY_DEBUG
80 }
81
82 template<typename TCommand, typename... TArgs >
84 {
85#if WITH_MASSENTITY_DEBUG
86 if constexpr (HasCheckBreakpointsWithEntity<TCommand, TArgs...>)
87 {
88 if (TCommand::CheckBreakpointsByInstance(Forward<TArgs>(InArgs)...))
89 {
90 UE::Mass::Debug::FBreakpoint::DebugBreak();
91 }
92 }
93#endif //WITH_MASSENTITY_DEBUG
94 }
95} // namespace UE::Mass::Debug
96
97
99{
102
104 template< template<typename... TArgs> typename TCommand, typename... TArgs >
105 void PushCommand(const FMassEntityHandle Entity, TArgs&&... InArgs)
106 {
109
110 LLM_SCOPE_BYNAME(TEXT("Mass/PushCommand"));
111 TCommand<TArgs...>& Instance = CreateOrAddCommand<TCommand<TArgs...>>();
112 Instance.Add(Entity, Forward<TArgs>(InArgs)...);
113 ++ActiveCommandsCounter;
114 }
115
116 template<typename TCommand, typename... TArgs>
117 void PushCommand(TArgs&&... InArgs)
118 {
120 UE::Mass::Debug::CallCheckBreakpointsByInstance<TCommand>(Forward<TArgs>(InArgs)...);
121
122 LLM_SCOPE_BYNAME(TEXT("Mass/PushCommand"));
124 Instance.Add(Forward<TArgs>(InArgs)...);
125 ++ActiveCommandsCounter;
126 }
127
129 template< typename TCommand>
131 {
133 UE::Mass::Debug::CallCheckBreakpoints<TCommand>(Entity);
134
135 LLM_SCOPE_BYNAME(TEXT("Mass/PushCommand"));
136 CreateOrAddCommand<TCommand>().Add(Entity);
137 ++ActiveCommandsCounter;
138 }
139
140 template< typename TCommand>
142 {
144 UE::Mass::Debug::CallCheckBreakpoints<TCommand>(Entities);
145
146 LLM_SCOPE_BYNAME(TEXT("Mass/PushCommand"));
147 CreateOrAddCommand<TCommand>().Add(Entities);
148 ++ActiveCommandsCounter;
149 }
150
160 {
162
163 LLM_SCOPE_BYNAME(TEXT("Mass/PushCommand"));
164 UE::TScopeLock Lock(AppendingCommandsCS);
166 AppendedCommandInstances.Add(MoveTemp(CommandInstance));
167
168 ++ActiveCommandsCounter;
169 }
170
171 template<typename T>
177
178 template<typename T>
184
185 template<typename T>
191
192 template<typename T>
198
200 template<typename T>
202 {
203 static_assert(UE::Mass::CTag<T>, "Given struct type is not a valid tag type.");
205 }
206
207 template<typename T>
209 {
210 static_assert(UE::Mass::CTag<T>, "Given struct type is not a valid tag type.");
212 }
213
214 template<typename T>
216 {
217 checkf(UE::Mass::IsA<FMassTag>(T::StaticStruct()), TEXT("Given struct type is not a valid tag type."));
219 }
220
222 template<typename T>
224 {
225 static_assert(UE::Mass::CTag<T>, "Given struct type is not a valid tag type.");
227 }
228
229 template<typename T>
231 {
232 static_assert(UE::Mass::CTag<T>, "Given struct type is not a valid tag type.");
234 }
235
236 template<typename T>
238 {
239 checkf(UE::Mass::IsA<FMassTag>(T::StaticStruct()), TEXT("Given struct type is not a valid tag type."));
241 }
242
244 template<typename TOld, typename TNew>
246 {
247 static_assert(UE::Mass::CTag<TOld>, "Given struct type is not a valid tag type.");
248 static_assert(UE::Mass::CTag<TNew>, "Given struct type is not a valid tag type.");
250 }
251
252 template<typename TOld, typename TNew>
254 {
255 checkf(UE::Mass::IsA<FMassTag>(TOld::StaticStruct()), TEXT("Given struct type is not a valid tag type."));
256 checkf(UE::Mass::IsA<FMassTag>(TNew::StaticStruct()), TEXT("Given struct type is not a valid tag type."));
258 }
259
264
269
274
276
283
284 bool HasPendingCommands() const
285 {
286 return ActiveCommandsCounter > 0;
287 }
288 bool IsFlushing() const { return bIsFlushing; }
289
298 {
299 CleanUp();
300 }
301
302 bool IsInOwnerThread() const
303 {
304 return OwnerThreadId == FPlatformTLS::GetCurrentThreadId();
305 }
306
315
316private:
317 friend FMassEntityManager;
318
319 template<typename T>
320 T& CreateOrAddCommand()
321 {
323
324 static_assert(!UE::Mass::Command::TCommandTraits<T>::RequiresUniqueHandling, "This command type needs to be added via PushUniqueCommand");
325 const int32 Index = FMassBatchedCommand::GetCommandIndex<T>();
326
327 if (CommandInstances.IsValidIndex(Index) == false)
328 {
329 CommandInstances.AddZeroed(Index - CommandInstances.Num() + 1);
330 }
331 else if (CommandInstances[Index])
332 {
333 return (T&)(*CommandInstances[Index].Get());
334 }
335
336 CommandInstances[Index] = MakeUnique<T>();
337 return (T&)(*CommandInstances[Index].Get());
338 }
339
344 bool Flush(FMassEntityManager& EntityManager);
345 MASSENTITY_API void CleanUp();
346
347 FTransactionallySafeCriticalSection AppendingCommandsCS;
348
360 TArray<TUniquePtr<FMassBatchedCommand>> AppendedCommandInstances;
361
362 int32 ActiveCommandsCounter = 0;
363
365 bool bIsFlushing = false;
366
372 uint32 OwnerThreadId;
373};
374
375#undef COMMAND_PUSHING_CHECK
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
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 LLM_SCOPE_BYNAME(...)
Definition LowLevelMemTracker.h:1098
#define UE_MT_SCOPED_WRITE_ACCESS(AccessDetector)
Definition MTAccessDetector.h:751
#define UE_MT_DECLARE_TS_RW_ACCESS_DETECTOR(AccessDetector)
Definition MTTransactionallySafeAccessDetector.h:285
#define COMMAND_PUSHING_CHECK()
Definition MassCommandBuffer.h:23
#define ASSERT_EXECUTE_DEPRECATION(CommandType)
Definition MassCommandBuffer.h:48
#define MASS_INVALID_FRAGMENT_MSG_F
Definition MassEntityElementTypes.h:17
#define MASS_INVALID_FRAGMENT_MSG
Definition MassEntityElementTypes.h:16
::FCriticalSection FTransactionallySafeCriticalSection
Definition TransactionallySafeCriticalSection.h:16
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
UE_NODEBUG UE_FORCEINLINE_HINT bool IsValidIndex(SizeType Index) const
Definition Array.h:1122
SizeType AddZeroed()
Definition Array.h:2755
Definition UniquePtr.h:107
Definition ScopeLock.h:21
Definition ConvertibleTo.h:16
Definition MassEntityConcepts.h:16
Definition MassEntityConcepts.h:23
Definition MassCommandBuffer.h:62
Definition MassCommandBuffer.h:55
Definition MassCommandBuffer.h:52
void CallCheckBreakpointsByInstance(TArgs &&... InArgs)
Definition MassCommandBuffer.h:83
void CallCheckBreakpoints(TArgs &&... InArgs)
Definition MassCommandBuffer.h:69
Definition MassCommandBuffer.h:30
PRAGMA_DISABLE_DEPRECATION_WARNINGS constexpr bool IsExecuteOverridden()
Definition MassCommandBuffer.h:33
bool IsA< FMassTag >(const UStruct *Struct)
Definition MassEntityElementTypes.h:61
bool IsA< FMassFragment >(const UStruct *Struct)
Definition MassEntityElementTypes.h:55
U16 Index
Definition radfft.cpp:71
static uint32 GetCurrentThreadId(void)
Definition AndroidPlatformTLS.h:20
virtual void Execute(FMassEntityManager &EntityManager) const
Definition MassCommands.h:109
Definition MassCommandBuffer.h:99
MASSENTITY_API void MoveAppend(FMassCommandBuffer &InOutOther)
Definition MassCommandBuffer.cpp:229
void RemoveTag(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:223
void PushCommand(const FMassEntityHandle Entity)
Definition MassCommandBuffer.h:130
void SwapTags(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:245
bool HasPendingCommands() const
Definition MassCommandBuffer.h:284
void RemoveFragment(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:186
void AddFragment_RuntimeCheck(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:179
void DestroyEntity(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:260
void RemoveFragment_RuntimeCheck(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:193
MASSENTITY_API FMassCommandBuffer()
Definition MassCommandBuffer.cpp:77
bool IsInOwnerThread() const
Definition MassCommandBuffer.h:302
void PushCommand(TArgs &&... InArgs)
Definition MassCommandBuffer.h:117
void AddTag_RuntimeCheck(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:215
void RemoveTag(TConstArrayView< FMassEntityHandle > Entities)
Definition MassCommandBuffer.h:230
void CancelCommands()
Definition MassCommandBuffer.h:297
void PushCommand(const FMassEntityHandle Entity, TArgs &&... InArgs)
Definition MassCommandBuffer.h:105
void PushCommand(TConstArrayView< FMassEntityHandle > Entities)
Definition MassCommandBuffer.h:141
void AddTag(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:201
MASSENTITY_API SIZE_T GetAllocatedSize() const
Definition MassCommandBuffer.cpp:246
void ForceUpdateCurrentThreadID()
Definition MassCommandBuffer.cpp:89
void RemoveTag_RuntimeCheck(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:237
MASSENTITY_API ~FMassCommandBuffer()
Definition MassCommandBuffer.cpp:82
void DestroyEntities(TArray< FMassEntityHandle > &&InEntitiesToDestroy)
Definition MassCommandBuffer.h:270
void DestroyEntities(TConstArrayView< FMassEntityHandle > InEntitiesToDestroy)
Definition MassCommandBuffer.h:265
void SwapTags_RuntimeCheck(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:253
bool IsFlushing() const
Definition MassCommandBuffer.h:288
void AddFragment(FMassEntityHandle Entity)
Definition MassCommandBuffer.h:172
void PushUniqueCommand(TUniquePtr< FMassBatchedCommand > &&CommandInstance)
Definition MassCommandBuffer.h:159
void AddTag(TConstArrayView< FMassEntityHandle > Entities)
Definition MassCommandBuffer.h:208
Definition MassEntityHandle.h:13
Definition MassEntityManager.h:96
Definition MassCommands.h:83
Definition MassCommandBuffer.h:43