Last active
March 1, 2024 16:42
-
-
Save Shilo/2141895e5dd0b920f4509be963f242da to your computer and use it in GitHub Desktop.
Godot 4 extensions for Node.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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