0 votes

I thought this would work:

tool
extends Label

func set_text(t):
    print(t)

But whenever selecting this node in the scene tree and typing text into the Text box underneath Label in the inspector, this function does not get called. "Set text" appears in the Output panel. How do we get a function called whenever label text is modified in the Inspector?

asked Sep 10, 2019 in Engine by goshot (61 points)

2 Answers

0 votes
Best answer

Figured out how:

func _set(k, v):
    if "text" == k:
        # Do something
answered Sep 14, 2019 by goshot (61 points)
0 votes

I don't think it's possible to react to text being changed. What do you need this for?
You could maybe workaround this by listening for label redraws:

var previous_text = ""

func _draw():
    if previous_text != text:
        print("Text changed: ", text)

If you want to trigger something exactly when the text changes, you'll have to do it differently with a custom property or function, instead of relying directly on text.

answered Sep 11, 2019 by Zylann (21,823 points)

See the answer I posted - _set gets called for text like for other properties.

What is your code doing in _set? If you access child nodes, it's potentially going to fail, similarly to what happens in https://godotengine.org/qa/50467/invalid-index-color-base-null-instance-with-value-type-color

Your point is valid that if something calls set_text on the extended Label node before it is in the scene tree, code that depends on being in the scene tree will fail. That could probably be protected with an is_inside_tree check. My code works with resources, so does not have this issue.

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.