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;
}
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<Actor*>(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<Actor*>(this);
for (auto child : Children)
{
const auto actor = child->FindActor(type, tag);
const auto actor = child->FindActor(type, tag, activeOnly);
if (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).
/// </summary>
/// <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>
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>
@@ -269,16 +270,17 @@ namespace FlaxEngine
{
return FindActor(typeof(T), name) as T;
}
/// <summary>
/// Tries to find actor of the given type and tag in this actor hierarchy (checks this actor and all children hierarchy).
/// </summary>
/// <param name="tag">A tag on the object.</param>
/// <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>
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>

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).
/// </summary>
/// <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>
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>
/// 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>
/// <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="activeOnly">Finds only an active actor.</param>
/// <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>
/// 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;
}
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<Tag>& 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;
}

View File

@@ -360,8 +360,9 @@ public:
/// Tries to find the actor of the given type in all the loaded scenes.
/// </summary>
/// <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>
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>
/// 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).
/// </summary>
/// <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>
/// <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>
/// Tries to find the actor of the given type and tag in all the loaded scenes.
/// </summary>
/// <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="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>
/// <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>
/// Tries to find the actors with the given tag (returns all found).