SupaLidlGame/Extensions/Node.cs

42 lines
1.1 KiB
C#
Raw Permalink Normal View History

2022-11-25 09:11:46 -08:00
using Godot;
namespace SupaLidlGame.Extensions
{
public static class NodeExtensions
{
/// <summary>
/// Iterates through each ancestor until it finds an ancestor of type
/// <c>T</c>
/// </summary>
public static T GetAncestor<T>(this Node node) where T : Node
{
Node parent;
while ((parent = node.GetParent()) != null)
{
if (parent is T t)
{
return t;
}
node = parent;
}
return null;
}
2022-11-25 11:59:55 -08:00
/// <summary>
2022-11-27 19:37:16 -08:00
/// A version <c>GetNode</c> that returns null rather than cause an
/// exception if the node is not found or is not the same type.
2022-11-25 11:59:55 -08:00
/// </summary>
2022-11-27 19:37:16 -08:00
/// <returns>
/// <see langword="null">null</see> if <param>name</param> does not match
/// a valid Node
/// </returns>
public static T GetN<T>(this Node node, string name) where T : Node
2022-11-25 11:59:55 -08:00
{
return node.GetNode(name) as T;
}
2022-11-25 09:11:46 -08:00
}
}