UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MassEntityCollection.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "MassEntityHandle.h"
8
9namespace UE::Mass
10{
26 {
27 FEntityCollection() = default;
28
37
38 //-----------------------------------------------------------------------------
39 // mutating API
40 //-----------------------------------------------------------------------------
45
54
59
64
70 template<typename T> requires std::is_same_v<typename TDecay<T>::Type, FMassArchetypeEntityCollection>
72 {
73 if (UNLIKELY(InEntityCollection.IsEmpty()))
74 {
75 return;
76 }
77
78 const bool bWasEmpty = EntityHandles.IsEmpty();
79 if (InEntityCollection.ExportEntityHandles(EntityHandles))
80 {
81 ConditionallyStoreCollection(bWasEmpty, Forward<T>(InEntityCollection));
82 }
83 }
84
96 MASSENTITY_API bool UpdateAndRemoveDuplicates(const FMassEntityManager& EntityManager, bool bForceOperation = false);
97
98 //-----------------------------------------------------------------------------
99 // state-querying API
100 //-----------------------------------------------------------------------------
102 {
103 CachedCollections.Reset();
104 }
105
106 bool IsEmpty() const
107 {
108 ensureMsgf(!(EntityHandles.Num() == 0 && CachedCollections.Num() != 0), TEXT("Stored entity array is empty while there are stored collections. This is unexpected."));
109 return EntityHandles.IsEmpty();
110 }
111
117 MASSENTITY_API bool IsUpToDate() const;
118
119 //-----------------------------------------------------------------------------
120 // data reading API
121 //-----------------------------------------------------------------------------
123 {
124 return EntityHandles;
125 }
126
135
141 {
142 ConditionallyUpdate(EntityManager);
143 return CachedCollections;
144 }
145
150 {
151 ConditionallyUpdate(EntityManager);
152 return MoveTemp(CachedCollections);
153 }
154
155 private:
156 template<typename T> requires std::is_same_v<typename TDecay<T>::Type, FMassArchetypeEntityCollection>
157 void ConditionallyStoreCollection(const bool bWasEmpty, T&& InEntityCollection)
158 {
159 // if there was no previous data, or the data was "complete", meaning we had collections for stored handles
160 // Note: this condition only works as expected if we make sure that adding handles without associated collections
161 // resets the CachedCollections array
162 // @todo add unit tests to ensure this behavior
163 if (bWasEmpty || !CachedCollections.IsEmpty())
164 {
165 if (!CachedCollections.IsEmpty() && CachedCollections.Last().IsSameArchetype(InEntityCollection))
166 {
167 // merge with the last collection since it's the same archetype.
168 CachedCollections.Last().Append(Forward<T>(InEntityCollection));
169 }
170 else
171 {
172 CachedCollections.Emplace(Forward<T>(InEntityCollection));
173 }
174 }
175 else
176 {
177 CachedCollections.Reset();
178 }
179 }
180
181 MASSENTITY_API void ConditionallyUpdate(const FMassEntityManager& EntityManager) const;
182
187 TArray<FMassEntityHandle> EntityHandles;
188
195 mutable TArray<FMassArchetypeEntityCollection> CachedCollections;
196
201 };
202}
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
#define TEXT(x)
Definition Platform.h:1272
#define UNLIKELY(x)
Definition Platform.h:857
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType & Last(SizeType IndexFromTheEnd=0) UE_LIFETIMEBOUND
Definition Array.h:1263
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_REWRITE bool IsEmpty() const
Definition Array.h:1133
UE_FORCEINLINE_HINT SizeType Emplace(ArgsType &&... Args)
Definition Array.h:2561
Definition MassArchetypeData.h:21
Definition MassArchetypeTypes.h:93
bool IsSameArchetype(const FMassArchetypeEntityCollection &Other) const
Definition MassArchetypeTypes.h:414
void Append(T &&Other)
Definition MassArchetypeTypes.h:421
EDuplicatesHandling
Definition MassArchetypeTypes.h:147
@ NoDuplicates
Definition MassArchetypeTypes.h:148
Definition MassEntityHandle.h:13
Definition MassEntityManager.h:96
Definition MassEntityCollection.h:26
TArray< FMassArchetypeEntityCollection > ConsumeArchetypeCollections(const FMassEntityManager &EntityManager) &&
Definition MassEntityCollection.h:149
TConstArrayView< FMassArchetypeEntityCollection > GetUpToDatePerArchetypeCollections(const FMassEntityManager &EntityManager) const
Definition MassEntityCollection.h:140
MASSENTITY_API void AppendHandles(TConstArrayView< FMassEntityHandle > Handles)
Definition MassEntityCollection.cpp:43
void MarkDirty()
Definition MassEntityCollection.h:101
bool IsEmpty() const
Definition MassEntityCollection.h:106
MASSENTITY_API bool UpdateAndRemoveDuplicates(const FMassEntityManager &EntityManager, bool bForceOperation=false)
Definition MassEntityCollection.cpp:71
TConstArrayView< FMassArchetypeEntityCollection > GetCachedPerArchetypeCollections() const
Definition MassEntityCollection.h:131
TConstArrayView< FMassEntityHandle > GetEntityHandlesView() const
Definition MassEntityCollection.h:122
void AppendCollection(T &&InEntityCollection)
Definition MassEntityCollection.h:71
MASSENTITY_API void AddHandle(FMassEntityHandle Handle)
Definition MassEntityCollection.cpp:64
MASSENTITY_API bool IsUpToDate() const
Definition MassEntityCollection.cpp:95