Add FindActor with type and name to level.

This commit is contained in:
Chandler Cox
2023-06-02 15:29:28 -05:00
parent efed1f5b1d
commit fcbe624f7c
3 changed files with 40 additions and 0 deletions

View File

@@ -1481,6 +1481,16 @@ Actor* Level::FindActor(const MClass* type)
return result;
}
Actor* Level::FindActor(const MClass* type, const StringView& name)
{
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, name);
return result;
}
Script* Level::FindScript(const MClass* type)
{
CHECK_RETURN(type, nullptr);

View File

@@ -66,6 +66,17 @@ namespace FlaxEngine
{
return FindActor(typeof(T)) as T;
}
/// <summary>
/// Tries to find actor of the given type and name in all loaded scenes.
/// </summary>
/// <param name="name">Name of the object.</param>
/// <typeparam name="T">Type of the object.</typeparam>
/// <returns>Found actor or null.</returns>
public static T FindActor<T>(string name) where T : Actor
{
return FindActor(typeof(T), name) as T;
}
/// <summary>
/// Tries to find actor with the given ID in all loaded scenes. It's very fast O(1) lookup.

View File

@@ -363,6 +363,14 @@ public:
/// <returns>Found actor or null.</returns>
API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type);
/// <summary>
/// Tries to find the actor of the given type and name 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="name">The name of the actor.</param>
/// <returns>Actor instance if found, null otherwise.</returns>
API_FUNCTION() static Actor* FindActor(API_PARAM(Attributes="TypeReference(typeof(Actor))") const MClass* type, const StringView& name);
/// <summary>
/// Tries to find the actor of the given type in all the loaded scenes.
/// </summary>
@@ -373,6 +381,17 @@ public:
return (T*)FindActor(T::GetStaticClass());
}
/// <summary>
/// Tries to find the actor of the given type and name in all the loaded scenes.
/// </summary>
/// <param name="name">The name of the actor.</param>
/// <returns>Actor instance if found, null otherwise.</returns>
template<typename T>
FORCE_INLINE static T* FindActor(const StringView& name)
{
return (T*)FindActor(T::GetStaticClass(), name);
}
/// <summary>
/// Tries to find the script of the given type in all the loaded scenes.
/// </summary>