Counting unprinted characters in bbformatted text in RichTextLabels

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

This is a very in-the-weeds kind of question. The TLDR is that I want to know how to return the number of characters that aren’t printed (e.g. tags) in a given slice of bb_code. As far as I can see, there is no easy way to do this.

Long question: I wanted to set up typewriter effects for a RichTextLabel using inline commands. I came up with the format of :

[command name=speed param=3][/command]

Using some inelegant code, the game iterates over the RichTextLabel, increasing the visible_character property by 1 each time so text is slowly revealed. However, if a piece of RegEx picks up this command, it sweeps it off and translates it into a function. This one speeds up the text reveal three times faster. The idea is that each command shuttles off into a different function so that it can create effects in time with the text. (e.g. “A door opens [command name=sound_play param=door_sound][/command]…” would play a door sound effect a the same time the word “opens” is revealed.)

The system works well except I noticed that the effect would be offset a few character later every time. After some investigation, I discovered that issue was with this function:

commandMatch = + offset))

“Visible characters” only count the characters that are actively rendered, so that the ones in the bb_code tags don’t count. But, the bbcode_text property includes those tags. So what I need to do is offset the visible_characters by the characters used in bbcode tags, except as far as I can see there’s no easy way to do this. The only way I can think of is to make another RegEx which captures all square bracketed text to the left, but to do that, I need to know the index of last visible character on the raw bbcode, which there’s no method for and can’t be calculated without knowing how many character were used in tags… so it’s a recursive problem.

A different illustration of the issue: This is what the player would see:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

If I retrieved the visible characters and used it as an index on the raw bb_code, it would quickly desynchronize. 123 characters looks like this:

Lorem ipsum dolor sit amet, consectetur [i]adipiscing[/i] elit, sed do eiusmod [i]tempor[/i] incididunt ut [i]labore[/i] et

If I wanted an inline command to play a sfx at the end of the sentence, it would instead appear to the player like it plays in the middle of the second sentence.

I guess the only way I can do this is to keep a variable which stores the .length() of all square bracketed text and use that as my offset. I don’t know… it seems like there should be an easier, more elegant solution, especially because it would create issues when there’s bracketed text that’s supposed to be visible. Maybe I just answered my own question, but I’m interested on what the experts think.

Thanks for your time.

:bust_in_silhouette: Reply From: DaddyMonster

Ok, so you have a string like var string = "123\t123" and you want to count the not visible characters. A regular expression should do the trick hopefully:

print(len(string.replace("/[^a-z0-9-]/g", "")))

Confession: I stopped reading after the first paragraph as the tldr seemed pretty clear. Also, I didn’t test anything and, it’s regex, everyone gets the first attempt wrong. My excuse is it’s Friday evening. :slight_smile:

Thank you! An implementation of this ended up being the solution I needed.

nationality | 2022-05-20 21:34

Delighted to hear it!

DaddyMonster | 2022-05-21 00:36