diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 75e3c12d4..06ddd8d8d 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -1359,14 +1359,16 @@ Actor* Actor::FindActor(const StringView& name) const return result; } -Actor* Actor::FindActor(const MClass* type) const +Actor* Actor::FindActor(const MClass* type, bool activeOnly) const { CHECK_RETURN(type, nullptr); + if (activeOnly && !_isActive) + return nullptr; if (GetClass()->IsSubClassOf(type)) return const_cast(this); for (auto child : Children) { - const auto actor = child->FindActor(type); + const auto actor = child->FindActor(type, activeOnly); if (actor) return actor; } @@ -1387,14 +1389,16 @@ Actor* Actor::FindActor(const MClass* type, const StringView& name) const return nullptr; } -Actor* Actor::FindActor(const MClass* type, const Tag& tag) const +Actor* Actor::FindActor(const MClass* type, const Tag& tag, bool activeOnly) const { CHECK_RETURN(type, nullptr); + if (activeOnly && !_isActive) + return nullptr; if (GetClass()->IsSubClassOf(type) && HasTag(tag)) return const_cast(this); for (auto child : Children) { - const auto actor = child->FindActor(type, tag); + const auto actor = child->FindActor(type, tag, activeOnly); if (actor) return actor; } diff --git a/Source/Engine/Level/Actor.cs b/Source/Engine/Level/Actor.cs index dbe8a89b5..f1fc09bfd 100644 --- a/Source/Engine/Level/Actor.cs +++ b/Source/Engine/Level/Actor.cs @@ -253,10 +253,11 @@ namespace FlaxEngine /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy). /// /// Type of the object. + /// Finds only a active actor. /// Actor instance if found, null otherwise. - public T FindActor() where T : Actor + public T FindActor(bool activeOnly = false) where T : Actor { - return FindActor(typeof(T)) as T; + return FindActor(typeof(T), activeOnly) as T; } /// @@ -269,16 +270,17 @@ namespace FlaxEngine { return FindActor(typeof(T), name) as T; } - + /// /// Tries to find actor of the given type and tag in this actor hierarchy (checks this actor and all children hierarchy). /// /// A tag on the object. /// Type of the object. + /// Finds only an active actor. /// Actor instance if found, null otherwise. - public T FindActor(Tag tag) where T : Actor + public T FindActor(Tag tag, bool activeOnly = false) where T : Actor { - return FindActor(typeof(T), tag) as T; + return FindActor(typeof(T), tag, activeOnly) as T; } /// diff --git a/Source/Engine/Level/Actor.h b/Source/Engine/Level/Actor.h index a1f2dc78f..ded07fce5 100644 --- a/Source/Engine/Level/Actor.h +++ b/Source/Engine/Level/Actor.h @@ -751,8 +751,9 @@ public: /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy). /// /// Type of the actor to search for. Includes any actors derived from the type. + /// Finds only a active actor. /// Actor instance if found, null otherwise. - API_FUNCTION() Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type) const; + API_FUNCTION() Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, bool activeOnly = false) const; /// /// Tries to find the actor of the given type and name in this actor hierarchy (checks this actor and all children hierarchy). @@ -767,8 +768,9 @@ public: /// /// Type of the actor to search for. Includes any actors derived from the type. /// The tag of the actor to search for. + /// Finds only an active actor. /// Actor instance if found, null otherwise. - API_FUNCTION() Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const Tag& tag) const; + API_FUNCTION() Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const Tag& tag, bool activeOnly = false) const; /// /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy). diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index dfd5786c6..19d0313bb 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -1421,13 +1421,13 @@ Actor* Level::FindActor(const StringView& name) return result; } -Actor* Level::FindActor(const MClass* type) +Actor* Level::FindActor(const MClass* type, bool activeOnly) { CHECK_RETURN(type, nullptr); Actor* result = nullptr; ScopeLock lock(ScenesLock); for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++) - result = Scenes[i]->FindActor(type); + result = Scenes[i]->FindActor(type, activeOnly); return result; } @@ -1441,29 +1441,33 @@ Actor* Level::FindActor(const MClass* type, const StringView& name) return result; } -Actor* FindActorRecursive(Actor* node, const Tag& tag) +Actor* FindActorRecursive(Actor* node, const Tag& tag, bool activeOnly) { + if (activeOnly && !node->GetIsActive()) + return nullptr; if (node->HasTag(tag)) return node; Actor* result = nullptr; for (Actor* child : node->Children) { - result = FindActorRecursive(child, tag); + result = FindActorRecursive(child, tag, activeOnly); if (result) break; } return result; } -Actor* FindActorRecursiveByType(Actor* node, const MClass* type, const Tag& tag) +Actor* FindActorRecursiveByType(Actor* node, const MClass* type, const Tag& tag, bool activeOnly) { CHECK_RETURN(type, nullptr); + if (activeOnly && !node->GetIsActive()) + return nullptr; if (node->HasTag(tag) && node->GetClass()->IsSubClassOf(type)) return node; Actor* result = nullptr; for (Actor* child : node->Children) { - result = FindActorRecursiveByType(child, type, tag); + result = FindActorRecursiveByType(child, type, tag, activeOnly); if (result) break; } @@ -1496,30 +1500,30 @@ void FindActorsRecursiveByParentTags(Actor* node, const Array& tags, const FindActorsRecursiveByParentTags(child, tags, activeOnly, result); } -Actor* Level::FindActor(const Tag& tag, Actor* root) +Actor* Level::FindActor(const Tag& tag, bool activeOnly, Actor* root) { PROFILE_CPU(); if (root) - return FindActorRecursive(root, tag); + return FindActorRecursive(root, tag, activeOnly); Actor* result = nullptr; for (Scene* scene : Scenes) { - result = FindActorRecursive(scene, tag); + result = FindActorRecursive(scene, tag, activeOnly); if (result) break; } return result; } -Actor* Level::FindActor(const MClass* type, const Tag& tag, Actor* root) +Actor* Level::FindActor(const MClass* type, const Tag& tag, bool activeOnly, Actor* root) { CHECK_RETURN(type, nullptr); if (root) - return FindActorRecursiveByType(root, type, tag); + return FindActorRecursiveByType(root, type, tag, activeOnly); Actor* result = nullptr; ScopeLock lock(ScenesLock); for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++) - result = Scenes[i]->FindActor(type, tag); + result = Scenes[i]->FindActor(type, tag, activeOnly); return result; } diff --git a/Source/Engine/Level/Level.h b/Source/Engine/Level/Level.h index 1f0acda2d..bb97b8908 100644 --- a/Source/Engine/Level/Level.h +++ b/Source/Engine/Level/Level.h @@ -360,8 +360,9 @@ public: /// Tries to find the actor of the given type in all the loaded scenes. /// /// Type of the actor to search for. Includes any actors derived from the type. + /// Finds only an active actor. /// Found actor or null. - API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type); + API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, bool activeOnly = false); /// /// Tries to find the actor of the given type and name in all the loaded scenes. @@ -375,18 +376,20 @@ public: /// Tries to find the actor with the given tag (returns the first one found). /// /// The tag of the actor to search for. + /// Finds only an active actor. /// 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); + API_FUNCTION() static Actor* FindActor(const Tag& tag, bool activeOnly = false, Actor* root = nullptr); /// /// Tries to find the actor of the given type and tag in all the loaded scenes. /// /// Type of the actor to search for. Includes any actors derived from the type. /// The tag of the actor to search for. + /// Finds only an active actor. /// The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes. /// Actor instance if found, null otherwise. - API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const Tag& tag, Actor* root = nullptr); + API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const Tag& tag, bool activeOnly = false, Actor* root = nullptr); /// /// Tries to find the actors with the given tag (returns all found).