0 votes

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.

Godot version 3.3
in Engine by (438 points)
edited by

1 Answer

0 votes
Best answer

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.

by (438 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.