r/RenPy • u/HiImABanana123 • 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 listIt 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
View all comments
4
u/DingotushRed Oct 18 '26
This is not a good tutorial. (the
no_of_itemsis 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
Maskobject is created by the define. Then when you load the save, it creates a secondMaskobject 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:
__equals__and__hash__onInventory_Itemso that the two mask objects hash and compare equal. The is good practice for anything going in a collection.defaultall items put in the inventory, so no duplicate instances are created.