Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some RichTextLabel getter methods only return a correct value on the next frame #36381

Open
Tracked by #39144
golddotasksquestions opened this issue Feb 20, 2020 · 8 comments

Comments

@golddotasksquestions
Copy link

Godot version: 3.2.stable

OS/device including version: Win 64

Issue description:
get_total_character_count() returns 0 in a RichTextLabel with text.

Steps to reproduce:

  1. Add RichTextLabel node
  2. add text in textbox
  3. add script:
extends RichTextLabel

func _ready():
	print( get_total_character_count() )

This should work according to the docs:
https://docs.godotengine.org/en/3.2/classes/class_richtextlabel.html#class-richtextlabel-method-get-total-character-count
possibly related: #874

@Calinou
Copy link
Member

Calinou commented Feb 20, 2020

Duplicate of #12884. That said, we should update the documentation to mention the character count functions won't work in a ready callback unless they're called using call_deferred().

@MJacred
Copy link
Contributor

MJacred commented Feb 22, 2020

then we'll have the same issue with RichTextLabel::set_percent_visible, as that one calls get_total_character_count().

I can make a PR, should I add a note to both?

@Calinou Calinou changed the title RichTextLabel: get_total_character_count() returns 0 Some RichTextLabel getter methods only return a correct value on the next frame Feb 25, 2020
@dalexeev
Copy link
Member

dalexeev commented Apr 9, 2020

I wanted to report the same problem, but found this issue.

extends Node

func _ready():
    var rtl = $RichTextLabel
    rtl.bbcode_text = "[color=red]hello[/color]\n"
    
    print("content_height: ", rtl.get_content_height())
    print("line_count: ", rtl.get_line_count())
    print("total_character_count: ", rtl.get_total_character_count())
    
    yield(get_tree(), "idle_frame")
    print("---")
    
    print("content_height: ", rtl.get_content_height())
    print("line_count: ", rtl.get_line_count())
    print("total_character_count: ", rtl.get_total_character_count())
    
    # content_height: 796355904  <-- An uninitialized variable?
    # line_count: 2
    # total_character_count: 0
    # ---
    # content_height: 30
    # line_count: 2
    # total_character_count: 5

I think this should be considered a bug. Information should be updated immediately, in the text/bbcode_text setter. Otherwise, you have to make the processing function asynchronous (which can cause unnecessary inconvenience.).

@Calinou
Copy link
Member

Calinou commented Apr 9, 2020

@dalexeev I'm not sure if it's even possible to make those methods synchronous in the first place. Otherwise, they probably would be synchronous already 🙂

@dalexeev
Copy link
Member

dalexeev commented Apr 9, 2020

@Calinou I meant a user-defined function (_ready or the like).

func write(text):
    rtl.bbcode_text += text
    yield(get_tree(), "idle_frame") # hack!
    tmr.start()
    while rtl.visible_characters < rtl.get_total_character_count():
        yield(tmr, "timeout")
        rtl.visible_characters += 1
    tmr.stop()

(My function is already asynchronous because the loop contains yield, but there may be different situations when the synchronous function is much more convenient.)

@Darenn
Copy link
Contributor

Darenn commented Apr 15, 2020

Hi,
The issue #37912 that I opened was a duplicate of this one.
I've listed some workaround if you want to check.
Here's a minimal project to reproduce if it can be helpful.
LabelBug.zip

@git2013vb
Copy link

Hi I have similar problem with GetContentHeight()

RichTextLabel.zip

@Yesyoor
Copy link

Yesyoor commented May 24, 2020

I encountered a problem when setting the text via gdscript. For any message set the line_count will always return 1, no matter how many lines are shown/drawn. visible_line_count returns the correct values, increasing while revealing them via a timeout signal.
Godot v 3.2.1 on macOS Catalina.

I wanted to set scroll_to_line to scroll down while revealing the lines, but always hit an index out of bounds error due to the incorrect lines.size() value. Found a workaround using a deferred call to the scroll bar. Still probably should be fixed though as invisible_line count works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants