0 votes

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

in Engine by (317 points)

1 Answer

0 votes

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

supertext.text = "your text here"
by (10,856 points)

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 theonelabel, 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?

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.

Yes that's right, I could completely erase "supertext" after refering theonelabel. Could just have used supertext for this in the first place, but this way my learning curve steepens a bit...

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?

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!

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.