0 votes

Hi everyone,

I have a Label set up for entering numbers and a comma, and all works fine.
What I'm trying to do is to have a space inserted automatically between the 3rd and 4th digit. Everything in the code below works until (and including) print:

...
var preCommaCharacterCount = comma

if preCommaCharacterCount == 0:
    return false

var preComma
if preCommaCharacterCount == 3:
    print ("preCommaCaracters are 3 !!!!!!!!!!!!!!!!!!!")
    preComma.insert(' ')

But preComma.insert(' ') leads to the error: Invalid call. Nonexistent function 'insert' in base 'Nil'.
Is there any simple way to have that "Space" (or anything really) be inserted there?

Godot version 3.2.3
in Engine by (365 points)

1 Answer

0 votes
Best answer

If I understand correctly, you can use the + operator to insert strings onto other strings like so:

preComma += ' '

For example, the following code will print "hello there!":

var sentence = 'hello'
sentence += ' '
sentence += 'there'
sentence += '!'
print(sentence)
by (85 points)
selected by

Feeling somewhat stupid right now...

    var ggg = ""
    ggg = ggg.insert(2, "hot")
    ggg = ggg.insert(3, "cold")
    text = ggg # didn't do "label.text" - didn't work, "text" solo works...

This replaces, as mentioned before.
Now I tried

    var ggg = ""
    ggg = ggg.text.insert(2, "hot")
    ggg = ggg.text.insert(3, "cold")
    text = ggg.text

But that crashes...

Oops, sorry in one of my projects I am referencing a label from another script, but in your case you would use "text" (like you are currently) rather than "label.text" like I was saying

What I meant to say was that text is a string itself so you can insert other strings into it:

text = text.insert(1, 'thing')

and add onto the end of the text like so:

text += 'thing'

The label variable that I was talking about before is for accessing a label from another script. If you were accessing a label from another script, you would have to create a variable that holds the label node and access the text of the label node from the label variable's .text property, but you are not accessing the label from another script, you're programming from a script that is connected directly to the node. So where I wrote label.text in previous comments, I should have just said "text" instead of "label.text".

So strings, like ggg in your example above, do not have a .text property, sorry if you were confused by me writing label.text, know that that was my mistake and I was thinking of a different case. There are no .text properties in any of the variables you are working with, as far as I know.

That's why your second code example isn't working, there is no .text property of a string

Oh no worries! I think my knowledge about coding by now reaches about exactly that far (having to "connect to another Node" or not, in such a case, theoretically...)!

But as my first code example replaces the Label's content rather adding to it... I just don't know where to put the text.insert. You say my string ggg doesn't have a text property? So I can't insert 'thing' that way?

Every string has the .insert function attached to it, so you can indeed use insert on ggg by simply doing:

ggg = ggg.insert(1, 'thing')

And because text is also a string, you can access insert from text too:

text = text.insert(1, 'thing')

For your first code example, you would change it to this to make ggg be added onto the end of text:

var ggg = ""
ggg = ggg.insert(2, "hot")
ggg = ggg.insert(3, "cold")
text += ggg # it is now text += ggg rather than text = ggg

or if you wanted to, for example, insert ggg after the first character in text, you would do this:

var ggg = ""
ggg = ggg.insert(2, "hot")
ggg = ggg.insert(3, "cold")
text = text.insert(1, ggg)

This works and teaches me about the logic behind things (need to understand where to put my ggg's...).

What I observe it that the 2 in the second line does not really have an impact, does it?

    var ggg = ""
    ggg = ggg.insert(2, "hot")
    text = text.insert(1, ggg)

The 1 in the third line defines the insert-position.

For some reason no more input is possible after inserting "hot" in my project now, but I guess that's something less obvious and due to its structure somehow - I'll try to find a way around.
Anyway, thanks again 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.