I can't get a RichTextLabel to update to reflect updated text. Ideas?

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

I’ve tried update() in various places, and I’ve confirmed via the console that both update() is called and that the changes I want to see in the RichTextLabel are happening in the code, but I just can’t get the label to update visually.

RichTextLabel doesn’t seem to have a method other than update() for this purpose. I’ve been to the Godot discord for help, and we couldn’t determine why updating the label’s text field(s) wouldn’t cause the thing to simply update visually as well, and as such the solutions we tried were both indirect and ineffective.

The code to update the label’s text does run once - during initiation, in the ready() method, which calls a method I created which simply calls clear() and adds new text via add_text(). Calling that same update method outside the does nothing, however, so my assumption is something somewhere is caching the drawn label and I’m failing to trigger a redraw. That or something unrelated, so I’ve included the project here:

Edit: And thank you to anyone who attempts to help with this problem, whether if it’s very obvious or one that stumps us all.

I’m not sure if this answers your question, but all you need is to update/change your text, right?
Well then you could simply alter the text like this:

$RichTextLabel.text = "Updated Text"

Also, I never even knew there was an update() function for RichTextLabels. I’ve always used this method.

Another method is to use the set_text() function. Like the function above, it will replace whatever was once on the label when you call it.

$RichTextLabel.set_text("Updated Text")

TheJokingJack | 2021-02-15 20:50

I opened your project and added this to the top of the Stats.gd script:

func _input(event):
	if event.is_action_pressed("ui_accept"):
		var c_scene := get_tree().current_scene
		var stat_panel: RichTextLabel = c_scene.find_node("stat_panel")
		assert(stat_panel)
		stat_panel.add_text("!")

Every time I hit Enter, it adds an exclamation mark as expected.
Tested in 3.2.3 and 3.2.4 RC2.

I searched your code, and didn’t see any text = ... or add_text(...) in any scripts other than StatPanel.gd, so I don’t know where or how you were attempting to change the text. add_text() is working perfectly fine for me.

Error7Studios | 2021-02-17 11:09

I used your comment to help me work toward what was wrong - i was trying to use methods and objects and so on that didn’t exist yet; programming as if everything simply poofed into existence simultaneously

Daimoth | 2022-04-30 20:33

:bust_in_silhouette: Reply From: Daimoth

The reason my code wasn’t working is because I was trying to use objects that weren’t initialized yet.