[SOLVED] Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

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.
Post Reply
Message
Author
fgfg3356
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

[SOLVED] Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#1 Post by fgfg3356 »

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.

Code: Select all

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

Code: Select all

    $ item_chemical = clue("chemical", "chemical.png")
    $ item_camera = clue("camera", "camera.png") 
Function to Call When Retrieving Clues During Label Progress:
$ add_clue(item_camera)
Last edited by fgfg3356 on Sun Jan 21, 2024 4:22 am, edited 1 time in total.

Another_Tom
Regular
Posts: 61
Joined: Mon Apr 24, 2023 9:06 am
Completed: Harold And The Witches
Projects: Steven On The Run
itch: dantom
Location: Germany
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#2 Post by Another_Tom »

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?


Code: Select all

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.

fgfg3356
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#3 Post by fgfg3356 »

- Mistake comments -
Last edited by fgfg3356 on Sun Jan 14, 2024 9:59 am, edited 1 time in total.

fgfg3356
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#4 Post by fgfg3356 »

Another_Tom wrote: Sun Jan 14, 2024 9:03 am
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?


Code: Select all

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?

Another_Tom
Regular
Posts: 61
Joined: Mon Apr 24, 2023 9:06 am
Completed: Harold And The Witches
Projects: Steven On The Run
itch: dantom
Location: Germany
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#5 Post by Another_Tom »

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?

Code: Select all

    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
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#6 Post by fgfg3356 »

Another_Tom wrote: Sun Jan 14, 2024 11:06 am
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?

Code: Select all

    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

Another_Tom
Regular
Posts: 61
Joined: Mon Apr 24, 2023 9:06 am
Completed: Harold And The Witches
Projects: Steven On The Run
itch: dantom
Location: Germany
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#7 Post by Another_Tom »

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.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2407
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#8 Post by Ocelot »

Code: Select all

init python:
    my_clue = []
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.
< < insert Rick Cook quote here > >

fgfg3356
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#9 Post by fgfg3356 »

Another_Tom wrote: Sun Jan 14, 2024 11:49 am
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 :)

fgfg3356
Newbie
Posts: 15
Joined: Fri Jul 02, 2021 5:36 am
Contact:

Re: Implementation of Inventory Functionality, but Facing Reset Issue upon Program Reconnection

#10 Post by fgfg3356 »

Ocelot wrote: Sun Jan 14, 2024 1:13 pm

Code: Select all

init python:
    my_clue = []
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!

Post Reply

Who is online

Users browsing this forum: Google [Bot]