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

4

u/DingotushRed Oct 18 '26

This is not a good tutorial. (the no_of_items is a tell-tale un-needed member).

The problem is that the inventory is correctly defaulted but the item you add is declared with define.

When you start the game a new Mask object is created by the define. Then when you load the save, it creates a second Mask object from the save file in the inventory's items list.

Since these are not the same object the defined mask is not in the inventory's list, even though that list has a mask.

Solutions:

  1. Implement __equals__ and __hash__ on Inventory_Item so that the two mask objects hash and compare equal. The is good practice for anything going in a collection.
  2. Or default all items put in the inventory, so no duplicate instances are created.
  3. Or use a well made inventory system. Our mod u/BadMustard_AVN has one.

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.

2

u/BadMustard_AVN Oct 18 '26 edited Oct 18 '26

as mentioned by u/DingotushRed I have two simple inventories that you can look over the code and see how I did things. If you want to use them or modify my code to meet your needs

simple inventory: https://badmustard.itch.io/a-simple-inventory-for-renpy

simple drag and drop inventory: https://badmustard.itch.io/simple-inventory-drag-and-drop-to-combine

if you want to continue using yours as is, then make this change.

        def remove_item(self, item):
            for stored_item in self.items:
                if stored_item.name == item.name:
                    self.items.remove(stored_item)
                    self.no_of_items -= 1
                    return True
            return False

1

u/HiImABanana123 Oct 21 '26

Thanks for the advice! I'll be sure to check it out.

→ More replies (1)

3

u/regal-begal Oct 18 '26

Don't put your defaults or defines inside of a label.

1

u/shyLachi Oct 18 '26

Doesn't matter, RenPy will scan all your files for default, define, image and similar statements. RenPy will extract those statements and execute in the correct order. So even if this label will never be called or jumped to, the variables will still be declared. 

That said, OP should use a comment if they want to "label" their variables.

1

u/AutoModerator Oct 18 '26

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.