0 votes

I thought this would work:

extends Label

func set_text(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?

in Engine by (69 points)

2 Answers

0 votes
Best answer

Figured out how:

func _set(k, v):
    if "text" == k:
        # Do something
by (69 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.

by (29,090 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 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.