using Godot;
namespace SupaLidlGame.Extensions;
public static class NodeExtensions
{
///
/// Iterates through each ancestor until it finds an ancestor of type
/// T
///
[System.Obsolete]
public static T GetAncestorDeprecated(this Node node) where T : Node
{
Node parent;
while ((parent = node.GetParent()) != null)
{
if (parent is T t)
{
return t;
}
node = parent;
}
return null;
}
///
/// A version GetNode that returns null rather than cause an
/// exception if the node is not found or is not the same type.
///
///
/// null if name does not match
/// a valid Node
///
public static T GetN(this Node node, string name) where T : Node
{
return node.GetNode(name) as T;
}
public static T FindChildOfType(this Node node) where T : Node
{
foreach (Node child in node.GetChildren())
{
if (child is T t)
{
return t;
}
}
return null;
}
}