UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MassCommands.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
6#include "AutoRTFM.h"
8#include "MassEntityTypes.h"
9#include "MassEntityUtils.h"
10#include "MassEntityManager.h"
12#include "MassCommands.generated.h"
13
19UENUM()
21{
22 None, // default value. Commands marked this way will be always executed last. Programmers are encouraged to instead use one of the meaningful values below.
23 Create, // signifies commands performing entity creation
24 Add, // signifies commands adding fragments or tags to entities
25 Remove, // signifies commands removing fragments or tags from entities
26 ChangeComposition, // signifies commands both adding and removing fragments and/or tags from entities
27 Set, // signifies commands setting values to pre-existing fragments. The fragments might be added if missing,
28 // depending on specific command, so this group will always be executed after the Add group
29 Destroy, // signifies commands removing entities
30 MAX
31};
32
33enum class EMassCommandCheckTime : bool
34{
35 RuntimeCheck = true,
36 CompileTimeCheck = false
37};
38
39#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
40# define DEBUG_NAME(Name) , FName(TEXT(Name))
41# define DEBUG_NAME_PARAM(Name) , const FName InDebugName = TEXT(Name)
42# define FORWARD_DEBUG_NAME_PARAM , InDebugName
43#else
44# define DEBUG_NAME(Name)
45# define DEBUG_NAME_PARAM(Name)
46# define FORWARD_DEBUG_NAME_PARAM
47#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
48
49namespace UE::Mass::Utils
50{
51 template<typename BitSetType, EMassCommandCheckTime CheckTime, typename... TTypes>
53 {
55 {
56 return BitSetType({ TTypes::StaticStruct()... });
57 }
58 else
59 {
60 BitSetType Result;
61 UE::Mass::TMultiTypeList<TTypes...>::PopulateBitSet(Result);
62 return Result;
63 }
64 }
65
66 template<EMassCommandCheckTime CheckTime, typename... TTypes>
71
72 template<EMassCommandCheckTime CheckTime, typename... TTypes>
77} // namespace UE::Mass::Utils
78
79namespace UE::Mass::Command
80{
81 template<typename T>
82 struct TCommandTraits final
83 {
84 enum
85 {
87 };
88 };
89};
90
92{
97#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
100 , DebugName(DebugName)
101 {}
102#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
104 {
105 Reset();
106 }
107
108 UE_DEPRECATED(5.7, "Mass Commands: CONST Execute function is deprecated in 5.7 and will be removed by 5.9. Use Run instead.")
109 virtual void Execute(FMassEntityManager& EntityManager) const
110 {
111 ensureMsgf(false, TEXT("FMassBatchedCommand::Execute is DEPRECATED, override Run function instead."));
112 }
113
114 virtual void Run(FMassEntityManager& EntityManager)
115 {
117 Execute(EntityManager);
119 }
120
121 virtual void Reset()
122 {
123 bHasWork = false;
124 }
125
126 bool HasWork() const { return bHasWork; }
128
129 template<typename T>
132 {
133 static const uint32 ThisTypesStaticIndex = CommandsCounter++;
135 }
136
137 virtual SIZE_T GetAllocatedSize() const = 0;
138
139#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
140 virtual int32 GetNumOperationsStat() const = 0;
141 FName GetFName() const { return DebugName; }
142#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
143
144protected:
145 // @todo note for reviewers - I could use an opinion if having a virtual function per-command would be a more
146 // preferable way of asking commands if there's anything to do.
147 bool bHasWork = false;
149
150#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
151 FName DebugName;
152#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
153
154private:
155 static MASSENTITY_API std::atomic<uint32> CommandsCounter;
156};
157
159{
161
166
168 {
170 TargetEntities.Add(Entity);
171 bHasWork = true;
172 }
173
175 {
177 TargetEntities.Append(Entities.GetData(), Entities.Num());
178 bHasWork = true;
179 }
180
187
188protected:
189 virtual SIZE_T GetAllocatedSize() const
190 {
192 }
193
194 virtual void Reset() override
195 {
197 Super::Reset();
198 }
199
200#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
201 virtual int32 GetNumOperationsStat() const override { return TargetEntities.Num(); }
202#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
203
206};
207
208//-----------------------------------------------------------------------------
209// Entity destruction
210//-----------------------------------------------------------------------------
212{
214
219
220#if WITH_MASSENTITY_DEBUG
221 template<typename T>
222 static bool CheckBreakpoints(T Entity)
223 {
224 return UE::Mass::Debug::FBreakpoint::CheckDestroyEntityBreakpoints(Entity);
225 }
226#endif // WITH_MASSENTITY_DEBUG
227
228protected:
237};
238
239//-----------------------------------------------------------------------------
240// Simple fragment composition change
241//-----------------------------------------------------------------------------
242template<EMassCommandCheckTime CheckTime, typename... TTypes>
244{
248 , FragmentsAffected(UE::Mass::Utils::ConstructFragmentBitSet<CheckTime, TTypes...>())
249 {}
250
251#if WITH_MASSENTITY_DEBUG
252 template<typename T>
253 static bool CheckBreakpoints(T Entity, TTypes... InFragments)
254 {
255 return UE::Mass::Debug::FBreakpoint::CheckFragmentAddBreakpoints(Entity, Forward<TTypes>(InFragments)...);
256 }
257#endif // WITH_MASSENTITY_DEBUG
258
259protected:
268};
269
270template<typename... TTypes>
272
273template<EMassCommandCheckTime CheckTime, typename... TTypes>
275{
278 : Super(EMassCommandOperationType::Remove DEBUG_NAME("RemoveFragments"))
279 , FragmentsAffected(UE::Mass::Utils::ConstructFragmentBitSet<CheckTime, TTypes...>())
280 {}
281
282#if WITH_MASSENTITY_DEBUG
283 template<typename T>
284 static bool CheckBreakpoints(T Entity, TTypes... InFragments)
285 {
286 return UE::Mass::Debug::FBreakpoint::CheckFragmentRemoveBreakpoints(Entity, Forward<TTypes>(InFragments)...);
287 }
288#endif // WITH_MASSENTITY_DEBUG
289
290protected:
299};
300
301template<typename... TTypes>
303
304//-----------------------------------------------------------------------------
305// Simple tag composition change
306//-----------------------------------------------------------------------------
338
339template<EMassCommandCheckTime CheckTime, typename... TTypes>
341{
344 : Super(
346 UE::Mass::Utils::ConstructTagBitSet<CheckTime, TTypes...>(),
347 {}
348 DEBUG_NAME("AddTags"))
349 {}
350};
351
352template<typename T>
354
355template<typename... TTypes>
357
358template<EMassCommandCheckTime CheckTime, typename... TTypes>
360{
369
370#if WITH_MASSENTITY_DEBUG
371 template<typename T>
372 static bool CheckBreakpoints(T Entity, TTypes... InFragments)
373 {
374 return UE::Mass::Debug::FBreakpoint::CheckFragmentAddBreakpoints(Entity, Forward<TTypes>(InFragments)...);
375 }
376#endif // WITH_MASSENTITY_DEBUG
377};
378
379template<typename T>
381
382template<typename... TTypes>
384
385template<EMassCommandCheckTime CheckTime, typename TOld, typename TNew>
387{
390 : Super(
392 UE::Mass::Utils::ConstructTagBitSet<CheckTime, TNew>(),
393 UE::Mass::Utils::ConstructTagBitSet<CheckTime, TOld>()
394 DEBUG_NAME("SwapTags"))
395 {}
396};
397
398template<typename TOld, typename TNew>
400
401//-----------------------------------------------------------------------------
402// Struct Instances adding and setting
403//-----------------------------------------------------------------------------
404template<typename... TOthers>
406{
408
413
415 {
416 Super::Add(Entity);
417 Fragments.Add(InFragments...);
418 }
419
420#if WITH_MASSENTITY_DEBUG
421 static bool CheckBreakpoints(const FMassEntityHandle Entity, TOthers... InFragments)
422 {
423 return UE::Mass::Debug::FBreakpoint::CheckFragmentAddBreakpoints(Entity, Forward<TOthers>(InFragments)...);
424 }
425#endif // WITH_MASSENTITY_DEBUG
426
427protected:
428 virtual void Reset() override
429 {
430 Fragments.Reset();
431 Super::Reset();
432 }
433
434 virtual SIZE_T GetAllocatedSize() const override
435 {
436 return Super::GetAllocatedSize() + Fragments.GetAllocatedSize() + FragmentsAffected.GetAllocatedSize();
437 }
438
453
454 mutable UE::Mass::TMultiArray<TOthers...> Fragments;
456};
457
480
481template<>
483{
484 enum
485 {
487 };
488};;
489
490template<typename... TOthers>
492{
494
499
500#if WITH_MASSENTITY_DEBUG
501 static bool CheckBreakpoints(TOthers... InFragments)
502 {
503 return UE::Mass::Debug::FBreakpoint::CheckCreateEntityBreakpoints(Forward<TOthers>(InFragments)...);
504 }
505#endif // WITH_MASSENTITY_DEBUG
506
507protected:
508
527};
528
534template<typename TSharedFragmentValues, typename... TOthers>
536{
538
540 : Super(EMassCommandOperationType::Create DEBUG_NAME("FMassCommandBuildEntityWithSharedFragments"))
541 , FragmentsAffected(UE::Mass::Utils::ConstructFragmentBitSet<EMassCommandCheckTime::CompileTimeCheck, TOthers...>())
542 {}
543
545 {
546 InSharedFragments.Sort();
547
548 // Compute hash before adding to the map since evaluation order is not guaranteed
549 // and MoveTemp will invalidate InSharedFragments
550 const uint32 Hash = GetTypeHash(InSharedFragments);
551
554 Instance.TargetEntities.Add(Entity);
555
556 bHasWork = true;
557 }
558
559#if WITH_MASSENTITY_DEBUG
560 static bool CheckBreakpoints(TOthers... InFragments)
561 {
562 // debugger doesn't currently support shared fragment filtering, so just send the others
563 return UE::Mass::Debug::FBreakpoint::CheckCreateEntityBreakpoints(Forward<TOthers>(InFragments)...);
564 }
565#endif // WITH_MASSENTITY_DEBUG
566
567protected:
568 virtual SIZE_T GetAllocatedSize() const override
569 {
570 SIZE_T TotalSize = 0;
571 for (const auto& KeyValue : Data)
572 {
573 TotalSize += KeyValue.Value.GetAllocatedSize();
574 }
575 TotalSize += Data.GetAllocatedSize();
576 TotalSize += FragmentsAffected.GetAllocatedSize();
577 return TotalSize;
578 }
579
580 virtual void Run(FMassEntityManager& EntityManager) override
581 {
583
584 constexpr int FragmentTypesCount = UE::Mass::TMultiTypeList<TOthers...>::Ordinal + 1;
587
588 for (auto It : Data)
589 {
590 It.Value.Fragments.GetAsGenericMultiArray(GenericMultiArray);
591
595 checkf(EntityCollections.Num() <= 1, TEXT("We expect TargetEntities to only contain archetype-less entities, ones that need to be \'build\'"));
596
597 if (EntityCollections.Num())
598 {
599 EntityManager.BatchBuildEntities(EntityCollections[0], FragmentsAffected, It.Value.SharedFragmentValues);
600 }
601
602 GenericMultiArray.Reset();
603 }
604 }
605
606 virtual void Reset() override
607 {
608 Data.Reset();
609 Super::Reset();
610 }
611
612#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
613 virtual int32 GetNumOperationsStat() const override
614 {
615 int32 TotalCount = 0;
616 for (const auto& KeyValue : Data)
617 {
618 TotalCount += KeyValue.Value.TargetEntities.Num();
619 }
620 return TotalCount;
621 }
622#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
623
625
642
644};
645
646//-----------------------------------------------------------------------------
647// Commands that really can't know the types at compile time
648//-----------------------------------------------------------------------------
649template<EMassCommandOperationType OpType>
651{
654
656 : Super(OpType DEBUG_NAME("BatchedDeferredCommand"))
657 {}
658
664
666 {
668 bHasWork = true;
669 }
670
671protected:
672 virtual SIZE_T GetAllocatedSize() const
673 {
674 return DeferredFunctions.GetAllocatedSize();
675 }
676
677 virtual void Run(FMassEntityManager& EntityManager) override
678 {
680
682 {
683 ExecFunction(EntityManager);
684 }
685 }
686
687 virtual void Reset() override
688 {
689 DeferredFunctions.Reset();
690 Super::Reset();
691 }
692
693#if CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
694 virtual int32 GetNumOperationsStat() const override
695 {
696 return DeferredFunctions.Num();
697 }
698#endif // CSV_PROFILER_STATS || WITH_MASSENTITY_DEBUG
699
701};
702
709
710#undef DEBUG_NAME
711#undef DEBUG_NAME_PARAM
712#undef FORWARD_DEBUG_NAME_PARAM
OODEFFUNC typedef void(OODLE_CALLBACK t_fp_OodleCore_Plugin_Free)(void *ptr)
#define check(expr)
Definition AssertionMacros.h:314
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
#define UE_AUTORTFM_ALWAYS_OPEN
Definition AutoRTFMDefines.h:114
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#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 TRACE_CPUPROFILER_EVENT_SCOPE(Name)
Definition CpuProfilerTrace.h:528
#define PRAGMA_ENABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:12
#define PRAGMA_DISABLE_DEPRECATION_WARNINGS
Definition GenericPlatformCompilerPreSetup.h:8
#define UE_MT_SCOPED_WRITE_ACCESS(AccessDetector)
Definition MTAccessDetector.h:751
EMassCommandCheckTime
Definition MassCommands.h:34
#define FORWARD_DEBUG_NAME_PARAM
Definition MassCommands.h:46
#define DEBUG_NAME(Name)
Definition MassCommands.h:44
EMassCommandOperationType
Definition MassCommands.h:21
#define DEBUG_NAME_PARAM(Name)
Definition MassCommands.h:45
T TNotNull
Definition NotNull.h:307
#define UENUM(...)
Definition ObjectMacros.h:749
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 NameTypes.h:617
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
void Append(const TArray< OtherElementType, OtherAllocatorType > &Source)
Definition Array.h:2412
UE_NODEBUG UE_FORCEINLINE_HINT SIZE_T GetAllocatedSize(void) const
Definition Array.h:1059
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition AndroidPlatformMisc.h:14
Definition UnrealString.h.inl:34
Definition MassCommandBuffer.cpp:17
Definition MassEntityUtils.cpp:15
FMassFragmentBitSet ConstructFragmentBitSet()
Definition MassCommands.h:67
BitSetType ConstructBitSet()
Definition MassCommands.h:52
void CreateEntityCollections(const FMassEntityManager &EntityManager, const TConstArrayView< FMassEntityHandle > Entities, const FMassArchetypeEntityCollection::EDuplicatesHandling DuplicatesHandling, TArray< FMassArchetypeEntityCollection > &OutEntityCollections)
Definition MassEntityUtils.cpp:72
FMassTagBitSet ConstructTagBitSet()
Definition MassCommands.h:73
Definition AdvancedWidgetsModule.cpp:13
static UE_API void CreateEntityRangesWithPayload(const FMassEntityManager &EntityManager, const TConstArrayView< FMassEntityHandle > Entities, const FMassArchetypeEntityCollection::EDuplicatesHandling DuplicatesHandling, FMassGenericPayloadView Payload, TArray< FMassArchetypeEntityCollectionWithPayload > &OutEntityCollections)
Definition MassArchetypeTypes.cpp:306
@ FoldDuplicates
Definition MassArchetypeTypes.h:150
Definition MassEntityTypes.h:297
SIZE_T GetAllocatedSize() const
Definition MassEntityTypes.h:983
Definition MassCommands.h:92
virtual SIZE_T GetAllocatedSize() const =0
static UE_AUTORTFM_ALWAYS_OPEN uint32 GetCommandIndex()
Definition MassCommands.h:131
EMassCommandOperationType GetOperationType() const
Definition MassCommands.h:127
virtual void Run(FMassEntityManager &EntityManager)
Definition MassCommands.h:114
bool bHasWork
Definition MassCommands.h:147
virtual ~FMassBatchedCommand()
Definition MassCommands.h:103
virtual void Reset()
Definition MassCommands.h:121
FMassBatchedCommand(EMassCommandOperationType OperationType)
Definition MassCommands.h:94
FMassBatchedCommand()=default
EMassCommandOperationType OperationType
Definition MassCommands.h:148
bool HasWork() const
Definition MassCommands.h:126
Definition MassCommands.h:159
FMassBatchedEntityCommand()=default
void Add(TArray< FMassEntityHandle > &&Entities)
Definition MassCommands.h:181
TArray< FMassEntityHandle > TargetEntities
Definition MassCommands.h:205
void Add(FMassEntityHandle Entity)
Definition MassCommands.h:167
UE_MT_DECLARE_TS_RW_ACCESS_DETECTOR(EntitiesAccessDetector)
virtual void Reset() override
Definition MassCommands.h:194
void Add(TConstArrayView< FMassEntityHandle > Entities)
Definition MassCommands.h:174
FMassBatchedEntityCommand(EMassCommandOperationType OperationType DEBUG_NAME_PARAM("BatchedEntityCommand"))
Definition MassCommands.h:163
virtual SIZE_T GetAllocatedSize() const
Definition MassCommands.h:189
Definition MassCommands.h:463
TNotNull< const UScriptStruct * > ElementType
Definition MassCommands.h:478
FMassCommandAddElement(const TNotNull< const UScriptStruct * > InElementType)
Definition MassCommands.h:466
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:472
Definition MassCommands.h:406
UE::Mass::TMultiArray< TOthers... > Fragments
Definition MassCommands.h:454
virtual SIZE_T GetAllocatedSize() const override
Definition MassCommands.h:434
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:439
void Add(FMassEntityHandle Entity, TOthers... InFragments)
Definition MassCommands.h:414
virtual void Reset() override
Definition MassCommands.h:428
FMassCommandAddFragmentInstances(EMassCommandOperationType OperationType=EMassCommandOperationType::Set DEBUG_NAME_PARAM("AddFragmentInstanceList"))
Definition MassCommands.h:409
const FMassFragmentBitSet FragmentsAffected
Definition MassCommands.h:455
Definition MassCommands.h:244
FMassFragmentBitSet FragmentsAffected
Definition MassCommands.h:267
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:260
FMassCommandAddFragmentsInternal()
Definition MassCommands.h:246
Definition MassCommands.h:341
FMassCommandAddTagsInternal()
Definition MassCommands.h:343
SIZE_T GetAllocatedSize() const
Definition MassCommands.h:633
UE::Mass::TMultiArray< TOthers... > Fragments
Definition MassCommands.h:639
TArray< FMassEntityHandle > TargetEntities
Definition MassCommands.h:638
FMassArchetypeSharedFragmentValues SharedFragmentValues
Definition MassCommands.h:640
FPerSharedFragmentsHashData(FMassArchetypeSharedFragmentValues &&InSharedFragmentValues)
Definition MassCommands.h:628
Definition MassCommands.h:536
virtual SIZE_T GetAllocatedSize() const override
Definition MassCommands.h:568
void Add(FMassEntityHandle Entity, FMassArchetypeSharedFragmentValues &&InSharedFragments, TOthers... InFragments)
Definition MassCommands.h:544
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:580
FMassFragmentBitSet FragmentsAffected
Definition MassCommands.h:624
virtual void Reset() override
Definition MassCommands.h:606
TMap< uint32, FPerSharedFragmentsHashData > Data
Definition MassCommands.h:643
FMassCommandBuildEntityWithSharedFragments()
Definition MassCommands.h:539
Definition MassCommands.h:492
FMassCommandBuildEntity()
Definition MassCommands.h:495
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:509
Definition MassCommands.h:308
FMassCommandChangeTags(EMassCommandOperationType OperationType, FMassTagBitSet TagsToAdd, FMassTagBitSet TagsToRemove DEBUG_NAME_PARAM("ChangeTags"))
Definition MassCommands.h:314
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:321
FMassTagBitSet TagsToAdd
Definition MassCommands.h:335
FMassTagBitSet TagsToRemove
Definition MassCommands.h:336
virtual SIZE_T GetAllocatedSize() const override
Definition MassCommands.h:330
FMassCommandChangeTags()
Definition MassCommands.h:310
Definition MassCommands.h:212
FMassCommandDestroyEntities()
Definition MassCommands.h:215
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:229
Definition MassCommands.h:275
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:291
FMassCommandRemoveFragmentsInternal()
Definition MassCommands.h:277
FMassFragmentBitSet FragmentsAffected
Definition MassCommands.h:298
Definition MassCommands.h:360
FMassCommandRemoveTagsInternal()
Definition MassCommands.h:362
Definition MassCommands.h:387
FMassCommandSwapTagsInternal()
Definition MassCommands.h:389
Definition MassCommands.h:651
void Add(const FExecFunction &ExecFunction)
Definition MassCommands.h:665
virtual void Run(FMassEntityManager &EntityManager) override
Definition MassCommands.h:677
TArray< FExecFunction > DeferredFunctions
Definition MassCommands.h:700
FMassDeferredCommand()
Definition MassCommands.h:655
virtual void Reset() override
Definition MassCommands.h:687
virtual SIZE_T GetAllocatedSize() const
Definition MassCommands.h:672
void Add(FExecFunction &&ExecFunction)
Definition MassCommands.h:659
Definition MassEntityHandle.h:13
Definition MassEntityManager.h:96
Definition MassEntityTypes.h:623
Definition MassCommands.h:83
@ RequiresUniqueHandling
Definition MassCommands.h:86