diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 338598541..6c53adf1c 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -755,6 +755,59 @@ int32 Level::GetLayerIndex(const StringView& layer) return result; } +Actor* FindActorRecursive(Actor* node, const Tag& tag) +{ + if (node->HasTag(tag)) + return node; + Actor* result = nullptr; + for (Actor* child : node->Children) + { + result = FindActorRecursive(child, tag); + if (result) + break; + } + return result; +} + +Actor* Level::FindActor(const Tag& tag, Actor* root) +{ + PROFILE_CPU(); + if (root) + return FindActorRecursive(root, tag); + Actor* result = nullptr; + for (Scene* scene : Scenes) + { + result = FindActorRecursive(scene, tag); + if (result) + break; + } + return result; +} + +void FindActorRecursive(Actor* node, const Tag& tag, Array& result) +{ + if (node->HasTag(tag)) + result.Add(node); + for (Actor* child : node->Children) + FindActorRecursive(child, tag, result); +} + +Array Level::FindActors(const Tag& tag, Actor* root) +{ + PROFILE_CPU(); + Array result; + if (root) + { + FindActorRecursive(root, tag); + } + else + { + for (Scene* scene : Scenes) + FindActorRecursive(scene, tag); + } + return result; +} + void Level::callActorEvent(ActorEventType eventType, Actor* a, Actor* b) { PROFILE_CPU(); diff --git a/Source/Engine/Level/Level.h b/Source/Engine/Level/Level.h index 8039464c5..134d5d8b1 100644 --- a/Source/Engine/Level/Level.h +++ b/Source/Engine/Level/Level.h @@ -16,6 +16,7 @@ class Engine; struct RenderView; struct RenderContext; struct RenderContextBatch; +struct Tag; /// /// The scene manager that contains the loaded scenes collection and spawns/deleted actors. @@ -461,6 +462,23 @@ public: /// API_FUNCTION() static int32 GetLayerIndex(const StringView& layer); +public: + /// + /// Tries to find the actor with the given tag (returns the first one found). + /// + /// The tag of the actor to search for. + /// The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes. + /// Found actor or null. + API_FUNCTION() static Actor* FindActor(const Tag& tag, Actor* root = nullptr); + + /// + /// Tries to find the actors with the given tag (returns all found). + /// + /// The tag of the actor to search for. + /// The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes. + /// Found actors or empty if none. + API_FUNCTION() static Array FindActors(const Tag& tag, Actor* root = nullptr); + private: // Actor API enum class ActorEventType diff --git a/Source/Engine/Level/Tags.cs b/Source/Engine/Level/Tags.cs index 7e31f7bc5..b8ea3efe6 100644 --- a/Source/Engine/Level/Tags.cs +++ b/Source/Engine/Level/Tags.cs @@ -166,7 +166,7 @@ namespace FlaxEngine { if (list == null) return false; - foreach (Tag tag in list) + foreach (Tag tag in tags) { if (HasTag(list, tag)) return true; @@ -184,7 +184,7 @@ namespace FlaxEngine { if (list == null) return false; - foreach (Tag tag in list) + foreach (Tag tag in tags) { if (HasTagExact(list, tag)) return true; @@ -204,7 +204,7 @@ namespace FlaxEngine return true; if (list == null || list.Length == 0) return false; - foreach (Tag tag in list) + foreach (Tag tag in tags) { if (!HasTag(list, tag)) return false; @@ -224,7 +224,7 @@ namespace FlaxEngine return true; if (list == null || list.Length == 0) return false; - foreach (Tag tag in list) + foreach (Tag tag in tags) { if (!HasTagExact(list, tag)) return false;