Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
I'm currently implementing an inventory system for item collection in my game.
My implemented code seemed to be working fine, but when I restart the game program and load the save data, all the items that were added to the inventory disappear. How can I solve this issue?
I will attach the code I used to implement the inventory and the variables I use to load items into the inventory.
init python:
class clue:
clues_dict = {} # Class variable to store all clues
def __init__(self, name, image):
self.name = name
self.idle_image = "images/item/" + image # Path for the default image
self.hover_image = "images/item/" + image # Path for the image when hovered
clue.clues_dict[name] = {
"idle_image": self.idle_image,
"hover_image": self.hover_image
}
my_clue = []
def add_clue(item):
# Check if an item with the same name already exists in the list.
for existing_item in my_clue:
if existing_item.name == item.name:
return # If an item with the same name exists, do not add and exit the function.
my_clue.append(item) # If there are no duplicate items, add it to the list.
# Developer's function to clear inventory
def clear_inventory():
global my_clue
my_clue = []
screen clues_button():
if clue_menu and not dialogue_active:
imagebutton:
idle "gui/menu.png"
hover "gui/menu.png"
action Show("clues_menu")
xpos 1770 # Adjust xpos to set the horizontal position of the button
ypos 10
xanchor 0.0
yanchor 0.0
screen clues_menu():
# Create a clickable button using gui/black_base.png, closes the screen when clicked.
button:
image "gui/menu_0.png"
action NullAction()
xalign 0.5
yalign 0.5
button:
text "Clear Inventory"
action Function(clear_inventory)
grid 3 3:
spacing 13
for i, item in enumerate(my_clue):
if i < 9: # Display only up to 9 items in a 3x3 grid
imagebutton:
idle item.idle_image
hover item.hover_image
action Function(show_item_screen, item)
xpos 897
ypos 220
fgfg3356 wrote: ↑Sun Jan 14, 2024 8:06 am
My implemented code seemed to be working fine, but when I restart the game program and load the save data, all the items that were added to the inventory disappear. How can I solve this issue?
I don't actually write classes, so I'm not very familiar with them, but at first glance I'm surprised at line 16, where my_clue = [] is emptied/cleared, why?
init python:
class clue:
clues_dict = {} # Class variable to store all clues
def __init__(self, name, image):
self.name = name
self.idle_image = "images/item/" + image # Path for the default image
self.hover_image = "images/item/" + image # Path for the image when hovered
clue.clues_dict[name] = {
"idle_image": self.idle_image,
"hover_image": self.hover_image
}
my_clue = [] # <- why clear the list here?
def add_clue(item):
...
fgfg3356 wrote: ↑Sun Jan 14, 2024 8:06 am
My implemented code seemed to be working fine, but when I restart the game program and load the save data, all the items that were added to the inventory disappear. How can I solve this issue?
I don't actually write classes, so I'm not very familiar with them, but at first glance I'm surprised at line 16, where my_clue = [] is emptied/cleared, why?
init python:
class clue:
clues_dict = {} # Class variable to store all clues
def __init__(self, name, image):
self.name = name
self.idle_image = "images/item/" + image # Path for the default image
self.hover_image = "images/item/" + image # Path for the image when hovered
clue.clues_dict[name] = {
"idle_image": self.idle_image,
"hover_image": self.hover_image
}
my_clue = [] # <- why clear the list here?
def add_clue(item):
...
But maybe I'm barking up the wrong tree.
To ensure that no clues have been collected when the game starts, I left it empty. Could this be the cause?
fgfg3356 wrote: ↑Sun Jan 14, 2024 9:55 am
To ensure that no clues have been collected when the game starts, I left it empty. Could this be the cause?
If you've defaulted that list it should be cleared at the first time running the game but not on loading.
And I'm sorry, I just saw it now, you don't return from the function add_clue(item) and the clear_inventory(): function will be executed if an item was append. Are you sure that your function work ingame?
my_clue = []
def add_clue(item):
# Check if an item with the same name already exists in the list.
for existing_item in my_clue:
if existing_item.name == item.name:
return # If an item with the same name exists, do not add and exit the function.
my_clue.append(item) # If there are no duplicate items, add it to the list.
## the next function will be executed too, if I'm not wrong
# Developer's function to clear inventory
def clear_inventory():
global my_clue
my_clue = []
fgfg3356 wrote: ↑Sun Jan 14, 2024 9:55 am
To ensure that no clues have been collected when the game starts, I left it empty. Could this be the cause?
If you've defaulted that list it should be cleared at the first time running the game but not on loading.
And I'm sorry, I just saw it now, you don't return from the function add_clue(item) and the clear_inventory(): function will be executed if an item was append. Are you sure that your function work ingame?
my_clue = []
def add_clue(item):
# Check if an item with the same name already exists in the list.
for existing_item in my_clue:
if existing_item.name == item.name:
return # If an item with the same name exists, do not add and exit the function.
my_clue.append(item) # If there are no duplicate items, add it to the list.
## the next function will be executed too, if I'm not wrong
# Developer's function to clear inventory
def clear_inventory():
global my_clue
my_clue = []
Yes, the function worked without assigning (return), and adding it did not change anything. As a precaution, I also tried removing the clear_inventory() function, but I couldn't prevent the inventory from resetting upon program termination.
Additionally, I attempted to replace it with a 'persistent' object, and I successfully prevented the inventory items from disappearing upon program reconnection. However, this approach maintains the inventory even when loading other save files, making it challenging to consider this issue resolved. It still feels like a maze. Any insights would be greatly appreciated
fgfg3356 wrote: ↑Sun Jan 14, 2024 11:43 am
However, this approach maintains the inventory even when loading other save files, making it challenging to consider this issue resolved. It still feels like a maze. Any insights would be greatly appreciated
That's why I don't use classes, you need to store that list/var somehow that renpy will save it to the save-file, but i don't know how. Sorry again.
Unless top level reference changes this will be marked as constant by RenPy savesystem and will not be saved.
Everything you want to save should be declared using default statement, unless you have an extensive knowledge of RenPy save system and can make it save some other way.
fgfg3356 wrote: ↑Sun Jan 14, 2024 11:43 am
However, this approach maintains the inventory even when loading other save files, making it challenging to consider this issue resolved. It still feels like a maze. Any insights would be greatly appreciated
That's why I don't use classes, you need to store that list/var somehow that renpy will save it to the save-file, but i don't know how. Sorry again.
That's great to hear! I finally solved the problem. Thank you for your interest in my issue, and I wish you many more good days ahead
Unless top level reference changes this will be marked as constant by RenPy savesystem and will not be saved.
Everything you want to save should be declared using default statement, unless you have an extensive knowledge of RenPy save system and can make it save some other way.
I'm thrilled beyond words. It seemed like an endless problem, but it disappeared so simply... I can't express enough gratitude to you. Defining default in screen.rpy solved everything. Thank you so much!