Class NodeExtensions
SC4X Godot.Node Extensions
public static class NodeExtensions
- Inheritance
-
NodeExtensions
- Inherited Members
Methods
EnsureSingleton<T>(Node, ref T)
Used internally to ensure an autoloaded node is also a singleton on the C# side of things.
public static bool EnsureSingleton<T>(this Node node, ref T instance) where T : Node
Parameters
node
NodeThe node to perform on
instance
TA reference to the singleton's instance field. This is assigned to the node parameter if this is the first instance. Ideally this field will be static
Returns
- bool
True if this is the first instance, false if it is a duplicate
Type Parameters
T
generally inferred from inputs. The type we are trying to make singleton
GetComponent<T>(Node)
Unity-like component approach. Iterates through the children of the node starting at the end (convention encourages placing component nodes at the bottom of the node's child list to speed up acquisition)
public static T GetComponent<T>(this Node node) where T : Node
Parameters
node
NodeThe node which we wish to acquire the component of.
Returns
- T
The component if found. Else null
Type Parameters
T
The type of component we are trying to acquire. This should be a script attached to a node
GetNode<T>(Node, NodePath, out T, bool)
An effective override of "GetNode" with built-in null checking and a debug environment safeguard. Coincidentally since Godot 4.1, we can just export the node directly, rendering this almost meaningless.
public static bool GetNode<T>(this Node node, NodePath path, out T result, bool alertErrors = true) where T : class
Parameters
node
NodeThe node we are searching from
path
NodePathThe path (absolute or relative to node) to the node we want. Can be a string
result
TOutputs the node if found, else null
alertErrors
bool
Returns
Type Parameters
T
The type of the node to acquire
GetSafe<T>(Node, NodePath, out T, bool)
A redirect to reduce confusion with the built-in function GetNode<T>(Node, NodePath, out T, bool)
public static bool GetSafe<T>(this Node node, NodePath path, out T result, bool alertErrors = true) where T : class
Parameters
node
Nodepath
NodePathresult
TalertErrors
bool
Returns
Type Parameters
T
HandleInput(Node)
Utility to set input as handled. The proper way to do this requires checking into the Node's viewport, which is tedious at best.
public static void HandleInput(this Node node)
Parameters
node
Node
RemoveAllChildren(Node)
Fairly straightforward. Calls QueueFree on all children. I got tired of writing for loops. Sue me.
public static void RemoveAllChildren(this Node node)
Parameters
node
Node