0 votes

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.

in Engine by (21 points)

1 Answer

+1 vote
Best answer

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

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:

by (21 points)
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.