Optimize actor search

This commit is contained in:
Mr. Capybara
2023-10-22 13:26:07 -04:00
parent 6c45141ef9
commit fff8a1e8a6
5 changed files with 41 additions and 26 deletions

View File

@@ -1359,14 +1359,16 @@ Actor* Actor::FindActor(const StringView& name) const
return result; return result;
} }
Actor* Actor::FindActor(const MClass* type) const Actor* Actor::FindActor(const MClass* type, bool activeOnly) const
{ {
CHECK_RETURN(type, nullptr); CHECK_RETURN(type, nullptr);
if (activeOnly && !_isActive)
return nullptr;
if (GetClass()->IsSubClassOf(type)) if (GetClass()->IsSubClassOf(type))
return const_cast<Actor*>(this); return const_cast<Actor*>(this);
for (auto child : Children) for (auto child : Children)
{ {
const auto actor = child->FindActor(type); const auto actor = child->FindActor(type, activeOnly);
if (actor) if (actor)
return actor; return actor;
} }
@@ -1387,14 +1389,16 @@ Actor* Actor::FindActor(const MClass* type, const StringView& name) const
return nullptr; 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); CHECK_RETURN(type, nullptr);
if (activeOnly && !_isActive)
return nullptr;
if (GetClass()->IsSubClassOf(type) && HasTag(tag)) if (GetClass()->IsSubClassOf(type) && HasTag(tag))
return const_cast<Actor*>(this); return const_cast<Actor*>(this);
for (auto child : Children) for (auto child : Children)
{ {
const auto actor = child->FindActor(type, tag); const auto actor = child->FindActor(type, tag, activeOnly);
if (actor) if (actor)
return actor; return actor;
} }

View File

@@ -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). /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy).
/// </summary> /// </summary>
/// <typeparam name="T">Type of the object.</typeparam> /// <typeparam name="T">Type of the object.</typeparam>
/// <param name="activeOnly">Finds only a active actor.</param>
/// <returns>Actor instance if found, null otherwise.</returns> /// <returns>Actor instance if found, null otherwise.</returns>
public T FindActor<T>() where T : Actor public T FindActor<T>(bool activeOnly = false) where T : Actor
{ {
return FindActor(typeof(T)) as T; return FindActor(typeof(T), activeOnly) as T;
} }
/// <summary> /// <summary>
@@ -269,16 +270,17 @@ namespace FlaxEngine
{ {
return FindActor(typeof(T), name) as T; return FindActor(typeof(T), name) as T;
} }
/// <summary> /// <summary>
/// Tries to find actor of the given type and tag in this actor hierarchy (checks this actor and all children hierarchy). /// Tries to find actor of the given type and tag in this actor hierarchy (checks this actor and all children hierarchy).
/// </summary> /// </summary>
/// <param name="tag">A tag on the object.</param> /// <param name="tag">A tag on the object.</param>
/// <typeparam name="T">Type of the object.</typeparam> /// <typeparam name="T">Type of the object.</typeparam>
/// <param name="activeOnly">Finds only an active actor.</param>
/// <returns>Actor instance if found, null otherwise.</returns> /// <returns>Actor instance if found, null otherwise.</returns>
public T FindActor<T>(Tag tag) where T : Actor public T FindActor<T>(Tag tag, bool activeOnly = false) where T : Actor
{ {
return FindActor(typeof(T), tag) as T; return FindActor(typeof(T), tag, activeOnly) as T;
} }
/// <summary> /// <summary>

View File

@@ -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). /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy).
/// </summary> /// </summary>
/// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param> /// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param>
/// <param name="activeOnly">Finds only a active actor.</param>
/// <returns>Actor instance if found, null otherwise.</returns> /// <returns>Actor instance if found, null otherwise.</returns>
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;
/// <summary> /// <summary>
/// Tries to find the actor of the given type and name in this actor hierarchy (checks this actor and all children hierarchy). /// 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:
/// </summary> /// </summary>
/// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param> /// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param>
/// <param name="tag">The tag of the actor to search for.</param> /// <param name="tag">The tag of the actor to search for.</param>
/// <param name="activeOnly">Finds only an active actor.</param>
/// <returns>Actor instance if found, null otherwise.</returns> /// <returns>Actor instance if found, null otherwise.</returns>
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;
/// <summary> /// <summary>
/// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy). /// Tries to find the actor of the given type in this actor hierarchy (checks this actor and all children hierarchy).

View File

@@ -1421,13 +1421,13 @@ Actor* Level::FindActor(const StringView& name)
return result; return result;
} }
Actor* Level::FindActor(const MClass* type) Actor* Level::FindActor(const MClass* type, bool activeOnly)
{ {
CHECK_RETURN(type, nullptr); CHECK_RETURN(type, nullptr);
Actor* result = nullptr; Actor* result = nullptr;
ScopeLock lock(ScenesLock); ScopeLock lock(ScenesLock);
for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++) for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++)
result = Scenes[i]->FindActor(type); result = Scenes[i]->FindActor(type, activeOnly);
return result; return result;
} }
@@ -1441,29 +1441,33 @@ Actor* Level::FindActor(const MClass* type, const StringView& name)
return result; 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)) if (node->HasTag(tag))
return node; return node;
Actor* result = nullptr; Actor* result = nullptr;
for (Actor* child : node->Children) for (Actor* child : node->Children)
{ {
result = FindActorRecursive(child, tag); result = FindActorRecursive(child, tag, activeOnly);
if (result) if (result)
break; break;
} }
return result; 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); CHECK_RETURN(type, nullptr);
if (activeOnly && !node->GetIsActive())
return nullptr;
if (node->HasTag(tag) && node->GetClass()->IsSubClassOf(type)) if (node->HasTag(tag) && node->GetClass()->IsSubClassOf(type))
return node; return node;
Actor* result = nullptr; Actor* result = nullptr;
for (Actor* child : node->Children) for (Actor* child : node->Children)
{ {
result = FindActorRecursiveByType(child, type, tag); result = FindActorRecursiveByType(child, type, tag, activeOnly);
if (result) if (result)
break; break;
} }
@@ -1496,30 +1500,30 @@ void FindActorsRecursiveByParentTags(Actor* node, const Array<Tag>& tags, const
FindActorsRecursiveByParentTags(child, tags, activeOnly, result); 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(); PROFILE_CPU();
if (root) if (root)
return FindActorRecursive(root, tag); return FindActorRecursive(root, tag, activeOnly);
Actor* result = nullptr; Actor* result = nullptr;
for (Scene* scene : Scenes) for (Scene* scene : Scenes)
{ {
result = FindActorRecursive(scene, tag); result = FindActorRecursive(scene, tag, activeOnly);
if (result) if (result)
break; break;
} }
return result; 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); CHECK_RETURN(type, nullptr);
if (root) if (root)
return FindActorRecursiveByType(root, type, tag); return FindActorRecursiveByType(root, type, tag, activeOnly);
Actor* result = nullptr; Actor* result = nullptr;
ScopeLock lock(ScenesLock); ScopeLock lock(ScenesLock);
for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++) 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; return result;
} }

View File

@@ -360,8 +360,9 @@ public:
/// Tries to find the actor of the given type in all the loaded scenes. /// Tries to find the actor of the given type in all the loaded scenes.
/// </summary> /// </summary>
/// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param> /// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param>
/// <param name="activeOnly">Finds only an active actor.</param>
/// <returns>Found actor or null.</returns> /// <returns>Found actor or null.</returns>
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);
/// <summary> /// <summary>
/// Tries to find the actor of the given type and name in all the loaded scenes. /// 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). /// Tries to find the actor with the given tag (returns the first one found).
/// </summary> /// </summary>
/// <param name="tag">The tag of the actor to search for.</param> /// <param name="tag">The tag of the actor to search for.</param>
/// <param name="activeOnly">Finds only an active actor.</param>
/// <param name="root">The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes.</param> /// <param name="root">The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes.</param>
/// <returns>Found actor or null.</returns> /// <returns>Found actor or null.</returns>
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);
/// <summary> /// <summary>
/// Tries to find the actor of the given type and tag in all the loaded scenes. /// Tries to find the actor of the given type and tag in all the loaded scenes.
/// </summary> /// </summary>
/// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param> /// <param name="type">Type of the actor to search for. Includes any actors derived from the type.</param>
/// <param name="tag">The tag of the actor to search for.</param> /// <param name="tag">The tag of the actor to search for.</param>
/// <param name="activeOnly">Finds only an active actor.</param>
/// <param name="root">The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes.</param> /// <param name="root">The custom root actor to start searching from (hierarchical), otherwise null to search all loaded scenes.</param>
/// <returns>Actor instance if found, null otherwise.</returns> /// <returns>Actor instance if found, null otherwise.</returns>
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);
/// <summary> /// <summary>
/// Tries to find the actors with the given tag (returns all found). /// Tries to find the actors with the given tag (returns all found).