+1 vote

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
in Engine by (60 points)

2 Answers

+2 votes
Best answer

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

by (10,543 points)
selected by

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 .addchild to the .addchild?

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.

0 votes

Can you make the cards a control type node? So you can set the size flag property on each card to SIZEEXPANDFILL. 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()  
by (2,238 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.