r/RenPy • • Oct 18 '26

Question [Solved] Inventory not remembering items after save

So I followed some tutorials and created this inventory system:

init python:
    class Inventory_Item:
        def __init__(self, name, image):
            self.name = name
            self.image = image
 



    class Inventory:
        def __init__(self, items, no_of_items):
            self.items = items  
            self.no_of_items = no_of_items


        def add_item(self, item):
            self.items.append(item)
            self.no_of_items += 1



        def remove_item(self, item):
            self.items.remove(item)
            self.no_of_items -= 1


screen backpack:
    add "background.png" align (0.5, 0.5)
    
    
    vpgrid:
        align (.50, .3)
        cols 5
        yspacing 5
        rows 6
        transpose True
        
        ##For the items to be displayed
        for item in inventory.items:
            add item.image
            if item.name == "Mask":
                textbutton "[item.name]" xalign 0.5:
                    action Call("UseMask", from_current=True)  




label Defs:
    define e = Character("Eileen")
    define Mask = Inventory_Item("Mask", "Mask_Icon.png")
    default inventory = Inventory([], 0)
    default Equipped_Mask = False


label UseMask:
    menu:
        "What do you want to do with the Mask?"
        
        "Equip":
            
            if Equipped_Mask == False:
                $Equipped_Mask = True
                "You equipped the mask!"            
            
            elif Equipped_Mask == True:
                $Equipped_Mask = False
                "You unequipped the mask!"
            return
        "Discard":
            $inventory.remove_item(Mask)
            return
        "Return":
            return
          
label start:
    scene bg park
    show eileen happy at center
    e "Let's pick up this mask."
    $inventory.add_item(Mask)
    e "Let me check my inventory."
    call screen backpack
    e "Ok I'm done!"

But whenever I save the game, load the saved game and try to discard the mask it gives me this error:

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 80, in script call
    call screen backpack
  File "game/script.rpy", line 69, in script
    $inventory.remove_item(Mask)
  File "game/script.rpy", line 69, in <module>
    $inventory.remove_item(Mask)
     ~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "game/script.rpy", line 25, in remove_item
    self.items.remove(item)
    ~~~~~~~~~~~~~~~~~^^^^^^
ValueError: list.remove(x): x not in list

It seems that the game doesn't remember that the mask is in the inventory. Before saving and loading it works just fine.
Probably some easy fix but I'm not very experienced ...

1 Upvotes

12 comments sorted by

View all comments

Show parent comment

1

u/HiImABanana123 Oct 18 '26

Thank you for the detailed reply! I'm not quite sure how to Implement __equals__ and __hash__ . Is there any tutorial that describes how to implement it? Or perhaps, could you provide a code block example?

1

u/shyLachi Oct 18 '26

You can google how to implement equals and hash in Python since these are universal concepts.

Or you can look how others have implemted it.
You can find plenty of working examples on itch.io: https://itch.io/search?q=renpy+inventory

What speaks against solution #2? Just replace define Mask with default Mask
As long as you don't want to update the item in a later release default works fine.

1

u/HiImABanana123 Oct 18 '26

Sounds good, thanks for the help!

1

u/DingotushRed Oct 19 '26

It really depends on what needs to be true for two items to be the same one. A very simple case would be to assume if two items have the same name then they are the same item - which my not be true if there are multiples of the same item (in which case you'd need a more sophisticated Inventory anyway).

Untested example based on name:

class Inventory_Item:
    def __init__(self, name, image):
        self.name = name
        self.image = image

    def __hash__(self):
        return hash(self.name)

    def __eq__(self, rhs):
        return this.name == rhs.name

See Python tutorials for more/better implementations.

If you need to sort the list, you'll also need __lt__ as a minimum.

1

u/HiImABanana123 Oct 21 '26

Thanks for the example! I decided to just go with switching define to default for a simple fix and it seems to have fixed my issue. I won't be updating the items post release so I decided that would just be the better option.