Godot Version
Replace this line with your Godot version
v4.3.stable.mono.official [77dcf97d8]
Question
Ask your question here! Try to give as many details as possible
Is there any more convenient way to get Node nodes in C#
Replace this line with your Godot version
v4.3.stable.mono.official [77dcf97d8]
Ask your question here! Try to give as many details as possible
Is there any more convenient way to get Node nodes in C#
You can use a member variable with the export attribute and then set it in the editor just by dragging the desired node. C# exported properties — Godot Engine (stable) documentation in English
A major benefit is that the code still works even if you move the node to somewhere else in the scene, since the reference doesn’t depend on the path.
Here’s how I set up controls:
public partial class MyWindow : Window
{
...
private Button ClipboardButton;
...
public override void _Ready()
{
ClipboardButton = (Button)FindChild("ClipboardButton");
ClipboardButton.Connect("pressed", new Callable(this, "OnClipboardButtonPressed"));
}
private void OnClipboardButtonPressed()
{
}
}
or just make scripts on buttons and connect them to game systems, via events, delegates or signals.
I’m late, but I usually just do:
[Export]
Node3D myNode {get; set;}
and just attach a node, especially if i’m not using component-based object oriented programming.
Because this popped up again and it wasn’t mentioned yet: You can also make them scene unique nodes. Then you can access them like this:
GetNode<Button>("%Button");
Like nodes set via exported properties they also work when you change the nodes position in the tree.