Add ActiveOnly parameter to Level::GetActors

This commit is contained in:
Mr. Capybara
2023-11-03 11:30:24 -04:00
parent 536be6c6cf
commit 1c23f0f5b4
3 changed files with 11 additions and 7 deletions

View File

@@ -1564,12 +1564,14 @@ Script* Level::FindScript(const MClass* type)
namespace
{
void GetActors(const MClass* type, Actor* actor, Array<Actor*>& result)
void GetActors(const MClass* type, Actor* actor, bool activeOnly, Array<Actor*>& result)
{
if (activeOnly && !actor->GetIsActive())
return;
if (actor->GetClass()->IsSubClassOf(type))
result.Add(actor);
for (auto child : actor->Children)
GetActors(type, child, result);
GetActors(type, child, activeOnly, result);
}
void GetScripts(const MClass* type, Actor* actor, Array<Script*>& result)
@@ -1582,13 +1584,13 @@ namespace
}
}
Array<Actor*> Level::GetActors(const MClass* type)
Array<Actor*> Level::GetActors(const MClass* type, bool activeOnly)
{
Array<Actor*> result;
CHECK_RETURN(type, result);
ScopeLock lock(ScenesLock);
for (int32 i = 0; i < Scenes.Count(); i++)
::GetActors(type, Scenes[i], result);
::GetActors(type, Scenes[i], activeOnly, result);
return result;
}

View File

@@ -119,10 +119,11 @@ namespace FlaxEngine
/// Finds all the actors of the given type in all the loaded scenes.
/// </summary>
/// <typeparam name="T">Type of the object.</typeparam>
/// <typeparam name="activeOnly">Finds only active actors.</typeparam>
/// <returns>Found actors list.</returns>
public static T[] GetActors<T>() where T : Actor
public static T[] GetActors<T>(bool activeOnly = false) where T : Actor
{
var actors = GetActors(typeof(T));
var actors = GetActors(typeof(T), activeOnly);
var result = new T[actors.Length];
for (int i = 0; i < actors.Length; i++)
result[i] = actors[i] as T;

View File

@@ -460,8 +460,9 @@ public:
/// Finds all the actors 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 active actors in the scene.</param>
/// <returns>Found actors list.</returns>
API_FUNCTION() static Array<Actor*> GetActors(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type);
API_FUNCTION() static Array<Actor*> GetActors(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, bool activeOnly = false);
/// <summary>
/// Finds all the scripts of the given type in all the loaded scenes.