text.erase() doesn't work

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

I have a TextEdit with a text member. I want to delete last character of this member using text.erase() but it doesn’t work.

here is my code:

    var bs = text.length()
    text.erase(bs-1,bs)

I used another variable to do so. Firstly I put TextEdit.text in another variable then I removed the last character from it using variable.erase() and I put the variable back into TextEdit.text. now in this case it workes.

here is my code:

    var bs = text.length()
    var thistext = text
    thistext.erase(bs-1,bs)
    text = thistext

what is the problem?

:bust_in_silhouette: Reply From: jgodfrey

Something like this should work:

func _ready():
	var text = $TextEdit.text        # grab the text
	text.erase(text.length() - 1, 1) # remove the last char
	$TextEdit.text = text            # update the text in the control

Errr… I feel like the question has been edited since I provided this answer. Now, the answer is presented as part of the question, and the question isn’t quite the same - which makes reading this entire thread quite confusing…

jgodfrey | 2020-03-20 14:30

Works flawlessly, thank you very much.

Icewheel12 | 2020-04-29 17:45

:bust_in_silhouette: Reply From: sepideh

What I mean is why I am not able to use text.erase() on TextEdit.text without using another variable? what prevents text.erase() function to work on TextEdit.text?
in this code:

var bs = text.length()
text.erase(bs-1,bs)

It does not get any errors, it just won’t work. but why not?

You can either edit your question (by clicking the three little dots in the lower right of your question and selecting “Edit”) or comment on answers of others (by clicking on “Comment” in the lower left of an answer). There’s no need to bloat the answer region of your question with simple remarks and subsequent information. :slight_smile:

njamster | 2020-03-20 14:14

:bust_in_silhouette: Reply From: njamster

If you access the text-property of a TextEdit, you’re getting a copy of its current value, not a dynamic reference to it. It’s easy to see by running this script:

func _ready():
	var value = $TextEdit.text
	print(value)
	$TextEdit.text = "Blau"
	print(value)

So in your first example, you’re erasing the last character in a copy of the displayed value and then do nothing more with it. In the second example you’re getting the copy, erase from it and then overwrite the current value with the modified copy.

This is wrong. You can call erase on the property but you can’t access the results.

siakc | 2020-03-20 21:37

Furthermore it seems that your example proves nothing! Of course when you don’t change “value” its value its not changed. Besides if $TextEdit.text returns a copy of the value, how $TextEdit.text = “Blau” changes the property value?

siakc | 2020-03-21 19:14

This is wrong. You can call erase on the property but you can’t access the results.

Where exactly am I wrong? I never wrote, that one cannot useerase on the property. However, erase is an in-place operation, i.e. it doesn’t return anything. So if you can access a string, you can also access the result of erase on it - always! The confusion stems from the fact that $TextEdit.text returns a string and erase is called on this string instead of the original value. So the original value won’t change - obviously.

njamster | 2020-03-21 10:00

Sorry I didn’t mean you are wrong. I meant the behavior is wrong. erase should either act on the reference or return the result.

siakc | 2020-03-21 19:14

I meant the behavior is wrong.

That’s your opinion. I’d consider it good (and common) practice.

erase should either act on the reference or return the result

It acts on the string you pass it, nothing more, nothing less.

Of course when you don’t change “value” its value its not changed.

If the variable value would contain a reference to $TextEditor.text, than changing $TextEdit.text would also change the variable of value. As this is not the case (it contains a value) it doesn’t, yes. That’s my point.

Besides if $TextEdit.text returns a copy of the value, how $TextEdit.text = “Blau” changes the property value?

Because the latter is equivalent to calling the setter-method set_text(value) and the former is equivalent to calling the getter-method get_text(value).

njamster | 2020-03-21 20:30

I disagree that it is a common practice and definitely not a good one. The getter should either return a read-only object which no in-place method can be called upon or erase should return the changed string. If non of these options are met, it is simply wrong. It is wrong because erase (or other such methods) are acting on a variable you don’t have a handle of. I conclude it is a GDscript design flaw.

siakc | 2020-03-22 16:57