KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
KarmaUtilities.h
Go to the documentation of this file.
1
10#pragma once
11
12#include "SubClassOf.h"
13#include "Actor.h"
14#include "stb_image.h"
15#include "Level.h"
16#include "World.h"
17#include "WorldSettings.h"
18
19namespace Karma
20{
21 class UObject;
22 class AActor;
23
24 class UClass;
25
30 {
31 public:
40 static std::string ReadFileToSpitString(const std::string& filePath);
41
48 static std::string GetFilePath(const std::string& str);
49
79 static unsigned char* GetImagePixelData(char const* fileName, int* width, int* height, int* channels, int req_comp);
80 };
81
89 {
90 public:
93
96
98 int32_t m_Index;
99
102
105
108
111
114
116 //FDelegateHandle ActorSpawnedDelegateHandle;
117
124
133 FActorIteratorState(const UWorld* InWorld, const TSubclassOf<AActor> InClass) :
134 m_CurrentWorld(InWorld),
135 m_Index(-1),
136 m_ReachedEnd(false),
138 m_CurrentActor(nullptr),
139 m_DesiredClass(InClass)
140 {
141 //check(IsInGameThread());
142 KR_CORE_ASSERT(m_CurrentWorld, "");
143
144 #if WITH_EDITOR
145 // In the editor, you are more likely to have many worlds in memory at once.
146 // As an optimization to avoid iterating over many actors that are not in the world we are asking for,
147 // if the filter class is AActor, just use the actors that are in the world you asked for.
148 // This could be useful in runtime code as well if there are many worlds in memory, but for now we will leave
149 // it in editor code.
150 if (InClass == AActor::StaticClass())
151 {
152 // First determine the number of actors in the world to reduce reallocations when we append them to the array below.
153 int32 NumActors = 0;
154 for (ULevel* Level : InWorld->GetLevels())
155 {
156 if (Level)
157 {
158 NumActors += Level->Actors.Num();
159 }
160 }
161
162 // Presize the array
163 ObjectArray.Reserve(NumActors);
164
165
166 // Fill the array
167 for (ULevel* Level : InWorld->GetLevels())
168 {
169 if (Level)
170 {
171 ObjectArray.Append(Level->Actors);
172 }
173 }
174 }
175 else
176 #endif // WITH_EDITOR
177 {
178 constexpr EObjectFlags ExcludeFlags = RF_ClassDefaultObject;
179 GetObjectsOfClass(InClass, m_ObjectArray, true, ExcludeFlags, EInternalObjectFlags::Garbage);
180 }
181
182
183 // const auto ActorSpawnedDelegate = FOnActorSpawned::FDelegate::CreateRaw(this, &FActorIteratorState::OnActorSpawned);
184 // m_ActorSpawnedDelegateHandle = m_CurrentWorld->AddOnActorSpawnedHandler(ActorSpawnedDelegate);
185 }
186
193 {
194 //m_CurrentWorld->RemoveOnActorSpawnedHandler(ActorSpawnedDelegateHandle);
195 }
196
197
205 {
206 KR_CORE_ASSERT(m_CurrentActor, "");
207 KR_CORE_ASSERT(!m_CurrentActor->IsUnreachable(), "");
208
209 return m_CurrentActor;
210 }
211
212 private:
213 /*
214 void OnActorSpawned(AActor* InActor)
215 {
216 if (InActor->IsA(DesiredClass))
217 {
218 SpawnedActorArray.AddUnique(InActor);
219 }
220 }*/
221 };
222
227 {
231 AllActors = 0x00000000,
235 SkipPendingKill = 0x00000001,
239 OnlySelectedActors = 0x00000002,
243 OnlyActiveLevels = 0x00000004,
244 };
245
250 {
251 End
252 };
253
257 template <typename Derived>
259 {
260 private:
261 EActorIteratorFlags m_Flags;
262
263 // smart pointer?
264 std::shared_ptr<FActorIteratorState> m_State;
265
266 protected:
279
290 TActorIteratorBase(const UWorld* InWorld, TSubclassOf<AActor> InClass, const EActorIteratorFlags InFlags)
291 : m_Flags(InFlags)
292 {
293 m_State.reset(new FActorIteratorState(InWorld, InClass));
294 }
295
296 public:
305 {
306 // Use local version to avoid LHSs as compiler is not required to write out member variables to memory.
307 AActor* localCurrentActor = nullptr;
308 int32_t localIndex = m_State->m_Index;
309 KarmaVector<UObject*>& localObjectArray = m_State->m_ObjectArray;// note that localObjectArray is a vector
310 KarmaVector<AActor*>& localSpawnedActorArray = m_State->m_SpawnedActorArray; // Contains any actors spawned during iteration
311 const UWorld* localCurrentWorld = m_State->m_CurrentWorld;
312
313 int32_t lObjectArrayNum = int32_t(localObjectArray.Num());
314 int32_t lSAArrayNum = int32_t(localSpawnedActorArray.Num());
315
316 while (++localIndex < (lObjectArrayNum + lSAArrayNum))
317 {
318 if (localIndex < lObjectArrayNum)
319 {
320 localCurrentActor = static_cast<AActor*>(localObjectArray.GetElements()[localIndex]);
321 }
322 else
323 {
324 localCurrentActor = localSpawnedActorArray.GetElements()[localIndex - lObjectArrayNum];
325 }
326 m_State->m_ConsideredCount++;// Number of actors that have been considered thus far
327
328 ULevel* actorLevel = localCurrentActor ? localCurrentActor->GetLevel() : nullptr;
329
330 if (actorLevel
331 && static_cast<const Derived*>(this)->IsActorSuitable(localCurrentActor)
332 && static_cast<const Derived*>(this)->CanIterateLevel(actorLevel) && actorLevel->GetWorld() == localCurrentWorld)
333 {
334 // ignore non-persistent world settings
335 if (actorLevel == localCurrentWorld->GetPersistentLevel() || !localCurrentActor->IsA(AWorldSettings::StaticClass()))
336 {
337 m_State->m_CurrentActor = localCurrentActor;
338 m_State->m_Index = localIndex;
339 return;
340 }
341 }
342 }
343 m_State->m_CurrentActor = nullptr;
344 m_State->m_ReachedEnd = true;
345 }
346
354 {
355 return m_State->GetActorChecked();
356 }
357
365 {
366 return m_State->GetActorChecked();
367 }
368
376 FORCEINLINE explicit operator bool() const
377 {
378 return !m_State->m_ReachedEnd;
379 }
380
387 {
388 KR_CORE_ASSERT(!m_State->m_ReachedEnd, "");
389 m_State->m_CurrentWorld->RemoveActor(m_State->m_CurrentActor, true);
390 }
391
400 {
401 return m_State->m_ConsideredCount;
402 }
403
413 FORCEINLINE bool CanIterateLevel(const ULevel* Level) const
414 {
415 /*
416 if (EnumHasAnyFlags(Flags, EActorIteratorFlags::OnlyActiveLevels))
417 {
418 const bool bIsLevelVisibleOrAssociating = (Level->bIsVisible && !Level->bIsBeingRemoved) || Level->bIsAssociatingLevel || Level->bIsDisassociatingLevel;
419
420 // Only allow iteration of Level if it's in the currently active level collection of the world, or is a static level.
421 const FLevelCollection* const ActorLevelCollection = Level->GetCachedLevelCollection();
422 const FLevelCollection* const ActiveLevelCollection = Level->OwningWorld ? Level->OwningWorld->GetActiveLevelCollection() : nullptr;
423
424 // If the world's active level collection is null, we can't apply any meaningful filter,
425 // so just allow iteration in this case.
426 const bool bIsCurrentLevelCollectionTicking = !ActiveLevelCollection || (ActorLevelCollection == ActiveLevelCollection);
427
428 const bool bIsLevelCollectionNullOrStatic = !ActorLevelCollection || ActorLevelCollection->GetType() == ELevelCollectionType::StaticLevels;
429 const bool bShouldIterateLevelCollection = bIsCurrentLevelCollectionTicking || bIsLevelCollectionNullOrStatic;
430
431 return bIsLevelVisibleOrAssociating && bShouldIterateLevelCollection;
432 }*/
433
434 return true;
435 }
436
446 FORCEINLINE bool IsActorSuitable(const AActor* Actor) const
447 {
449 {
450 return false;
451 }
452
454 {
455 return false;
456 }
457
458 return true;
459 }
460 };
461}
This file contains the class AActor.
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:170
EActorIteratorFlags
Iteration flags, specifies which types of levels and actors should be iterated.
Definition KarmaUtilities.h:227
@ OnlySelectedActors
Only iterate actors that are selected.
Definition KarmaUtilities.h:239
@ SkipPendingKill
Skip pending kill actors.
Definition KarmaUtilities.h:235
@ OnlyActiveLevels
Only iterate active levels.
Definition KarmaUtilities.h:243
@ AllActors
No flags, iterate all actors.
Definition KarmaUtilities.h:231
EActorIteratorType
Type enum, used to represent the special End iterator.
Definition KarmaUtilities.h:250
This file contains the class ULevel.
FORCEINLINE bool IsValid(const UObject *Test)
Test validity of object.
Definition Object.h:239
This file contains the class TSubclassOf and relevant helpers.
constexpr bool EnumHasAnyFlags(Enum Flags, Enum Contains)
Sees if a specified flag(s) exists in the linear combination of flags.
Definition UObjectGlobals.h:45
EObjectFlags
Flags describing an object instance.
Definition UObjectGlobals.h:183
@ RF_ClassDefaultObject
This object is its class's default object.
Definition UObjectGlobals.h:194
@ Garbage
Garbage from logical point of view and should not be referenced. This flag is mirrored in EObjectFlag...
Definition UObjectGlobals.h:247
signed int int32
32-bit signed integer
Definition UObjectGlobals.h:33
This file contains UWorld class and FActorSpawnParameters.
This file contains the class AWorldSettings.
Actor is the base class for an object that can be placed or spawned in a level.
Definition Actor.h:35
ULevel * GetLevel() const
Definition Actor.cpp:18
Abstract base class for actor iteration. Implements all operators and relies on IsActorSuitable to be...
Definition KarmaUtilities.h:89
FActorIteratorState(const UWorld *InWorld, const TSubclassOf< AActor > InClass)
Default constructor, initializes everything relevant.
Definition KarmaUtilities.h:133
UClass * m_DesiredClass
Definition KarmaUtilities.h:113
FActorIteratorState()
Default constructor.
Definition KarmaUtilities.h:123
KarmaVector< UObject * > m_ObjectArray
Definition KarmaUtilities.h:95
bool m_ReachedEnd
Definition KarmaUtilities.h:101
const UWorld * m_CurrentWorld
Definition KarmaUtilities.h:92
KarmaVector< AActor * > m_SpawnedActorArray
Definition KarmaUtilities.h:110
~FActorIteratorState()
A destructor.
Definition KarmaUtilities.h:192
FORCEINLINE AActor * GetActorChecked() const
Returns the current suitable actor pointed at by the Iterator.
Definition KarmaUtilities.h:204
int32_t m_Index
Definition KarmaUtilities.h:98
int32_t m_ConsideredCount
Definition KarmaUtilities.h:104
AActor * m_CurrentActor
Definition KarmaUtilities.h:107
The basic utilities class.
Definition KarmaUtilities.h:30
static unsigned char * GetImagePixelData(char const *fileName, int *width, int *height, int *channels, int req_comp)
Gathers image pixel data, arranged left-to-right, top-to-bottom, for the supplied image file.
Definition KarmaUtilities.cpp:32
static std::string GetFilePath(const std::string &str)
Extracts the file path from file source string (filepath + filename.extention).
Definition KarmaUtilities.cpp:26
static std::string ReadFileToSpitString(const std::string &filePath)
Generates a sting of text contained within a file.
Definition KarmaUtilities.cpp:5
FORCEINLINE AActor * operator->() const
Returns the current suitable actor pointed at by the Iterator.
Definition KarmaUtilities.h:364
void operator++()
Iterates to next suitable actor.
Definition KarmaUtilities.h:304
TActorIteratorBase(const UWorld *InWorld, TSubclassOf< AActor > InClass, const EActorIteratorFlags InFlags)
Constructor for setting m_State (FActorIteratorState) with the supplied InWorld and InClass.
Definition KarmaUtilities.h:290
FORCEINLINE bool CanIterateLevel(const ULevel *Level) const
Used to examine whether this level is valid for iteration or not. This function should be redefined (...
Definition KarmaUtilities.h:413
FORCEINLINE bool IsActorSuitable(const AActor *Actor) const
Determines whether this is a valid actor or not. This function should be redefined (thus hiding this ...
Definition KarmaUtilities.h:446
FORCEINLINE AActor * operator*() const
Returns the current suitable actor pointed at by the Iterator.
Definition KarmaUtilities.h:353
TActorIteratorBase(EActorIteratorType)
A constructor.
Definition KarmaUtilities.h:275
void ClearCurrent()
Clears the current Actor in the array (setting it to NULL).
Definition KarmaUtilities.h:386
int32 GetProgressNumerator() const
Returns the number of actors considered thus far. Can be used in combination with GetProgressDenomina...
Definition KarmaUtilities.h:399
Template to allow TClassType's to be passed around with type safety.
Definition SubClassOf.h:117
An object class.
Definition Class.h:158
A Level is a collection of Actors (lights, volumes, mesh instances etc.). Multiple Levels can be load...
Definition Level.h:28
virtual UWorld * GetWorld() const override final
Override for UObject's GetWorld.
Definition Level.cpp:17
FORCEINLINE bool IsA(OtherClassType SomeBase) const
Definition UObjectBase.h:288
The base class of all the game code relevant objects.
Definition Object.h:106
bool IsSelected() const
Test the selection state of a UObject.
Definition Object.cpp:181
The World is the top level object representing a map or a sandbox in which Actors and Components will...
Definition World.h:148
ULevel * GetPersistentLevel() const
Definition World.h:199
Karma's std::vector wrapper with additional functionalities.
Definition KarmaTypes.h:243
uint32_t Num() const
Returns the total number of elements in a vector.
Definition KarmaTypes.h:454
const std::vector< BuildingBlock > & GetElements() const
Getter for the elements of vector.
Definition KarmaTypes.h:536