Add support in GetScript to interface types

#290
This commit is contained in:
Wojtek Figat
2023-02-10 15:36:02 +01:00
parent 9b6ba67186
commit d58a77cc60
3 changed files with 39 additions and 10 deletions

View File

@@ -498,10 +498,21 @@ Script* Actor::GetScript(int32 index) const
Script* Actor::GetScript(const MClass* type) const
{
CHECK_RETURN(type, nullptr);
for (auto script : Scripts)
if (type->IsInterface())
{
if (script->GetClass()->IsSubClassOf(type))
return script;
for (auto script : Scripts)
{
if (script->GetClass()->HasInterface(type))
return script;
}
}
else
{
for (auto script : Scripts)
{
if (script->GetClass()->IsSubClassOf(type))
return script;
}
}
return nullptr;
}
@@ -509,9 +520,18 @@ Script* Actor::GetScript(const MClass* type) const
Array<Script*> Actor::GetScripts(const MClass* type) const
{
Array<Script*> result;
for (auto script : Scripts)
if (script->GetClass()->IsSubClassOf(type))
result.Add(script);
if (type->IsInterface())
{
for (auto script : Scripts)
if (script->GetClass()->HasInterface(type))
result.Add(script);
}
else
{
for (auto script : Scripts)
if (script->GetClass()->IsSubClassOf(type))
result.Add(script);
}
return result;
}

View File

@@ -222,7 +222,7 @@ namespace FlaxEngine
/// </summary>
/// <typeparam name="T">Type of the script to search for. Includes any scripts derived from the type.</typeparam>
/// <returns>The script or null if failed to find.</returns>
public T GetScript<T>() where T : Script
public T GetScript<T>() where T : class
{
return GetScript(typeof(T)) as T;
}
@@ -233,7 +233,7 @@ namespace FlaxEngine
/// <typeparam name="T">Type of the script to search for. Includes any scripts derived from the type.</typeparam>
/// <param name="script">The returned script, valid only if method returns true.</param>
/// <returns>True if found a script of that type or false if failed to find.</returns>
public bool TryGetScript<T>(out T script) where T : Script
public bool TryGetScript<T>(out T script) where T : class
{
script = GetScript(typeof(T)) as T;
return script != null;
@@ -244,7 +244,7 @@ namespace FlaxEngine
/// </summary>
/// <typeparam name="T">Type of the object.</typeparam>
/// <returns>Script instance if found, null otherwise.</returns>
public T FindScript<T>() where T : Script
public T FindScript<T>() where T : class
{
return FindScript(typeof(T)) as T;
}
@@ -290,7 +290,7 @@ namespace FlaxEngine
/// </summary>
/// <typeparam name="T">Type of the scripts to search for. Includes any scripts derived from the type.</typeparam>
/// <returns>All scripts matching the specified type.</returns>
public T[] GetScripts<T>() where T : Script
public T[] GetScripts<T>() where T : class
{
var count = ScriptsCount;
var length = 0;

View File

@@ -124,6 +124,15 @@ bool MClass::IsSubClassOf(const MonoClass* monoClass) const
}
#endif
bool MClass::HasInterface(const MClass* klass) const
{
#if USE_MONO
return klass && mono_class_is_assignable_from(klass->GetNative(), _monoClass) != 0;
#else
return false;
#endif
}
bool MClass::IsInstanceOfType(MObject* object) const
{
if (object == nullptr)