HBoxContainer question

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

Can someone tell me how to use an HBoxContainer from code. I’m trying to add_child several times and add a sprite each time I click left mouse button on screen. It adds it the first time but then just puts the next sprite on top of the last. It seems like it stops adding children on some level or doesn’t space it out right. Here’s the code I’m frustrated with… For the record I’m trying to make the players hand of cards appear in a HBoxContainer. Any insight is appreciated.

if Input.is_action_just_released("mouseleft"):
	showcard = scards[cardcount].substr(0,2).to_int()
	print(String(showcard) + " " + scards[cardcount])
	get_node("Sprite").texture = actual[showcard]
	get_node("Label").text = scards[cardcount]
	get_node("Label2").text = String(cardcount)

	cardn.append(Sprite.new())
	cardn[cardcount].set_name("card" + String(cardcount))
	cardn[cardcount].texture = actual[showcard]

	cardn[cardcount].set_scale(s)
	print(cardn[cardcount].name)
	for n in cardcount:
		get_node("HBoxContainer").add_child(cardn[n])
		print(cardn[n].name)
		#get_node("HBoxContainer").add_spacer(true)
	
	cardcount = cardcount + 1
	if cardcount == 78:
		cardcount = 0
:bust_in_silhouette: Reply From: estebanmolca

Can you make the cards a control type node? So you can set the size flag property on each card to SIZE_EXPAND_FILL. Or better, I would do it manually, it would be something like that, keep in mind that it is a quick code of tests you would have to adjust details but basically it is about dividing the width of the control (although it can be any node that has a width) to get the position :

extends Node2D
var scn=preload("res://SpriteScene.tscn")
var cardcount=5
func _ready():
	pass
	
func add_hijo():
	for i in range(cardcount):
		var s=scn.instance()
		var pos=$HBoxContainer.get_global_rect().size.x / cardcount * i
		s.position.x= pos
		s.position.y= $HBoxContainer.get_global_rect().position.y
		$HBoxContainer.add_child(s)
	
func _input(event):
	if event is InputEventMouseButton and event.is_pressed():
		add_hijo()	
:bust_in_silhouette: Reply From: njamster

HBoxContainer is a control-node and only applies to other control-nodes. A Sprite however is no control-node. Consider using a TextureRect instead.

This helped a lot.THANKS! However, now I can’t seem to scale the TextureRect. I’ve tried looking at inhereted classes and using things like

	cardn[cardcount].set_scale(s)
	cardn[cardcount].set_stretch_mode(1)
	#cardn[cardcount].apply_scale(s)
	#cardn[cardcount].set_size(s)

Nothing seems to scale it correctly. Is there another node I should wrap the textureRect in maybe and .add_child to the .add_child?

bluemage | 2020-02-07 20:02

var s = Vector2(2.0, 2.0)
cardn[cardcount].expand = true
cardn[cardcount].set_stretch_mode(5)
cardn[cardcount].rect_min_size = cardn[cardcount].rect_size * s

This will only work of course, if rect_size is set to the size of the texture. Which will happen automatically when you add a texture in the inspector, but has to be done manually if you’re doing that from within GDscript. I assume this won’t be an issue, given that your designing a card game and I expect all cards to have a uniform size.

njamster | 2020-02-08 15:41