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?


asked Jun 24, 2016 in Engine by Lindsay4047 (39 points)
retagged Jun 25, 2016 by Lindsay4047

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
answered Jun 25, 2016 by ericdl (1,286 points)
selected Jun 26, 2016 by Lindsay4047

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.