Random beginner-question: tap button to write text in label

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

Hi everyone,

I built a 0-9 number-pad from several Buttons and placed a Label above. What I’d like is to click on a number and have it written in the label.

I managed to have the dedicated text appear directly on the button with this code:

extends Button

onready var supertext = get_tree().get_root().find_node("TheOneLabel")

func _ready():
	set_process_input(true)

func _on_Button_gui_input(event):
	if event.is_action_pressed("mousebuttonclick"):
		print ("clicked!)
	elif event.is_action_released("mousebuttonclick"):
		_the_wonders_of_button_1()

func _the_wonders_of_button_1():
	set_text("That's a 1!")

But I couldn’t make it into TheOneLabel yet, although I have the strong feeling that the var supertext might come in handy… can anyone tell me how I can connect my Buttons to the Label? (Tapping buttons in a row should create a row of numbers then. And is a Label actually useful to further use its text or should I maybe use some different text-node in the first place?)

:bust_in_silhouette: Reply From: jgodfrey

Assuming your supertext variable has a valid reference to the your Label node, you can set its text via:

supertext.text = "your text here"

I added export (NodePath) var the_one_label, assigned it to the Label node and changed the

func _the_wonders_of_button_1():
	get_node(the_one_label).supertext.text = "your text here"

Now I get the error message Invalid get index 'supertext' (on base: 'Label'). and am a little lost.
…yes, and just understood that I changed supertext to the_one_label, so this is working. Thanks once more for your help!

The next step would be to string clicked numbers together in the label. Now there’s only one being shown at a time. I assume I need a script attached to the the Label node “collecting” the various inputs to a string, something like [“x” + “y” + “z”], but in this field I don’t have any experience yet at all. Is there any function made for this?

pferft | 2020-11-30 17:35

This seems to be a bit of a moving target… But, with the above-mentioned change, you’d want this code (assuming you set the value of the exported variable to your label via the inspector)

func _the_wonders_of_button_1():
    get_node(the_one_label).text = "your text here"

supertext has nothing to do with it now.

jgodfrey | 2020-11-30 18:15

Yes that’s right, I could completely erase “supertext” after refering the_one_label. Could just have used supertext for this in the first place, but this way my learning curve steepens a bit…

pferft | 2020-11-30 18:26

The next step would be to string clicked numbers together in the label. Now there’s only one being shown at a time. I assume I need a script attached to the the Label node “collecting” the various inputs to a string, something like [“x” + “y” + “z”], but in this field I don’t have any experience yet at all. Is there any function made for this?

Didn’t we already have a long and winding discussion about this over here, where I already provided you with a working example?

Isn’t this the same question?

jgodfrey | 2020-11-30 18:40

Oh yes. Of course it is. (I haven’t forgotten, but I think I’m done for today, It’s getting late…)

I believe I’m trying the opposite approach in this case, setting up the visible textfield with all it’s quirks and nice behaviour front and center and then grab the content for further usage. Thanks for really pushing on today’s progress!

pferft | 2020-11-30 19:18