Godot Version
4.2.1 .NET
Question
For my GUI I have a (relatively) deep hierarchy, to mimic the desired layout. Consider the following example:
Main
|-- ...
|-- GUI
|-- SidePanel
|-- VBoxContainer
|-- DropDownContainerButton1
|-- MarginContainer
|-- GridContainer
|-- Control1
|-- Control2
|-- Control3
|-- Control4
|-- DropDownContainerButton2
|-- MarginContainer
|-- GridContainer
|-- Control5
|-- Control6
|-- Control7
|-- Control8
|-- ...
Now, if I need to do something with, say, Control2
or Control7
, I have to access them by path:
# GUI script
var node1 = $PanelContainer/VBoxContainer/DropDownContainerButton1/MarginContainer/GridContainer/Control2
node1.connect("signal", self, do_something)
var node2 = $PanelContainer/VBoxContainer/DropDownContainerButton2/MarginContainer/GridContainer/Control7
node2.connect("signal", self, do_something)
There are a few problems with that:
- Whenever path changes (happens somewhat often during development), I have to update paths to different controls, even if each path is only referenced once.
- It’s just tiresome and error prone to type this, even with completion (available only in GDScript)
I am wondering if there’s a better way of doing that? Maybe referencing a node not by its path, but by some other means. Or maybe shortening the paths. OR something completely different.
I came up with the solution when I add a metadata, say, “NodeId” to the desired controls (in the example above these are ControlN
nodes) and I provide a function GetNodeByMetaId(id: string)
which iterates through all children of GUI
node (root node for app’s GUI), searches the ones that have metadata with the name "NodeId"
and selecting a node that matches the id
parameter.
But this looks more like a hackaround than a solution to this problem. Aany other things I may try?
Thanks.