bbcode offset text position

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

Hi everyone,

I built a number-pad so the user can fill numbers in a RichTextLabel by pressing the numbers.
My bbcode makes the first digit stand apart a bit when reaching 4 digits by offset (no blank charakter!), like this: 123 → 1 234
All in all there will be a maximum of six digits possible to being entered, and I’d like that offset-gap to always set the righmost three digits apart from the left ones, like 12 345 and 123 456.

What would I need to add to my code?

extends RichTextLabel

var templateGap = "[offset x=-10 y=0]{leftDigits}[/offset]{rightDigits}"

var fourDigitsReached = 4
var fiveDigitsReached = 5
var maxDigits = 6

var previous_text = ""


func _draw():
	if previous_text != text: # just to register that input has changed...
		if text.length() == fourDigitsReached:
			bbcode_text = templateGap.format({ "leftDigits": text[0], "rightDigits": text.substr(1)})

Until here everything works. The next lines though don’t seem to do anything:

	if text.length() == fiveDigitsReached:
		bbcode_text = templateGap.format({ "leftDigits": text[0], "rightDigits": text.substr(1)})


	if text.length() >= maxDigits:
		bbcode_text = templateGap.format({ "leftDigits": text[0], "rightDigits": text.substr(1)})

I’d assume that these text[0] need to be tweaked somehow, but I couldn’t figure it out.

:bust_in_silhouette: Reply From: pferft

Thanks to kleonc on reddit, suggesting:

const rightDigitsMaxLength = 3

...

    var leftDigitsLength = max(0, text.length() - rightDigitsMaxLength)
    if leftDigitsLength > 0:
        bbcode_text = templateGap.format({
            "leftDigits": text.substr(0, leftDigitsLength),
            "rightDigits": text.substr(leftDigitsLength),
        })

…which works like a charm.