peter-kish / gloot

A universal inventory system for the Godot game engine.
MIT License
625 stars 28 forks source link

InventoryItem not updating Protoset when added as a child node of an inventory #184

Closed whompratt closed 7 months ago

whompratt commented 7 months ago

In the gloot documentation, one of the ways to add an item to an inventory is described as follows:

Add items by creating InventoryItem nodes as child nodes of the inventory node.

NOTE: When an InventoryItem node is added to an inventory node, its protoset property is automatically set to be the same as the item_protoset property of the inventory node.

However, adding an item this way encounters an issue wherein the protoset for said item isn't updated. I believe this is because in 'inventory_item.gd', the following is present in the var setter:

@export var protoset: ItemProtoset :
    set(new_protoset):
        ...

        if _inventory != null:
            return

        ....

Because of this, the following code doesn't execute:

func _on_added_to_inventory(inventory: Inventory) -> void:
    assert(inventory != null)
    _inventory = inventory
    if _inventory.item_protoset:
        protoset = _inventory.item_protoset

    added_to_inventory.emit(_inventory)
    _inventory._on_item_added(self)

This means you can't manually update the protoset and get an error when had_prototype is called on the item.

whompratt commented 7 months ago

As a suggested fix, maybe the following:

@export var protoset: ItemProtoset :
    set(new_protoset):
        ...
        if _inventory != null && protoset != null:
            return
        ....

This would stop manual editing of the protoset still (which I think is the desired goal), but would allow the item's protoset to be set outright.

peter-kish commented 7 months ago

Looks like this bug has been introduced in a recent refactoring. I think it should be fixed now (20d3eb00771c15a5b2f36a0f8ccfab8497b84ab6).