[Solved] Game doesn't save dictionary changes.

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
bloodzy
Newbie
Posts: 2
Joined: Thu Jan 21, 2021 12:50 am
Projects: My Kingdom
Contact:

[Solved] Game doesn't save dictionary changes.

#1 Post by bloodzy »

Hello, this is my first post, sorry for my English, it is not my native language.
My problem is the following, I have a dictionary to save some characteristics of the characters, everything works fine, except that when I change some value of the dictionary within the game, I save the game progress after modifying the value, I close the game, I open it , I load the progress, the dictionary value returns to its default value, it does not save the change that had been made previously, is that understood? Here I leave the code if someone can help me, it would be excellent, or guide me in the right direction on how to save the progress and changes made.

char_vars.rpy

Code: Select all

init python:
    # Defines the information for each character's images and story path
    characters = {

        #Sophie
        "sophie": {
            "locked_image": "sophie_locked.png",
            "idle_image"  : "sophie_idle.png",
            "hover_image" : "sophie_hover.png",
            "story_label" : "sophie_story",
            "tooltip"     : "Sophie",
            "tooltip_lock": "This player is locked",
            "unlocked"    : True
        },

        #Mia
        "mia": {
            "locked_image": "mia_locked.png",
            "idle_image"  : "mia_idle.png",
            "hover_image" : "mia_hover.png",
            "story_label" : "mia_story",
            "tooltip"     : "Mia",
            "tooltip_lock": "This player is locked",
            "unlocked"    : False
        },
    }
The problem is here, it doesn't change the default "unlocked" value from False to True, or to be more precise, it does change it, but it doesn't save the change after restarting the game.

character_selection.rpy [To show characters availables and unlocked.]

Code: Select all

# Screen to switch between character stories.
screen character_selection():
    add "background.png" 

    frame:
        background None
        align (0.0, 0.0)
        xminimum (1920)
        yminimum (1080)
        
        # Left navigation arrow
        frame:
            background None
            align (0.05, 0.5)
            if character_index > 0:
                imagebutton at Transform(zoom=0.5):
                    auto "images/arrow_left_%s.png"
                    action SetVariable("character_index", character_index - 1)

        # Character image
        $ char_id, char_info = list(characters.items())[character_index]
        $ image_to_display = char_info["idle_image"] if char_info["unlocked"] else char_info["locked_image"]
        $ hover_image = char_info["hover_image"] if char_info["unlocked"] else image_to_display
        $ tt = char_info["tooltip"] if char_info["unlocked"] else char_info["tooltip_lock"]
        $ action = Jump(char_info["story_label"]) if char_info["unlocked"] else NullAction()
        
        # Frame for Character image
        frame:
            background None
            align (0.5, 1.0)
            imagebutton at test_transform:
                idle "images/" + image_to_display
                hover "images/" + hover_image
                tooltip tt
                action action
                xalign 0.3
                yalign 1.0

        # Right navigation arrow.
        frame:
            background None
            align (0.95, 0.5)
            if character_index < len(characters) - 1:
                imagebutton at Transform(zoom=0.5):
                    auto "images/arrow_right_%s.png"
                    action SetVariable("character_index", character_index + 1)

        # ImageButton for quit the game.
        frame:
            background None
            xalign 1.0
            yalign 0.0
            imagebutton:
                auto "images/quit_%s.png"
                action MainMenu()

        # Tooltip Message
        $ tooltip = GetTooltip()
        if tooltip:
            nearrect:
                focus "tooltip"
                prefer_top True
                frame:
                    style "frame_style"
                    text tt

# character_selection styles and transforms
style frame_style:
    background "#0000009a"
    padding (20, 25, 20, 15)
    xalign 0.5

transform test_transform:
    on idle:
        easein 0.5 zoom 1.0
    on hover:
        easein 0.5 zoom 1.05
script.rpy

Code: Select all

default persistent.dialogueBoxOpacity = 1.0
image white = Solid("#fff")
# Label for start the game and show the screen with characters selection and their story.
label start:
    scene white
    "Test"
    call screen character_selection
    return

# Labels for character stories.
label sophie_story:
    $ characters["mia"]["unlocked"] = True # unlock Mia
    "Unlock Mia's Story..."
    jump start

label mia_story:
    "Test the game"
    jump start
Sorry for bothering you and thanks in advance.
Last edited by bloodzy on Wed Apr 10, 2024 11:00 am, edited 1 time in total.

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

Re: [HELP] Game doesn't save dictionary changes.

#2 Post by Ocelot »

https://www.renpy.org/doc/html/save_load_rollback.html
The Python state consists of the variables in the store that have changed since the game began, and all objects reachable from those variables. Note that it's the change to the variables that matters – changes to fields in objects will not cause those objects to be saved.

Variables set using the default statement will always be saved.
If you want something to save, declare it using default statement.
< < insert Rick Cook quote here > >

bloodzy
Newbie
Posts: 2
Joined: Thu Jan 21, 2021 12:50 am
Projects: My Kingdom
Contact:

Re: [HELP] Game doesn't save dictionary changes.

#3 Post by bloodzy »

Ocelot wrote: Wed Apr 10, 2024 8:52 am https://www.renpy.org/doc/html/save_load_rollback.html
The Python state consists of the variables in the store that have changed since the game began, and all objects reachable from those variables. Note that it's the change to the variables that matters – changes to fields in objects will not cause those objects to be saved.

Variables set using the default statement will always be saved.
If you want something to save, declare it using default statement.
Thanks for your answer, I was trying to define the unlocked variables outside the dictionary but it always gave me an error, in the end the solution was to remove "unlocked" from the dictionary, place it outside and modify the screen so that it takes the values from the dictionary, and the variable from another place.

I share the modification in case anyone needs it.

char_vars.rpy

Code: Select all

init python:
    # Defines the information for each character's images and story path
    characters = {

        #Sophie
        "sophie": {
            "locked_image": "sophie_locked.png",
            "idle_image"  : "sophie_idle.png",
            "hover_image" : "sophie_hover.png",
            "story_label" : "sophie_story",
            "tooltip"     : "SophieTooltip",
            "tooltip_lock": "This player is locked",
        },

        #Mia
        "mia": {
            "locked_image": "mia_locked.png",
            "idle_image"  : "mia_idle.png",
            "hover_image" : "mia_hover.png",
            "story_label" : "mia_story",
            "tooltip"     : "Mia Tooltip",
            "tooltip_lock": "This player is locked",
            
        },
}

# This is the default index for the character currently being viewed.
default character_index = 0
default sophie_unlocked = True
default mia_unlocked = False
character_selection.rpy

Code: Select all

# Character image and navigation buttons
        frame:
            background None
            align (0.5, 1.0)
            $ char_name = list(characters.keys())[character_index]
            $ char_info = characters[char_name]
            $ image_to_display = char_info["idle_image"] if globals()["{}_unlocked".format(char_name)] else char_info["locked_image"]
            $ hover_image = char_info["hover_image"] if globals()["{}_unlocked".format(char_name)] else image_to_display
            $ tt = char_info["tooltip"] if globals()["{}_unlocked".format(char_name)] else char_info["tooltip_lock"]
            $ action = Jump(char_info["story_label"]) if globals()["{}_unlocked".format(char_name)] else NullAction()
            imagebutton at arrows:
                idle "images/" + image_to_display
                hover "images/" + hover_image
                tooltip tt
                action action

Post Reply

Who is online

Users browsing this forum: Bing [Bot]