Random beginner-question: Signal when Label is empty or not empty

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

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():?

:bust_in_silhouette: Reply From: godot.ma.desive.logo

Do you have Label node called linetext in Scene?
Try

if $linetext.text != "":
  print("IT WORKS!!!")

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!")

pferft | 2020-12-02 11:33

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

godot.ma.desive.logo | 2020-12-02 11:42

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

pferft | 2020-12-02 12:00

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.

godot.ma.desive.logo | 2020-12-02 13:00

Yes, the_one_label 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?

pferft | 2020-12-02 13:14

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

godot.ma.desive.logo | 2020-12-02 13:59

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!

pferft | 2020-12-02 14:17