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 = Button.new()
	for n in DecksList:
		add_child(AddButton)

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.

Correction

func _ready():
    for n in DecksList.size():
        var AddButton = Button.new()
        add_child(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 = StyleBoxFlat.new()
style.bg_color = Color.red
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):
    print(button.name)

func _ready():
    for n in DecksList.size():
        var AddButton = Button.new()
        add_child(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
etc…

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 = Directory.new()
var DecksList = Array()
var CardsPath = "res://cards/"

func _ready():
	if dir.open(CardsPath) == OK:
		dir.list_dir_begin()
		var file_name = dir.get_next()
		while (file_name != ""):
			if file_name.ends_with(".import"):
				pass
			else:
				DecksList.push_back((CardsPath)+file_name)
			if dir.current_is_dir():
				numCards += 1
			file_name = dir.get_next()
			print(DecksList)

mehmet432 | 2023-03-01 18:38

To get the base folder of any path use the following

get_base_dir().get_file()

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)

and

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,

dir.list_dir_begin(true)

until i saw this, i was using

DecksList.remove(0)
DecksList.remove(0)

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