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

@@ -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;
}