0 votes

Hello, I'm following a Youtube tutorial on how to design an inventory system. I'm not getting any errors, however I can't get anything to appear in the player's inventory. It should go like this:

  1. Player moves to area and clicks a button
  2. A new child of the label type gets added to a grid container which is attached to the player
  3. Grid container should display all items currently in the inventory.

The problem is that I can't get the grid container to add any labels, even though items seem to be getting added to the _items array. When I try printing the array after adding an item I get this output:

[{item_reference:[Resource:1308], quantity:50}

that appears about 100 times or so.

Inventory script:

extends Resource

class_name Inventory

signal inventory_changed

export var items = Array() setget setitems, get_items

func setitems(newitems):
items = newitems
emitsignal("inventorychanged", self)

func get_items():
return _items

func get_item(index):
return _items[index]

func additem(itemname, quantity):
if quantity <= 0:
print("Can't add less than 0 items, ya schmuck!")
return

var item = ItemDatabase.get_item(item_name)

if not item:
    print("Could not find item, ya schmuck!")
    return

var remaining_quantity = quantity
var max_stack_size = item.max_stack_size if item.stackable else 1

if item.stackable:
    for i in range(_items.size()):
        if remaining_quantity == 0:
            break

        var inventory_item = _items[i]

        if inventory_item.item_reference.name != item.name:
            continue

        if inventory_item.quantity < max_stack_size:
            var original_quantity = inventory_item.quantity
            inventory_item.quantity = min(original_quantity + remaining_quantity, max_stack_size)
            remaining_quantity -= inventory_item.quantity - original_quantity

while remaining_quantity > 0:

    var new_item = {

        item_reference = item,
        quantity = min(remaining_quantity, max_stack_size)

    }

    _items.append(new_item)
    remaining_quantity -= new_item.quantity

emit_signal("inventory_changed", self)

print(_items)

GameManager script:

extends Node

signal player_initialised

var player

func ready():
if not player:
initialise
player()
return

func initialiseplayer():
player = get
tree().getroot().getnode("/root/World/Player")
if not player:
print("That player path was wrong, ya schmuck!")
return

emit_signal("player_initialised", player)

player.inventory.connect("inventory_changed", self, "_on_player_inventory_changed")

var existing_inventory = load("user://inventory.tres")
if existing_inventory:
    player.inventory.set_items(existing_inventory.get_items())

func onplayerinventorychanged(inventory):
ResourceSaver.save("user://inventory.tres", inventory)

InventoryGrid script:

extends GridContainer

func ready():
GameManager.connect("player
initialised", self, "onplayer_initialised")
print("here")

func onplayerinitialised(player):
player.inventory.connect("inventory
changed", self, "onplayerinventorychanged")

func onplayerinventorychanged(inventory):
for n in getchildren():
n.queue
free()

for item in inventory.get_items():
    var item_label = Label.new()
    item_label.text = "%s x%d" % [item.item_reference.name, item.quantity]
    add_child(item_label)

ItemDatabase script:

extends Node

var items = Array()

func ready():
var directory = Directory.new()
directory.open("res://Items/")
directory.list
dir_begin()

var file_name = directory.get_next()
while(file_name):
    if not directory.current_is_dir():
        items.append(load("res://Items/%s" % file_name))

    file_name = directory.get_next()

func getitem(itemname):
for i in items:
if i.name == item_name:
return i

return null

Item script

extends Resource

class_name ItemResource

export var name : String
export var stackable : bool = true
export var maxstacksize : int = 1

I'm sorry if my blockquotes aren't including all my code, I am not sure how to fix it. Thanks in advance!

Godot version 3.4.4
in Engine by (12 points)

The code looks OK to me. Are you sure that the Grid is visible such as it's height/width is not zero for example?

That could be the issue! How would I check that? Would I hardcode the dimensions of the label somehow?

Sorry, I misread your question. Currently I have a control node with a child of type panel set to fill rect and a grid container as the child of panel. I'll make sure that I have the grid container's size set properly. Thanks for the suggestion!

Please log in or register to answer this question.

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.