Random beginner-question: signal that a certain symbol is erased

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

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 “the_comma_is_being_erased”-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?

:bust_in_silhouette: Reply From: Inces

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 = get_node(rich_text_label).text
if linetext.find(“,”) == linetext.length()-1 :
…get_node(back_end_numbers).text = linetext
…linetext.erase(linetext.length()-1,1)
…get_node(rich_text_label).text = linetext
else:
…linetext.erase(linetext.length()-1,1)
…get_node(back_end_numbers).text = linetext
…get_node(rich_text_label).text = linetext

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

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 back_end_numbers 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?

pferft | 2021-02-24 11:29

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

when you feed text to labels :
linetext2 = get_node(richtextlabel).text.rstrip(“,”)
get_node(backendlabel).text = linetext2

when you erase :
var linetext = get_node(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.

Inces | 2021-02-24 11:49

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…

pferft | 2021-02-24 14:15

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

Inces | 2021-02-24 14:47

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?

pferft | 2021-02-24 15:48

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)
get_node(rich_text_label).text = linetext
get_node(back_end_numbers).text = linetext.rstrip(“,”)

Inces | 2021-02-24 16:37

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 back_end_numbers label… which is strange, isn’t it? Shouldn’t the last line make generally no comma appearing in the back_end_numbers label? Hmm…

pferft | 2021-02-24 17:28

Hehe it is not so easy, good :slight_smile:

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

So the new thing is replace() :stuck_out_tongue: :

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

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

Inces | 2021-02-24 18:55

…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.

pferft | 2021-02-24 19:06

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?

pferft | 2021-02-25 13:00

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 ? :stuck_out_tongue: And that main text will never start with comma ? :stuck_out_tongue:

Inces | 2021-02-25 16:38

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

→ 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!

pferft | 2021-02-26 10:05