0 votes

Hi guys,
I know this question may sound silly, but I am an unexperienced programmer.
I am trying to code something that should create and show a button over a Panel.
Here is what i've tried :

extends Panel

func _ready():
    OS.set_window_size(Vector2(800,600))
    set_size(OS.get_window_size())
    var button1 = Button.new()
    button1.set_pos(Vector2(20,20))
    button1.set_size(Vector2(80,20))
    button1.show()
    pass

But it doesn't work and I don't even see the button1 variable in the debugger tab.
Do you know what I've done wrong ?
Sorry if this is stupid and thanks for your help.

in Engine by (19 points)
reshown by

3 Answers

+1 vote
Best answer

You forgot to add the button to the scene tree

...

func _ready():
    ...
    add_child(button1)
    pass

'... 'is a placeholder for the code you already wrote in your question

by (484 points)
selected by
+1 vote

Everything in the node hierarchy has to be child of a node. Try to insert add_child(button1) once button1 is prepared before show it. Possibly show() is not needed.

by (681 points)
0 votes

It worked !
Thanks again for your help, it's very appreciated.

by (19 points)
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.