0 votes

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")
Godot version 3.4.2
in Engine by (93 points)

you can write their names short

1 Answer

+1 vote
Best answer

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....

by (8,097 points)
selected by

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!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.