|
|
|
|
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
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.
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
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?
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(“,”)
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
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() :
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.
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 ? And that main text will never start with comma ?
I hope You are planning on having only one maximum comma in main
label ? And that main text will never start with comma ?
→ 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