How to show buttons created in code on screen?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dontknowanything
   extends Button
var arg=[]
func ready():
    arg.resize(76)
    for i in range(arg.size()):
        var button = Button.new()
        add_child(button)
        button.set_size(Vector2(64,64))
        button.connect("pressed", self, "myrotate", [i])
        $button["custom_styles/normal"].bg_color = Color("#bada55")
        button.show()
        arg.insert(i, button)
    arg[0].set_position(Vector2(192,64))
    arg[1].set_position(Vector2(256,64))

Created buttons aren’t showing on screen when I start the game. It’s not showing any errors either. Any and all help is apreciated.

:bust_in_silhouette: Reply From: godol

Put a child in the button.

in “add_child(button)”

put a way

example:
“$”.“.add_child(button)”

and use a visible
button.visible = true

if the button dont appears anyways, try put a position in the button.

extends Button

var arg=[]

func ready():
	arg.resize(76)
	for i in range(arg.size()):
		var button = Button.new()
		$Button.add_child(button)
		button.set_size(Vector2(64,64))
		button.visible=true
		button.connect("pressed", self, "myrotate", [i])
		$button["custom_styles/normal"].bg_color = Color("#bada55")
		button.show()
        arg.insert(i, button)
	arg[0].set_position(Vector2(192,64))
	arg[1].set_position(Vector2(256,64))

I tried what you said, it still isn’t showing. Maybe I missunderstood you. Is it supposed to be like this? I cut out the code with positions of the buttons. Each one of them had a position.

dontknowanything | 2022-11-27 21:52

$Button is wrong.

try use

get_tree().root.add_child(button)

godol | 2022-11-30 22:54

:bust_in_silhouette: Reply From: dontknowanything

For anyone having the same problem, you shouldn’t add button as a child of another button. Add it as a child of a control node. There are some minor syntax mistakes also in my code. So here is how it looks now, it still has problems like rotating it, but visibility is ok.

extends Control
var arg=[]

    func _ready():
    	arg.resize(76)
    	for i in range(arg.size()):
    		var button = TextureButton.new()
    		button.set_rotation_degrees(0)
    		button.set_size(Vector2(64,64))
    		button.texture_normal = load("res://cijev.png");
    		button.connect("pressed", self, "myrotate", [i])
    		arg.insert(i, button)
    		add_child(arg[i])
    		setposition(i)

Discord community helpen in solving this.

dontknowanything | 2022-11-29 20:06