Is there a more elegant way to call distant child nodes?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By YangTegap

Is there a more elegant way to call these?

var location_tab
var map_tab
var ship_tab
var crew_tab


func _ready():
	
	location_tab = get_node("VBoxContainer/ViewsContainer/LocationView")
	map_tab = get_node("VBoxContainer/ViewsContainer/MapView")
	ship_tab = get_node("VBoxContainer/ViewsContainer/ShipView")
	crew_tab = get_node("VBoxContainer/ViewsContainer/CrewView")

you can write their names short

ramazan | 2022-01-31 20:49

:bust_in_silhouette: Reply From: Inces

Ellegancy of this approach is small problem, the thing is everything will go broken if You choose to rearrange control nodes hierarchy just a little. You can avoid this introducing reference like this :

export (NodePath) onready var location tab = get_node(location_tab)

This way You will be able to manually join reference to the node using editors interface, and update it easily whenever scene hierarchy is rearranged.

As for another approach, You may not need any references at all when using observer pattern. Nodes can communicate with each other using signals emitted by Autoload, so they don’t need to know references to each other for signal connection. This approach has limits though, sometimes it is easier to just use unellegant looking long ref…

Ah, this is what I was looking for with the term “elegance.” Thank you so much! Still learning a lot, haha! This is very helpful!

YangTegap | 2022-01-31 22:11