How to add nodes to GridContainer

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

I’m trying to make a scrollable inventory grid. I have a GridContainer inside a ScrollContainer:

editor screenshot

When the popup emits about_to_show(), I call fill_grid():

func fill_grid():
    # Clear existing items from the grid.
    while inventory_grid.get_child_count() > 0:
    for i in range(0, character.inventory.size()): 
        var inventory_grid_item = preload("res://gui/InventoryGridItem.tscn").instance()

There I add a bunch of InventoryGridItem instances. InventoryGridItem is just a TextureRect set to the Godot logo texture.

I verified that the for loop is entered and the InventoryGridItem instances are being added to the grid, but they don’t show in the GridContainer. I know I’m doing something wrong, but the documentation for adding nodes to a GridContainer is non-existent…

So, what do I need to do to get the items to show up?

did you set min_size for added node?

volzhs | 2019-04-29 19:52

did you set min_size for added node?

Great hint and was the solution for my children of the gridcontainer not showing up: i only set “child.rect_size” and not “child.rect_min_size”. Now they appear, thank you so much @volzhs! P.S. Maybe others want to try the solution by the author’s answer below too, but keep in mind that ItemList is very limited for an inventory and e.g. only adds ONE ITEM OF A KIND (String + Title of one Element) and not amounts of Items or other Data about it. A Item List with Entries like “Potion x99” are not possible per default.

Captain.Susuwatari | 2020-04-20 18:00

:bust_in_silhouette: Reply From: oskfo

I just ended up using ItemList (the node is called $Grid):

func fill_grid():
    # Clear existing items from the grid.
    while $Grid.get_item_count() > 0:
    for i in range(0, character.inventory.size()): 
        var item_stack = character.inventory.item_at(i)
        var item_data = ItemDatabase.item_at(item_stack.item_name)
        $Grid.add_item(item_stack.item_name + " (" + str(item_stack.stack_size) + ")",

I still don’t know if it supports drag and drop though…

:bust_in_silhouette: Reply From: pacri

GridContainer is a container node itself. Maybe its because of the lifecycle callback of the popup you’re using, but you can try to call the container queue_sort function to make the container realign its children.
Btw, if this is not working, what are your child node’s size flags? Do they have a min size?