How to return text from one node to another?

: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 want to create something similar to text adventure games, where you input your text into a box (the LineEdit node) and the output returns in a text box above it, as well as the result of your action right after.

How would I go about returning the text you input into the above node? I’m using the LineEdit node as the input and a RichTextLabel as the output.

Thanks in advance.

:bust_in_silhouette: Reply From: Warlaan

As a rule of thumb: whenever you want to pass information or trigger something in a node of the current scene use function calls.
If on the other hand you want to pass information or trigger something in a node of the surrounding scene like in your case then use signals.

Signals allow you to add a function call before you even know which function is going to be called. By connecting the signal you are assigning the function to be called without having to know when exactly it is called.

In your case you can simply use a built-in signal of the LineEdit node: LineEdit — Godot Engine (latest) documentation in English

Warlaan | 2016-06-23 08:48

Do you happen to know what the full code might look like? My code isn’t carrying over the text (the player input) and the debugger isn’t giving me any hints as to why.

I know now that I need to use func _on_lineInput_text_entered()as a signal (lineInput being the LineEdit node), but I can’t figure out to actually pass it over to the RichTextLabel so it types it into it.

Lindsay4047 | 2016-06-23 22:01

There are some caveats.

  1. If you want to connect the signal using the connect-button above the node list in the Scene tab the target object (in your case the RichTextLabel) needs to have a script attached, otherwise the function to be called is not generated. In that case either write it yourself or just detach the signal and reattach it after you have added a script to the target node.

  2. I haven’t worked with the RichTextLabel before and it took me a few moments to figure out how to get it to display text at all. If I am not mistaken you need to enable Bbcode and then use the set_bbcode()-method.

  3. You should connect both the text_entered-signal and the text_changed-signal, otherwise you won’t see any changes until the user presses enter.

So this is what my script looks like, but I guess the interesting bits happen in the editor, so the comments above will hopefully help you more than the code.

extends RichTextLabel

func _on_LineEdit_text_changed( text ):
	set_bbcode(text)

func _on_LineEdit_text_entered( text ):
	set_bbcode(text)

Warlaan | 2016-06-24 05:30

Yep, that was it. The bbcode() is how to edit/add text for RichTextLabel nodes apparently. Thanks so much for your help!

Lindsay4047 | 2016-06-24 22:01