0 votes

Hi everyone,

with my number-pad I can put digits in my RichTextLabel as well as one single comma wherever I want it, and I can erase single positions for corrections.
The numbers are being put into another Label simultaneously as well, but not the comma, because I want to use the entered numbers as full integers for backend calculations. So the comma-button simply is not connected to that Label at all. That comma is basically just decoration, it could be any symbol really...

Naturally, with the comma not appearing in my "backend"-Label, erasing it in the RichTextLabel will erase the next digit. So what I'm trying to achieve is my backend-Label to do nothing when the comma is being erased in the RichTextLabel.

I connected my erase-button with the RichTextLabel

var linetext = get_node(rich_text_label).text
linetext.erase(linetext.length()-1,1)
get_node(rich_text_label).text = linetext

and the backend-Label

var linetext2 = get_node(back_end_numbers).text
linetext2.erase(linetext2.length()-1,1)
get_node(back_end_numbers).text = linetext2

and I can work with comma and no-comma like this:

var comma = linetext.find(",")
if comma >= 1:
...
if comma <= 0:
...

I was trying to have the backend-Label not react with all kinds of variables back and forth, but if I could at least make it not erase a number along with the comma, I couldn't make it continue erasing afterwards again, and I'm at a loss here.

What could be a good way to "pause when the comma is being erased"? I thought that a "thecommaisbeingerased"-signal from within the RichTextLabel could do the trick (--> if the signal fires: return, otherwise: do your erasing...).

How could I set up such a Signal? Or is there another simple way to achieve this?
Any help is much appreciated.

EDIT:
Just a thought: it would be neat if the entered numbers could be copied into the backend-label when confirming (there's a confirm button), just without the comma. Maybe that would be an easier way... how could that be done?

Godot version 3.2.3
in Engine by (331 points)
edited by

1 Answer

0 votes
Best answer

Yeah, I couldn't really get it until your Edit :). This is definitetly easier with string operators like erase, lstrip and rstrip.
it should go like this :

var formattedtext = linetext.rstrip(",")

Just check String in help documentation, there are more functions that allow to reshape strings like this. Especially if You want to recognize trails of characters inbetween commas.

However I don't really get it, why didn't You use your comma seeking module ( string.find) in if statement just above and use use one source text instead of two texts :

var linetext = getnode(richtextlabel).text
if linetext.find(",") == linetext.length()-1 :
.....get
node(backendnumbers).text = linetext
......linetext.erase(linetext.length()-1,1)
......getnode(richtextlabel).text = linetext
else:
.....linetext.erase(linetext.length()-1,1)
.....get
node(backendnumbers).text = linetext
......getnode(richtext_label).text = linetext

So backlabel would simply erase last character unless last character in mainlabel is a comma

by (1,121 points)
selected by

Yes, my first thought with a Signal, well, I guess it's a beginner's first thought all too often. Signals are tempting! ; )

That code is a big step, thanks! (I'll definitely have to look into the rstrip and all the other string functions!) I thought about something like this theoretically but couldn't come up with it practically yet...

There's only one issue now: the moment I erase something after inserting a comma, the comma appears in backendnumbers as well (it even appears when I instantly erase the comma again!). It disappears the moment I erase the number left of it. So I assume I need to place .rstrip(",") in the right spot... where could that be?

Ah right, I didn't think of that :)
let's make it easier with rstrip :

when you feed text to labels :
linetext2 = getnode(richtextlabel).text.rstrip(",")
get
node(backendlabel).text = linetext2

when you erase :
var linetext = getnode(richtextlabel).text
var linetextmain = linetext.erase(linetext.length()-1,1)
var linetextback = linetextmain.rstrip(",")
get
node(richtextlabel).text = linetextmain
get_node(backendlabel).text = linetextback

This way no if statement is required. Only main label handles erasing and back label simply updates accordingly. When only comma is erased in main label, the text in backlabel will stay the same.

So close, I can feel it! Tapping "erase" gives me: "Invalid call. Nonexistent function 'rstrip' in base 'Nil'." in the line var linetextback = linetextmain.rstrip(","). And I don't understand...

Now it looks like just a typo :)
error means that linetextmain is null. There must be some misspelling one line above, where linetextmain var was defined

var linetext = get_node(rich_text_label).text
var linetext2 = get_node(back_end_numbers).text
linetext2 = get_node(rich_text_label).text.rstrip(",")
get_node(back_end_numbers).text = linetext2

var linetextmain = linetext.erase(linetext.length()-1,1)
var linetextback = linetextmain.rstrip(",")
get_node(rich_text_label).text = linetextmain
get_node(back_end_numbers).text = linetextback

Can't find any typo! Am I blind?

Ah I'm sorry,I forgot string.erase returns void. It will go even shorter :

Erasing part

var linetext = get-node(rishtextlabel).text
linetext.erase(linetext.length()-1,1)
getnode(richtextlabel).text = linetext
get
node(backendnumbers).text = linetext.rstrip(",")

Don't be sorry! I'm so glad you're helping me with this.

This works - as long as I enter only one position after the comma. As soon as I have 2 post-comma digits, erasing the last one again makes a comma appear in the backendnumbers label... which is strange, isn't it? Shouldn't the last line make generally no comma appearing in the backendnumbers label? Hmm...

Hehe it is not so easy, good :)

So I have just learnt, that rstrip sucks since any characters from specified site ( left or right ) will block the stripping :)

So the new thing is replace() :P :

last line :
getnode(backendlabel).text = linetext.replace(",","")

syntax looks ridicolous :). First argument is character to be replaced, another is empty string :)

...aaaand we have a winner! Thanks a million!

Too bad rstrip sucks *; )* However, the string class seems to be full of little wonders. Especially that PoolStringArray split is interesting... if I may I might come back to you with a following question in this context of my versatile label.

So I'm proceeding on my way of distributing text.

I added two RichTextLabels: PreCommaDigits and PostCommaDigits (and I'd like the numbers before the comma to appear in the first one and those behind the comma in the other one). They're connected to my entry-Label via

export (NodePath) var pre_comma_digits
export (NodePath) var post_comma_digits

I thought that PoolStringArray split is what I'm looking for and indeed, this works:

var leftandrightofcomma = text.split(",", true, 1)

get_node(pre_comma_digits).text = leftandrightofcomma[0]
get_node(post_comma_digits).text = leftandrightofcomma[1]

Well, at least [0] does. [1] crashes on me with Invalid get index '1' (on base: 'PoolStringArray').. I thought that this is according to the example in the docs... do you have any idea what I'm missing here?

Well it seems it is another imperfect built-in function. Middle argument doesn't work, it doesn't allow empty strings and because of that, when your text ends with comma, resulting split array has only one string. So it has to be fixed with additional statement :

var leftandrightofcomma = text.split(",", true, 1)
get_node(pre_comma_digits).text = leftandrightofcomma[0]
if leftandrightofcomma.size() > 1:
        get_node(post_comma_digits).text = leftandrightofcomma[1]
else:
        get_node(post_comma_digits).text = ""

I hope You are planning on having only one maximum comma in main label ? :P And that main text will never start with comma ? :P

I hope You are planning on having only one maximum comma in main
label ? :P And that main text will never start with comma ? :P

--> exactly this! I'm always impressed how pros like you just see such potential issues I'd only realize when things break down... ; )

I've been trying around with some if-clauses as well but couldn't come up with your idea - which works perfectly!

You're the hero of the week, thanks so much for your help!

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 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 webmaster@godotengine.org with your username.