Add Level.GetActors and Level.GetScripts

This commit is contained in:
Wojtek Figat
2021-02-09 23:31:23 +01:00
parent b8420feed1
commit 73efa35008
3 changed files with 80 additions and 10 deletions

View File

@@ -1326,12 +1326,8 @@ Actor* Level::FindActor(const MClass* type)
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);
}
return result;
}
@@ -1340,25 +1336,57 @@ Script* Level::FindScript(const MClass* type)
CHECK_RETURN(type, nullptr);
Script* result = nullptr;
ScopeLock lock(ScenesLock);
for (int32 i = 0; result == nullptr && i < Scenes.Count(); i++)
{
result = Scenes[i]->FindScript(type);
return result;
}
namespace
{
void GetActors(const MClass* type, Actor* actor, Array<Actor*>& result)
{
if (actor->GetClass()->IsSubClassOf(type))
result.Add(actor);
for (auto child : actor->Children)
GetActors(type, child, result);
}
void GetScripts(const MClass* type, Actor* actor, Array<Script*>& result)
{
for (auto script : actor->Scripts)
if (script->GetClass()->IsSubClassOf(type))
result.Add(script);
for (auto child : actor->Children)
GetScripts(type, child, result);
}
}
Array<Actor*> Level::GetActors(const MClass* type)
{
Array<Actor*> result;
CHECK_RETURN(type, result);
ScopeLock lock(ScenesLock);
for (int32 i = 0; i < Scenes.Count(); i++)
::GetActors(type, Scenes[i], result);
return result;
}
Array<Script*> Level::GetScripts(const MClass* type)
{
Array<Script*> result;
CHECK_RETURN(type, result);
ScopeLock lock(ScenesLock);
for (int32 i = 0; i < Scenes.Count(); i++)
::GetScripts(type, Scenes[i], result);
return result;
}
Scene* Level::FindScene(const Guid& id)
{
ScopeLock lock(ScenesLock);
for (int32 i = 0; i < Scenes.Count(); i++)
{
if (Scenes[i]->GetID() == id)
return Scenes[i];
}
return nullptr;
}

View File

@@ -77,5 +77,33 @@ namespace FlaxEngine
{
return FindActor(id) as T;
}
/// <summary>
/// Finds all the scripts of the given type in all the loaded scenes.
/// </summary>
/// <typeparam name="T">Type of the object.</typeparam>
/// <returns>Found scripts list.</returns>
public static T[] GetScripts<T>() where T : Script
{
var scripts = GetScripts(typeof(T));
var result = new T[scripts.Length];
for (int i = 0; i < scripts.Length; i++)
result[i] = scripts[i] as T;
return result;
}
/// <summary>
/// Finds all the actors of the given type in all the loaded scenes.
/// </summary>
/// <typeparam name="T">Type of the object.</typeparam>
/// <returns>Found actors list.</returns>
public static T[] GetActors<T>() where T : Actor
{
var actors = GetActors(typeof(T));
var result = new T[actors.Length];
for (int i = 0; i < actors.Length; i++)
result[i] = actors[i] as T;
return result;
}
}
}

View File

@@ -377,6 +377,20 @@ public:
return (T*)FindScript(T::GetStaticClass());
}
/// <summary>
/// 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>
/// <returns>Found actors list.</returns>
API_FUNCTION() static Array<Actor*> GetActors(const MClass* type);
/// <summary>
/// Finds all the scripts of the given type in all the loaded scenes.
/// </summary>
/// <param name="type">Type of the script to search for. Includes any scripts derived from the type.</param>
/// <returns>Found scripts list.</returns>
API_FUNCTION() static Array<Script*> GetScripts(const MClass* type);
/// <summary>
/// Tries to find scene with given ID.
/// </summary>