diff --git a/Source/Engine/Level/Actor.cpp b/Source/Engine/Level/Actor.cpp index 090f3c071..67d84b81d 100644 --- a/Source/Engine/Level/Actor.cpp +++ b/Source/Engine/Level/Actor.cpp @@ -1323,6 +1323,20 @@ Actor* Actor::FindActor(const MClass* type) const return nullptr; } +Actor* Actor::FindActor(const MClass* type, const StringView& name) const +{ + CHECK_RETURN(type, nullptr); + if (GetClass()->IsSubClassOf(type) && StringUtils::Compare(*_name, *name) == 0) + return const_cast(this); + for (auto child : Children) + { + const auto actor = child->FindActor(type, name); + if (actor) + return actor; + } + return nullptr; +} + Script* Actor::FindScript(const MClass* type) const { CHECK_RETURN(type, nullptr); diff --git a/Source/Engine/Level/Actor.cs b/Source/Engine/Level/Actor.cs index bd005d415..27263dfea 100644 --- a/Source/Engine/Level/Actor.cs +++ b/Source/Engine/Level/Actor.cs @@ -259,6 +259,17 @@ namespace FlaxEngine return FindActor(typeof(T)) as T; } + /// + /// Tries to find the actor of the given type and name in this actor hierarchy (checks this actor and all children hierarchy). + /// + /// Name of the object. + /// Type of the object. + /// Actor instance if found, null otherwise. + public T FindActor(string name) where T : Actor + { + return FindActor(typeof(T), name) as T; + } + /// /// Searches for all actors of a specific type in this actor children list. /// diff --git a/Source/Engine/Level/Actor.h b/Source/Engine/Level/Actor.h index 53427ab52..0dc7d34e3 100644 --- a/Source/Engine/Level/Actor.h +++ b/Source/Engine/Level/Actor.h @@ -734,6 +734,14 @@ public: /// Actor instance if found, null otherwise. API_FUNCTION() Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type) const; + /// + /// Tries to find the actor of the given type and name 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. + /// The name of the actor. + /// Actor instance if found, null otherwise. + API_FUNCTION() Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const StringView& name) const; + /// /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy). /// @@ -744,6 +752,17 @@ public: return (T*)FindActor(T::GetStaticClass()); } + /// + /// Tries to find the actor of the given type and name in this actor hierarchy (checks this actor and all children hierarchy). + /// + /// The name of the actor. + /// Actor instance if found, null otherwise. + template + FORCE_INLINE T* FindActor(const StringView& name) const + { + return (T*)FindActor(T::GetStaticClass(), name); + } + /// /// Tries to find the script of the given type in this actor hierarchy (checks this actor and all children hierarchy). ///