Arranging labels inside of panelcontainer

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

So, I’m trying to have a column of panels where each one contains one or two labels.
Thing is, I want the labels to be “centered” inside of each panel.
The current code is supposed to grab the x size and position of each panel, which are saved in variables that are available inside of the function, and center them (return_array_of_xpositions() returns the necessary x positions to assign to each label and have them well-alligned)
While debugging I noticed:
The margin of each label IS assigned as suposed to.
But then, when the for loop ends and the create_layers function is completed, they are reset and the value assigned is wiped.
Anyone has an idea about why could this be happening?
At risk of stating the obvious, if there is a more straightforward way for centering those labels in the HBox (Each panelcontainer is parent of an hbox, which is the parent of each label) it shall be well received :slight_smile:

func create_layers():
var new_vbox = VBoxContainer.new()
var layer_leftx = my_viewport.size.x * 0.08
var layer_rightx = my_viewport.size.x * 0.92
new_vbox.margin_left = layer_leftx
new_vbox.margin_right = layer_rightx
new_vbox.margin_top = my_viewport.size.y * 0.2
add_child(new_vbox)
#new_vbox.margin_bottom = my_viewport.size.y * 0.2
for element in elements_array:
	var new_layer = PanelContainer.new()
	var layer_hbox = HBoxContainer.new()
	new_layer.add_child(layer_hbox)
	var layer_digit = Label.new()
	layer_digit.set('custom_fonts/font', layers_font)
	layer_digit.text = panel_map.number_to_text_map[element.digit][language]
	layer_hbox.add_child(layer_digit)
	if element.decimal != 0:
		var layer_amplifier = Label.new()
		layer_amplifier.set('custom_fonts/font', layers_font)
		layer_amplifier.text = panel_map.amplifier_to_text_map[element.decimal][language]
		layer_hbox.add_child(layer_amplifier)
#		var hbox_position = layer_hbox.rect_position
#		var hbox_size = layer_hbox.rect_size
	var necessary_xpositions = return_array_of_xpositions_not_equal(layer_hbox.get_children(), layer_leftx, layer_rightx)
	var layer_hbox_array = layer_hbox.get_children()
	new_vbox.add_child(new_layer)
	for i in layer_hbox_array.size():
		layer_hbox_array[i].margin_left = necessary_xpositions[i]
	pass
:bust_in_silhouette: Reply From: zhyrin

Wouldn’t it work to have a CenterContainer in your panels?