Getters and setters as proxies for children's properties?

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

I’m getting stumped about setting children properties from getters in tools. Here’s a piece of code:

export(String) var title setget _set_title, _get_title

func _set_title(_value: String) -> void:
	title_label.text = _value

func _get_title() -> String: 
	return title_label.text

setting the title in the inspector gives you:

Invalid set index 'text' (on base: 'Nil') with value of type 'String'.

So, you do this:

func _set_title(_value: String) -> void:
    if not is_inside_tree(): yield(self, 'ready')
    title_label.text = _value

This works, but then, each time you save the scene, you get the same message.
So you do:

func _set_title(_value: String) -> void:
    if not is_inside_tree(): yield(self, 'ready')
    if not title_label: return
    title_label.text = _value

And this works, but now when running the scene, I get

resume: Resumed function '_set_title()' after yield, but class instance is gone. 

This is all very confusing, and seems like a lot of acrobatics for what boils down to accessing children’s properties from a scene that needs to be instanced a lot.

To note: everything technically works, as in, the scene runs and does what it should. But since this is supposed to be released as a plugin, the error is annoying.

:bust_in_silhouette: Reply From: xananax

After a lot of trial and error, this seems to work without problems:

tool
extends Node2D

export(String) var title setget set_title, get_title
onready var title_node = $path/to/label

func set_title(value: String) -> void:
	if not is_inside_tree(): yield(self, 'ready')
	title_node.text = value

func get_title() -> String:
	if not title_node: return ''
	return title_node.text

Downloadable project here: