Renpy not remembering value of save_name variable

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
User avatar
Dreadwing93
Regular
Posts: 30
Joined: Mon May 20, 2013 1:44 am
Projects: 0xE125: Crimson Spiral
Contact:

Renpy not remembering value of save_name variable

#1 Post by Dreadwing93 » Thu May 08, 2014 1:40 am

So I've been trying to make a system where when saving the game it will ask you what to name the save file, and I've got it working pretty nicely, but I've run into a bit of a problem.

I have it set so that the current save name will be the default value for the input prompt.
When I save the game and then check the variable viewer, the save_name variable has been changed to the correct value.

However, when I save the game, then exit and load back into the game, the save_name variable is back to what it was before I saved.

Here's what I have:

An alternative input screen. This one is modal.

Code: Select all

screen modal_input:
    modal True
    window style "input_window":
        has vbox

        text prompt style "input_prompt"
        input id "input" style "input_text" default default
This is the action I'm using for saving and loading

Code: Select all

action If(renpy.current_screen().screen_name[0] == "load",FileLoad(i),get_save_name(i))
And finally the important part

Code: Select all

init -2 python:
    class get_save_name(FileSave):
        def __init__(self, name, confirm=True, newest=True, page=None, cycle=False):
            super(get_save_name,self).__init__(name=name,confirm=confirm,newest=newest,page=page,cycle=cycle)
        def __call__(self):
            renpy.call_in_new_context("get_save_name")
            return super(get_save_name,self).__call__()

label get_save_name:
    show screen save
    $ save_name = renpy.call_screen("modal_input", prompt="Enter save name", default=save_name, length=20)
    return
I think I know why it's not working. I think it's not remembering the value I set it to because I'm setting it from a screen action.

Is there a way to get renpy to remember the value of save_name after I set it or am I just going to have to deal without a default value?

edit:

I've discovered something new.
If I save the game, progress a little in the game, and then save again, then the save_name gets saved properly.

So Renpy doesn't realize that the variable changed until you continue into the game. If you save and then quit immediately, then the save_name variable won't be saved. Is there any way to fix this?
Image

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Renpy not remembering value of save_name variable

#2 Post by PyTom » Thu May 08, 2014 11:20 am

So the problem here is that a load causes the equivalent of a rollback to the start of the current statement to occur. This will change the contents of variables, including the save_name variable.

To work around this, call renpy.retain_after_load() after changing save_name. (But realize that this can have side-effects that may be unexpected.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
Dreadwing93
Regular
Posts: 30
Joined: Mon May 20, 2013 1:44 am
Projects: 0xE125: Crimson Spiral
Contact:

Re: Renpy not remembering value of save_name variable

#3 Post by Dreadwing93 » Thu May 08, 2014 12:32 pm

After making this change:

Code: Select all

label get_save_name:
    show screen save
    $ save_name = renpy.call_screen("modal_input", prompt="Enter save name", default=save_name, length=20)
    $ renpy.retain_after_load()
    return
I get this error upon trying to save:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/screens.rpy", line 327, in script
  File "game/screens.rpy", line 327, in python
AttributeError: 'module' object has no attribute 'retain_after_load'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "/home/dreadwing93/renpy/renpy/execution.py", line 288, in run
    node.execute()
  File "/home/dreadwing93/renpy/renpy/ast.py", line 720, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "/home/dreadwing93/renpy/renpy/python.py", line 1304, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/screens.rpy", line 327, in <module>
    $ renpy.retain_after_load()
AttributeError: 'module' object has no attribute 'retain_after_load'

Linux-3.13.0-24-generic-i686-with-debian-jessie-sid
Ren'Py 6.16.3.502
0xE125: CS 0.0
Image

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Renpy not remembering value of save_name variable

#4 Post by PyTom » Thu May 08, 2014 12:57 pm

I think it might be new in 6.17.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
Dreadwing93
Regular
Posts: 30
Joined: Mon May 20, 2013 1:44 am
Projects: 0xE125: Crimson Spiral
Contact:

Re: Renpy not remembering value of save_name variable

#5 Post by Dreadwing93 » Thu May 08, 2014 1:02 pm

I updated to the newest version of Renpy and now it works!
Thanks PyTom!
Image

Post Reply

Who is online

Users browsing this forum: _ticlock_