automatically crated labels

Godot Version

v4.2.2.stable.official [15073afe3]

Question

so i have a display of 20 buttons with 2 labels each created from an array with a for each, how can i acces that labels to update them?

theres no label or button created manually
image

here is the code of the container script:

extends VBoxContainer

var button_data = [
{"name": "Botón 1", "id": 101},
{"name": "Botón 2", "id": 102},
{"name": "Botón 3", "id": 103},
{"name": "Botón 4", "id": 104},
{"name": "Botón 5", "id": 105},
{"name": "Botón 6", "id": 106},
{"name": "Botón 7", "id": 107},
{"name": "Botón 8", "id": 108},
{"name": "Botón 9", "id": 109},
{"name": "Botón 10", "id": 110},
{"name": "Botón 11", "id": 111},
{"name": "Botón 12", "id": 112},
{"name": "Botón 13", "id": 113},
{"name": "Botón 14", "id": 114},
{"name": "Botón 15", "id": 115},
{"name": "Botón 16", "id": 116},
{"name": "Botón 17", "id": 117},
{"name": "Botón 18", "id": 118},
{"name": "Botón 19", "id": 119},
{"name": "Botón 20", "id": 120}
]


func _ready():
for i in range(button_data.size()):
var data = button_data[i]

# Crear un HBoxContainer
var hbox = HBoxContainer.new()

# Crear un Label (opcional, para mostrar el nombre)
var label = Label.new()
label.text = data["name"]

# Crear un Button
var button = Button.new()
button.text = data["name"]

button.set_script("res://Scripts/ButtonScript.gd")

# Asignar el ID como metadata del botón
button.set_meta("button_id", data["id"])

# Conectar la señal pressed del botón a la función del contenedor principal
button.connect("pressed", Callable(self, "_on_button_pressed").bind(button))

# Añadir Label y Button al HBoxContainer
hbox.add_child(label)
hbox.add_child(button)

# Añadir HBoxContainer al VBoxContainer
add_child(hbox)

# Añadir un nodo de espaciador si no es el último elemento
if i < button_data.size() - 1:
var spacer = Control.new()
spacer.set_custom_minimum_size(Vector2(1, 60))
add_child(spacer)

# Función llamada cuando se presiona un botón
func _on_button_pressed(button):
var button_id = button.get_meta("button_id")
var index = button_id - 101
var cost = Global.upgrades[index]["cost"]
var cpsUp = Global.upgrades[index]["cpsUp"]
if Global.coins >= cost:
Global.coins -= cost
Global.upgrades[index]["cost"] += Global.upgrades[index]["cost"]*0.15
Global.coinsPerSecond += cpsUp

You can name and label, and create a variable of labels: Array = , so after adding the label in scene tree, do like labels.append(label)
Or if you want to do that on editor, then you need to study that how to create own plugin in Godot

Just add the Label node to the bind.

button.connect("pressed", Callable(self, "_on_button_pressed").bind(button, label))

and in function:

func _on_button_pressed(button, label):