if label.text = "something": doesnt work

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

hi.
im messing a bit around with the gd script and im trying to do something…

i want that if a label has the text hi, an other label writes also hi. i tried to do it like so:

func _process(delta):

if $Labelperson.text = “hi”:
$LabelAI.text = “hi”
pass

but this doesnt work.
Has someone an idea to solve this?
the error is “Unexepected assign”

(i dont usually speak english,so im sorry for any
spelling mistakes)

:bust_in_silhouette: Reply From: antoniodev

if $Labelperson.text == “hi”:
$LabelAI.text = “hi”

you put = which means it changes, and == means is it equal to

:bust_in_silhouette: Reply From: Help me please

= is used to assign value to a variable or to a property in inspector so you can use

$label.text = "apple"

To assign value to text property of label.


Whereas to compare values you have to use ==. And hence:

if $label.text == "apple":
    print("labels text is apple")
:bust_in_silhouette: Reply From: theMX89

so, now i got another problem…
if i want to ask the question to the “AI”, i can click a button or press enter.
but the if statement just gets called if you press the button, not if you you press enter.

this is my code:

func _process(_delta):
if Input.is_action_just_pressed(“enter”):
$Labelperson.text = $TextEdit.text
$TextEdit.text = “”

if $Labelperson.text == "apple":
	$LabelAI.text = "ok..."
pass

func _on_Button_pressed():
$Labelperson.text = $TextEdit.text
$TextEdit.text = “”

pass 
:bust_in_silhouette: Reply From: Help me please

Your code seems to be right. There might be some other errors. So try adding both enter keys in the input map for “enter” event. Cross check spelling of enter in both from input event and from your code. And remember “apple” and “Apple” are both different strings.

If still it don’t work then add some print() like "enter key was pressed" , "if statement worked" etc. to check which part of code did not worked. And then act accordingly.