Skip to content

Instantly share code, notes, and snippets.

@Shilo
Last active March 1, 2024 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shilo/2141895e5dd0b920f4509be963f242da to your computer and use it in GitHub Desktop.
Save Shilo/2141895e5dd0b920f4509be963f242da to your computer and use it in GitHub Desktop.
Godot 4 extensions for Node.
using Godot;
public static class NodeExtensions
{
public static void RemoveAndFree(this Node node)
{
Node parent = node.GetParent();
if (!GodotObject.IsInstanceValid(parent))
return;
parent.RemoveChild(node);
node.QueueFree();
}
public static T GetNodeOfType<T>(this Node node) where T : Node
{
foreach (Node child in node.GetChildren())
{
if (child is T typedNode)
return typedNode;
}
return null;
}
public static T GetAncestorOfType<T>(this Node node) where T : Node
{
Node parent = node.GetParent();
while (parent != null)
{
if (parent is T typedNode)
return typedNode;
parent = parent.GetParent();
}
return null;
}
public static SignalAwaiter WaitOneFrame(this Node node)
{
return node.ToSignal(node.GetTree(), SceneTree.SignalName.ProcessFrame);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment