0 votes

EDIT:
Actually a "wrong thought" question.
if linetext.empty() and if not linetext.empty() work perfectly. A structural problem lead to the issue...


Hi everyone,

I managed my Label to tell me if it's empty:

if linetext.empty():
        print ("No Text in Label")

but I don't succeed in it telling me when there's actually something written in it.
I tried

if not linetext.empty():
if linetext != 0:
if not linetext == 0:
if linetext >= 1:
if text.length() >= 1:
even desperate trials like if linetext = "":

and whatnot, but nothing worked.

Any idea what I'm doing wrong and how this could be done? Shouldn't this be as simple as if linetext.empty():?

in Engine by (317 points)
edited by

1 Answer

0 votes

Do you have Label node called linetext in Scene?
Try

if $linetext.text != "":
  print("IT WORKS!!!")
by (59 points)
edited by

This leads to the error:

Invalid get index 'text' (on base: 'String').

My function looks like this right now:

func _my_erase_button():
    var linetext = get_node(the_one_label).text
    linetext.erase(linetext.length()-1,1)
    get_node(the_one_label).text = linetext

    if linetext.empty():
        print ("No Text in Label")
    if linetext.text != "":
        print ("There's actually something written in the Label!")

The problem maybe is, you hoave something like this in your code:

if $the_one_label.text.text != "":

you are doubling the .text

So you can try only:

if linetext != "":
 print("Works!")

BTW get_node(the_one_label) =$the_one_label
it is shorter and better

Removing "text" after linetext. evades the crash but it still doesn't work. I can't find any double .text anywhere. It's so weird because the if-line before works perfectly.

(For whatever reason $ leads to a crash here as well, but get_node works...)

the_one_label is what? Is it name of the Label in Scene tree?
If I tried it, I created new node in Scene tree - Label and I called it linetext. And everything worked perfectly with the code I shared with you.

Yes, theonelabel is the name of the Label. And actually your code indeed works. I'm sure the problem lies somewhere within my scenario.

My function is built right into a Button I use for erasing what other Buttons write into a Label (I created a number-pad...):

func _my_erase_button():

var linetext = get_node(the_one_label).text

linetext.erase(linetext.length()-1,1)

get_node(the_one_label).text = linetext

if linetext.empty():

print ("No Text in Label")

if not linetext.empty():

print ("There's actually something written in the Label!")

And like this everything works - tapping (number-)buttons to add numbers and tapping the erase-button to erase one position. What I try to do is to make this erase-button disabled when the Label is empty so there's actually nothing to erase. And this works fine by simply adding disabled = true under if linetext.empty():.

What does not work, however, is getting the button back to work again as soon as something is written into the label by adding disabled = false under if not linetext.empty():. The button stays disabled... printing works, but not this.

What could the little detail be I'm missing here?

if linetext != "":
  $Button.set_disabled(false)

This all works, thank you!

I realize that the problem in my case actually lies somweher in the structure...

It all seems to boil down to how I can have my Label emit a Signal when changing from "nothing written" to "something written". With such a signal I then can "un-disable" the button.
So I guess I'll have to lable this question as "wrong thought" and open a new one with addressing the core problem... but 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.