How to add Controls to a Vbox via code?

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

I’m trying to make an inventory system like Pokémon (every item shown in a list). I just don’t know how to add labels and textures for every new item that’s added.
How can I do this?

If it helps, the items are being saved in an array.

Are you referring to a VBoxContainer? If so, you add your Control nodes as children to the VBoxContainer.

Ertain | 2020-07-04 03:32

Yes. Manually, that’s the idea. But how do I make it so the code can add a new control for every new item got?

Lfzinho | 2020-07-04 03:38

have you tried instancing a “control” scene into the vbox node?

dustin | 2020-07-04 12:16

:bust_in_silhouette: Reply From: njamster

Your question is a bit sparse on details but this should give you an idea anyways:

extends VBoxContainer

var items = [
    { "texture": "res://apple.png", "name": "Apple"}, 
    { "texture": "res://banana.png", "name": "Banana"}
]

func open_Inventory():
    for item in items:
        var item_row = HBoxContainer.new()

        var item_image = TexureRect.new()
        item_image.texture = load(item.texture)
        item_row.add_child(item_image)

        var item_name = Label.new()
        item_name.text = str(item.text)
        item_row.add_child(item_name)

        add_child(item_row)

    show()

func close_inventory():
    for item in get_children():
        item.queue_free()

    hide()