How to clear text on key input?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lindsay4047
:warning: Old Version Published before Godot 3 was released.

I’m sure this should be easy to do, but I can’t get my code to work. I am using the LineEdit node and want the text you type into it to clear when you press enter.

Also, I am using a signal that emits when enter is pressed. It carries over the text you input. Would that cause problems if they are not coded under the same function?

Thanks!

:bust_in_silhouette: Reply From: ericdl

LineEdit has a text_entered signal that will fire when it has focus and Enter is pressed. Connect that signal to a function and it should be as easy as calling clear() on the LineEdit node. You should be able to write your other logic in there as well.

Just tested and it works:

extends LineEdit

var label_node

func _ready():
	label_node = self.get_parent().get_node("RichTextLabel")
	label_node.set_scroll_follow(true)

func _on_LineEdit_text_entered( text ):
	if text.length() > 0:
		
		# append the text to RichTextLabel
		# option: label_node.newline() instead of "\n"
		label_node.add_text(text + "\n")

		# clear LineEdit
		self.clear()

I tried your code, but nothing seems to be happening. I used the text_entered signal that’s built in.

The farthest I’ve gotten is getting the text to append to the RichTextLabel with this code:

extends RichTextLabel

func _on_LineEdit_text_entered(playerInput):`
   newline()
   add_text(">")
   append_bbcode(playerInput)

But I still can’t figure out how to get that stubborn text in LineEdit to clear.

Lindsay4047 | 2016-06-25 17:30

Are there any error messages in the debugger when you run your game?

If you are calling the LineEdit’s clear() method from within the RichTextLabel, did you get a reference to the LineEdit node first by using get_node()?

Not sure if this will help, but here’s a link to a small test project: https://drive.google.com/file/d/0BwnfZQAEnciAZmtfaUZ6cjFvSUk/view?usp=sharing

ericdl | 2016-06-25 20:48

Oh I see the issue now. In your example, you used two connections, while I only used one. That was the only thing causing it to not work. Thanks a lot for your help!

Lindsay4047 | 2016-06-26 18:20