Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | nimh |
As far as I can tell, the get_visible_line_count()
method of RichTextLabel only counts autowraps, not actual newlines in your text. Possibly it’s even weirder than that. get_line_count()
only counts newlines but not autowraps (and obviously doesn’t care if the lines are visible or not). scroll_to_line()
only counts newlines.
Here’s the thing. You have a RichTextLabel with text in it. That label can only fit so many lines of text and you need to scroll to the lines you can’t see, regardless of whether those lines exist because of newlines in the text string or autowraps. So why does scroll_to_line()
ignore autowraps, and why can’t you get the actual number of lines your text takes up in the RichTextLabel or the actual number of lines that are visible (according to the the visible_characters property)? Actually you can get the total number of lines, if you know your font’s height, since get_content_height()
does work in a useful manner. However, there is no way to scroll to an arbitrary location, so that doesn’t help.
Here’s what I’m trying to do, but can’t. Maybe I’m doing this wrong, and someone can explain how I should go about it. I want to add rich text textboxes to my game. I don’t want them to automatically scroll if there is more text than can fit in the textbox, and I also don’t want them to have a scroll bar. I want them to work like they do in all sorts of games. I want the text to fill up the textbox, and then stop until the player presses a key. Then the textbox clears and is fill up with the next bit of text.
Say for example, I have 8 lines of text (and this means it would take an 8 line tall RichTextLabel to display it all at once. It shouldn’t matter whether I add the newlines, or let the text get autowrapped, it still takes up the same 8 lines) and a RichTextLabel that is tall enough to show 3 lines at a time. I want lines 1-3 shown. When the user presses a key, lines 4-6 are shown. When the user presses a key again, lines 7 & 8 are shown.
I tried to do this in _physics_process()
, increasing visible_characters until get_visible_line_count()
is larger than the number of lines my RichTextLabel can show (3 in my example). As long as I only use autowrapped text, this works. Then, when a button is pressed, I try to scroll the RichTextLabel to the next line (4 in my example) with scroll_to_line()
. This causes an error, since get_visible_line_count()
doesn’t count newlines, and scroll_to_line()
only counts newlines, so according to it, there is only one line of text. Why can’t they both just count the actual number of lines your text takes up in the RichTextLabel. Failing that, it there was the ability to scroll to some arbitrary point, instead of a line number, I could do the math to make this work.
Now, I can obviously work around this by never having messages that take up more than 3 lines. Instead I can manually break long blocks of text into separate messages. But especially with rich text, it’s hard to know how much text you can fit in three lines. Also, if I later want to change my text, I have to reformat it into separate messages again.
I feel like I’m working around a bad design here, rather than simply going about this in the wrong way, but if there is a better way to do this, I’d love to hear it.