+2 votes

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

editor screenshot

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

func fill_grid():
    # Clear existing items from the grid.
    while inventory_grid.get_child_count() > 0:
        inventory_grid.remove_child(inventory_grid.get_child(0))

    for i in range(0, character.inventory.size()): 
        var inventory_grid_item = preload("res://gui/InventoryGridItem.tscn").instance()
        inventory_grid.add_child(inventory_grid_item)

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?

in Engine by (38 points)

did you set min_size for added node?

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.

2 Answers

0 votes

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:
        $Grid.remove_item(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) + ")",
            load(item_data["ui_texture"]))

I still don't know if it supports drag and drop though..

by (38 points)
0 votes

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?

by (14 points)
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 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 webmaster@godotengine.org with your username.