0 votes

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.

class_name MyWidget
extends PopupPanel

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

func _ready():

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

func set_some_value(value):
    some_value = value
# ... even more setters
Godot version 3.4.2
in Engine by (30 points)

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.