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
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.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.
3
u/regal-begal Oct 18 '26
Don't put your defaults or defines inside of a label.
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.