Best way to implement tool subscenes

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

I often have the case where I instantiate child scenes of little control widgets in the editor. I’m looking for the most convenient way of defining and handling these child scenes.

My current workflow is the following:

  • Design the node tree of the widget in the editor
  • Attach a tool script to the widget’s root node
  • The script exposes relevant layout variables as exports (typically these translate into setting properties of child nodes). They all need setters, otherwise the layout won’t update in the editor when I change it. To avoid writing extensive setters for a dozen variables that each need to check whether the children exist already in the tree (see also Best way to tell if node is loaded when exporting variables?), I transfer the layout logic into a generic setup function that is called in every setter AND the _ready function.

Question: Is this a recommended, godot-ish design pattern or should I go otherwise? I.e. it’s still a nuisance to write all those setter-functions that essentially all do the same. I was trying to generalize this but failed so far to create setter-functions dynamically. I have the strong feeling I’m missing something crucial here in the way Godot works.

tool
class_name MyWidget
extends PopupPanel

export(float, 0.0, 1.0) var some_value setget set_some_value
# ... even more values


func _ready():
    setup()

func setup():
    if is_inside_tree():
        # apply exports to child nodes

func set_some_value(value):
    some_value = value
    setup()
# ... even more setters