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. ///