0 votes

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?


in Engine by (40 points)
retagged by

1 Answer

+2 votes
Best answer

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

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
by (1,310 points)
selected by

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):`

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

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

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!

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.