Exporting a child property

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

Hi,

Coming from Qt, in QML there is this cool feature where you can create an alias to a child’s property to expose it as a property from the parent.

How can you do this in GDscript?

I tried to export a var with setget so I could update the child property in a set function but this is not working properly, well it’s working in the editor but not at runtime where the value is not updated.

Just to be sure, you don’t mean export as the godot script keyword export?
Also I’m not familair with Qt, so what do you mean with:
create an alias to a child’s property to expose it as a property from the parent.

Could you describe it in a generic way?

coffeeDragon | 2018-04-04 11:19

Yes I do mean export keyword in GDScript.

My case in simple. I created a scene for a custom button, with the following structure:

  • TextureButton (root): button background

  • Label (child): button text

What I want to do is to expose the Label’s text property so other scenes using the custom button can change the text easily.

BraindeadBZH | 2018-04-04 11:35

:bust_in_silhouette: Reply From: BraindeadBZH

I have a solution, here the code for my custom button, still I wish there would be a nicer way:

tool
extends TextureButton

export(String) var button_text = "BUTTON" setget set_button_text

func _ready():
  $text.text = button_text # Compensate for not being able to set the value before in the tree

func set_button_text(new_text):
  button_text = new_text
  if !is_inside_tree(): return # Avoid crash when value is set before the node is added to the tree
  $text.text = new_text
:bust_in_silhouette: Reply From: rolevax

There is a simple way without adding code in _ready

export(String) var button_text = "BUTTON" setget set_button_text

func set_button_text(new_text):
    button_text = new_text
    if not is_inside_tree():
        yield(self, "ready")
    $text.text = new_text