From efed1f5b1d99f34c57bbc9809cce5864b5aba64b Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 2 Jun 2023 15:12:55 -0500 Subject: [PATCH 1/2] Add `FindActor` by type and name. --- Source/Engine/Level/Actor.cpp | 14 ++++++++++++++ Source/Engine/Level/Actor.cs | 11 +++++++++++ Source/Engine/Level/Actor.h | 19 +++++++++++++++++++ 3 files changed, 44 insertions(+) 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). /// From fcbe624f7c3f3ee4a834bfb7521862263ab30fec Mon Sep 17 00:00:00 2001 From: Chandler Cox Date: Fri, 2 Jun 2023 15:29:28 -0500 Subject: [PATCH 2/2] Add `FindActor` with type and name to level. --- Source/Engine/Level/Level.cpp | 10 ++++++++++ Source/Engine/Level/Level.cs | 11 +++++++++++ Source/Engine/Level/Level.h | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/Source/Engine/Level/Level.cpp b/Source/Engine/Level/Level.cpp index 4fa37c80d..d2cfb648e 100644 --- a/Source/Engine/Level/Level.cpp +++ b/Source/Engine/Level/Level.cpp @@ -1481,6 +1481,16 @@ Actor* Level::FindActor(const MClass* type) return result; } +Actor* Level::FindActor(const MClass* type, const StringView& name) +{ + 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, name); + return result; +} + Script* Level::FindScript(const MClass* type) { CHECK_RETURN(type, nullptr); diff --git a/Source/Engine/Level/Level.cs b/Source/Engine/Level/Level.cs index de589f6df..c38b287ca 100644 --- a/Source/Engine/Level/Level.cs +++ b/Source/Engine/Level/Level.cs @@ -66,6 +66,17 @@ namespace FlaxEngine { return FindActor(typeof(T)) as T; } + + /// + /// Tries to find actor of the given type and name in all loaded scenes. + /// + /// Name of the object. + /// Type of the object. + /// Found actor or null. + public static T FindActor(string name) where T : Actor + { + return FindActor(typeof(T), name) as T; + } /// /// Tries to find actor with the given ID in all loaded scenes. It's very fast O(1) lookup. diff --git a/Source/Engine/Level/Level.h b/Source/Engine/Level/Level.h index ed2efe485..fb5448e20 100644 --- a/Source/Engine/Level/Level.h +++ b/Source/Engine/Level/Level.h @@ -363,6 +363,14 @@ public: /// Found actor or null. API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type); + /// + /// Tries to find the actor of the given type and name in all the loaded scenes. + /// + /// 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() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const StringView& name); + /// /// Tries to find the actor of the given type in all the loaded scenes. /// @@ -373,6 +381,17 @@ public: return (T*)FindActor(T::GetStaticClass()); } + /// + /// Tries to find the actor of the given type and name in all the loaded scenes. + /// + /// The name of the actor. + /// Actor instance if found, null otherwise. + template + FORCE_INLINE static T* FindActor(const StringView& name) + { + return (T*)FindActor(T::GetStaticClass(), name); + } + /// /// Tries to find the script of the given type in all the loaded scenes. ///