You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
913 B
27 lines
913 B
using System;
|
|
using Godot;
|
|
namespace Babushka.scripts.CSharp.Common.Util;
|
|
|
|
public static class NodeExtension
|
|
{
|
|
/// <summary>
|
|
/// Searches for a parent node of the specified type.
|
|
/// </summary>
|
|
/// <typeparam name="T">The type of the parent node to search for. The search is successful, when <code>searchedNode is T</code></typeparam>
|
|
/// <param name="self">The node from which to start the search.</param>
|
|
/// <returns>The parent node of type T if found, otherwise throws an exception.</returns>
|
|
public static T FindParentByType<T>(this Node self)
|
|
{
|
|
var parent = self.GetParent();
|
|
while (parent != null)
|
|
{
|
|
if (parent is T tParent)
|
|
{
|
|
return tParent;
|
|
}
|
|
parent = parent.GetParent();
|
|
}
|
|
throw new Exception($"Parent of type {typeof(T)} not found for node {self.Name}");
|
|
}
|
|
}
|