Cant change textureRect size in hboxcontainer

Godot Version

4.4

Question

Im trying to fill an hbox container with texture rect, but i cant change their size, here is the code where i add them:

func deal_hand():
	for i in range(8):
		var rand = randi() % deck_cards.size()
		var card = deck_cards[rand]
		deck_cards.remove_at(rand)
		var card_texture = load_card_texture(card)
		
		var card_node = TextureRect.new()
		card_node.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
		card_node.size_flags_vertical = Control.SIZE_SHRINK_CENTER
		card_node.texture = card_texture
		card_node.scale = Vector2(0.3, 0.3)
		
		card_node.connect("input_event", Callable(self, "_on_card_click").bind(card, card_node))
		
		hand_container.add_child(card_node)

here is the full code in case its usefull:

extends Node2D

@export var deck_cards: Array  # Array para almacenar las cartas
@onready var hand_container: HBoxContainer = $HBoxContainer  # Contenedor donde se mostrarán las cartas

func _ready():
	randomize()
	# Llenamos la baraja con cartas
	fill_deck()
	# Repartir 8 cartas a la mano
	deal_hand()

# Función para llenar la baraja con cartas
func fill_deck():
	var palos = ["hearts", "spades", "diamonds", "clubs"]
	var figuras = ["jack", "queen", "king"]
	
	deck_cards.clear()
	
	for palo in palos:
		for i in range(2, 15):  # De 2 a 14
			var suit = palo
			var score = i
			var is_face = false
			var face_type = ""
			
			if i == 11:
				face_type = "ace"  # El as tiene un valor de 11
			elif i == 12:
				face_type = "jack"
				score = 10  # Las figuras (jack, queen, king) tienen valor 10
				is_face = true
			elif i == 13:
				face_type = "queen"
				score = 10  # Las figuras (jack, queen, king) tienen valor 10
				is_face = true
			elif i == 14:
				face_type = "king"
				score = 10  # Las figuras (jack, queen, king) tienen valor 10
				is_face = true
			
			var card = {
				"suit": suit,
				"score": score,
				"is_face": is_face,
				"face_type": face_type,
				"selected": false
			}
			
			# Agregar la carta a la baraja
			deck_cards.append(card)

func deal_hand():
	for i in range(8):
		var rand = randi() % deck_cards.size()
		var card = deck_cards[rand]
		deck_cards.remove_at(rand)
		var card_texture = load_card_texture(card)
		
		var card_node = TextureRect.new()
		card_node.size_flags_horizontal = Control.SIZE_SHRINK_CENTER
		card_node.size_flags_vertical = Control.SIZE_SHRINK_CENTER
		card_node.texture = card_texture
		card_node.scale = Vector2(0.3, 0.3)
		
		card_node.connect("input_event", Callable(self, "_on_card_click").bind(card, card_node))
		
		hand_container.add_child(card_node)


# Función para cargar la textura de la carta basada en su información
func load_card_texture(card):
	var score = card["score"]
	var suit = card["suit"]
	var face_type = card["face_type"]
	var score2 = ""

	# Ajustar la representación del nombre para el archivo de la imagen
	if score == 11:
		score2 = "ace"
	elif card["is_face"]:
		score2 = face_type
	else:
		score2 = str(score)

	# Generar el nombre del archivo de la carta
	var file_name = "%s_of_%s.png" % [score2, suit]
	var route = "res://sprites/" + file_name
	
	# Cargar la imagen de la carta
	return load(route)

# Función que se llama cuando se hace clic en una carta
func _on_card_click(viewport, event, card, card_node):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
		# Cambiar la variable selected de la carta a true
		card["selected"] = true
		print("Carta seleccionada:", card["suit"], card["score"])
		# Cambiar la escala o apariencia para indicar que la carta está seleccionada
		card_node.scale = Vector2(0.35, 0.35)  # Ejemplo: aumentar tamaño para indicar selección

I don’t think you can do that. What you have to do is put your hbox as a child of a margin_container. Then add your texture rect as another child of the margin container. The margin container will expand to the size of the hbox, and then you can stretch your texture rect or tile it etc. The nine patch rect works superbly well in this situation if your texture needs it. A panel works very well too in this situation.

Hope that helps in some way.