lineedit box cant find when assigning to variable

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

hello,

I am trying to get the text from a line edit node, and i tried this:

 onready	var sendSound : LineEdit = $"/root/Server/HBoxContainer/VBoxContainer/HBoxContainer/VBoxContainer/InputSound"

doesnt seem to work, i tried also:

onready var sendSound : LineEdit = $HBoxContainer/VBoxContainer/HBoxContainer/VBoxContainer/InputSound

which this gave me through its auto-populate or auto-correct, but that doesnt work… WHAT AM I DOING WRONG!!!

:bust_in_silhouette: Reply From: deaton64

Hello,

If you want the text when someone types and presses enter, add a signal on the LineEdit node to do this.
or are you trying to do something else?

HEy deaton64, yes when someone enters text in the LineEdit, then presses a button, I want to capture that text into a variable, is there an easy way? you said add a signal?

siten0308 | 2021-06-24 22:08

Hi,
Usually, you enter text, press enter and that’s when the text is read.
Click on your LineEdit node in the Scene. Then in the Inspector click on the Node tab.
Double click on either text_changed or the text_entered signal and connect those to your code.
Then you end up with something like this to get the text:

func _on_LineEdit_text_entered(new_text: String) -> void:
print(new_text)

Or, if you want to grab the contents of the text box when someone hits a button, you can: print($LineEdit.text)

deaton64 | 2021-06-25 07:50

cool thanks, for some reason, i am not sure why, i tried yours, and still didn’t work… then I found this when i right-click the child node: Copy Node Path

and added the .text like yours, and it worked… weird weird thing is… i did this before, were i copied the path like that, and tried it… and still didn’t work, not sure what is the difference between yesterday and today… but its working… big thank you :slight_smile:

so it worked as such, I right click, copy node path, then added .text

$HBoxContainer/VBoxContainer/HBoxContainer/VBoxContainer/InputSound.text

siten0308 | 2021-06-25 18:15

Glad it works.
Be careful if you move the line edit object in the scene tree, the node path will change, but you code will point to the object in it’s original place.

You can get round this by searching for the node and assigning it to a variable and using that to reference the object. A bit like you were trying in the original post.

onready var sendSound :LineEdit = find_node("InputSound")

then you can access the text like this: print(sendSound.text)
Makes your code neater as well.

deaton64 | 2021-06-25 23:15