How to get function called when text changed in Inspector for a tool node/script that extends Label?

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

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?

:bust_in_silhouette: Reply From: Zylann

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.

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

goshot | 2019-09-14 21:51

What is your code doing in _set? If you access child nodes, it’s potentially going to fail, similarly to what happens in Invalid set index 'color' (on base: 'null instance') with value of type 'Color' - Archive - Godot Forum

Zylann | 2019-09-14 22:31

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.

goshot | 2019-09-16 21:24

:bust_in_silhouette: Reply From: goshot

Figured out how:

func _set(k, v):
	if "text" == k:
		# Do something