How to spawn buttons inside a GridContainer depending on the number of folders and give names to the buttons.

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

So i have some sort of “Deck Selection” screen for a card game where you can just put a folder full of .pngs inside res://cards/ and i want those individual folders to appear as “tickable/toggleable” buttons so the user can choose to include every one of them to the output.

How do i expand upon this ?

var DecksList = Array()

func _ready():
	var AddButton =
	for n in DecksList:

this currently only adds 1 button without a name and not several buttons.
i hope to make it add X amount of buttons where X is the number of stuff in the DecksList, and i want it to name the buttons it adds in order like DecksList[0] and then DecksList[1] and so on automatically.

:bust_in_silhouette: Reply From: Wakatta

Hmm surprising this does not throw an error because you continually add the same button.


func _ready():
    for n in DecksList.size():
        var AddButton =
        AddButton.set_name("DecksList[%s]" % [n])
        AddButton.set_text("DecksList[%s]" % [n])

Is there a way to edit the style of these generated buttons.

The buttons are literally named DecksList[0], DecksList[1]… etc, can i give the respective folders name to these.

also this will be much to ask but can i get button signal outputs from them so i can assign the signals to… you know whether or not that folder is going to be included in the total array of Cards

mehmet432 | 2023-03-01 13:56

Yeah there is you can set the style you want then save it or set it up in code

var style =
style.bg_color =
AddButton.set('custom_styles/normal', style)

is DeckList a list of Folder Paths? if so

AddButton.set_name(DecksList[n].get_slice("/", 2))

to set up signals are as follows

func _on_pressed(button):

func _ready():
    for n in DecksList.size():
        var AddButton =
        AddButton.connect("pressed", self, "_on_pressed", [AddButton])

Wakatta | 2023-03-01 15:24

this is the DecksList, i want the buttons to be named as the subdirectory folders like
Button1 = Base Cards
Button2 = Booster Pack 1

This is the output i get when i “print(DecksList)”

var DecksList = [res://cards/., res://cards/.., res://cards/Base Cards, res://cards/Booster Pack 1, res://cards/Booster Pack 2, res://cards/Max Basic Pack, res://cards/Max Booster Pack 1, res://cards/Max Booster Pack 2, res://cards/Max Magic, res://cards/Optimized Cards (Hard)]

also i have noticed a problem just right now that i have 2 “res://cards/.” in the array.

i’m using this code to generate it and i assume those 2 will interfere with what i’m trying to do, so how do i remove those, i don’t even know why they are generated.

var numCards = 0
var dir =
var DecksList = Array()
var CardsPath = "res://cards/"

func _ready():
	if == OK:
		var file_name = dir.get_next()
		while (file_name != ""):
			if file_name.ends_with(".import"):
			if dir.current_is_dir():
				numCards += 1
			file_name = dir.get_next()

mehmet432 | 2023-03-01 18:38

To get the base folder of any path use the following


so the name will be

AddButton.set_name("%s" % DecksList[n].get_base_dir().get_file())

Wakatta | 2023-03-03 12:14

you can fix the double “res://cards” by using



dir.list_dir_begin(true, true)

#to avoid having to use this line
if file_name.ends_with(".import"):

Wakatta | 2023-03-03 12:17

You’ve been an unbelievable help,


until i saw this, i was using


to fix the problem :smiley:

mehmet432 | 2023-03-05 22:10

Been where you are now
Just know that it and you does gets better
And when it does just pay it forward.

Wakatta | 2023-03-05 22:48

Thank you Wakatta, :3

mehmet432 | 2023-03-05 22:50