Persistent Data not saving after game closes[solved]

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
MissLanna
Regular
Posts: 52
Joined: Fri Feb 08, 2013 10:58 am
Contact:

Persistent Data not saving after game closes[solved]

#1 Post by MissLanna »

Gah, I was SO looking forward to not having to make ANOTHER thread asking for help in here for awhile. Oh well.

So I've got my very own CG gallery to work and it is wonderful. I have the first image locked when you open the game, then when you hit start after some dialogue the image is unlocked. Then you save the game, exit said game to the main menu, and peek into the cg gallery and there it is! Unlocked, accessible, just the way I like it.

Then I close the launcher, re-open it, and bam, it's locked again. I don't know if I'm using persistent data correctly or not, but I thought I was seeing as how it seems to save as I want it to up until the launcher closes...

Any ideas?

This is my code (and I'm sure it's terribly inelegant but it functions and I understand it, so...) we're looking at the img01 variable in particular.

In Screens:

Code: Select all

# CG Gallery

init: 
    $ gal_page = 1 #the page you begin on
  
    
    #page 1

    $ persistent.galimg01 = False
    $ galimg02 = False
    $ galimg03 = False
    ETC. ....

....

if(gal_page == 1): #check to see what page to show
            if(persistent.galimg01): #check to see if we should show the image or not
                 imagemap:
                     
                     ground "gui/gallery/thumbnail/thumb01.png" #ground and idle can be the same
                     idle "gui/gallery/thumbnail/thumb01.png"
                     hover "gui/gallery/thumbnail/thumb01hov.png"
                     cache False
                     
                     hotspot (32,170,200,201) action (SetVariable("star_image", gal1), ShowMenu("galleryfs")) activate_sound "sfx/menu/select.mp3" hover_sound "sfx/menu/hover.mp3"


In in the script:

Code: Select all

mc "Let's see if I can unlock an image in the image gallery now."
$ persistent.galimg01 = True
mc "Okay! Let's see how this works!"
Any ideas, O kind and helpful and knowledgeable code masters? :/
Last edited by MissLanna on Tue Jul 09, 2013 7:04 pm, edited 1 time in total.

Ryue
Miko-Class Veteran
Posts: 745
Joined: Fri Nov 02, 2012 8:41 am
Projects: Red eyes in the darkness
Contact:

Re: Persistent Data not saving after game closes

#2 Post by Ryue »

The problem can be because you are initializing variables in the init area.
thus:

Code: Select all

   $ gal_page = 1 #the page you begin on
 
   
    #page 1

    $ persistent.galimg01 = False
    $ galimg02 = False
    $ galimg03 = False
Should be put just after the start label (had some discussions there myself and what you describe as phenomenon is exactly what I was told would happen when I initialize
variables in the init block)

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: Persistent Data not saving after game closes

#3 Post by Alex »

Code: Select all

$ persistent.galimg01 = False
This line changes your variable everytime you start the game.

Should be

Code: Select all

if not persistent.galimg01:
    $ persistent.galimg01 = False
(will set the variable only if it doesn't have any value)

pwisaguacate
Veteran
Posts: 356
Joined: Mon Mar 11, 2013 11:03 pm
Contact:

Re: Persistent Data not saving after game closes

#4 Post by pwisaguacate »

Also, unlike normal variables, condition statements don't seem to mind if persistent variables are "nonexistent".
Last edited by pwisaguacate on Mon Jul 08, 2013 2:09 pm, edited 2 times in total.

Anima
Veteran
Posts: 448
Joined: Wed Nov 18, 2009 11:17 am
Completed: Loren
Projects: PS2
Location: Germany
Contact:

Re: Persistent Data not saving after game closes

#5 Post by Anima »

Actually you don't need to initialize persistent variables at all. If the variable is not defined the persistent object always returns a False value.
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase III

MissLanna
Regular
Posts: 52
Joined: Fri Feb 08, 2013 10:58 am
Contact:

Re: Persistent Data not saving after game closes

#6 Post by MissLanna »

Thank you everyone! I got it to work. This is my new code. Moving the gal_page default number under the start made it not work, but just moving the persistent.galiamgewhatever variables underneath it did.


Under the start label in my script:

Code: Select all

#page 1

$ persistent.galimg01 = False
$ persistent.galimg02 = False

...
etc.


In screens

Code: Select all

init: 
    $ gal_page = 1 #the page you begin on
  

screen gallery:
    modal True
    tag menu
...
etc.

RangerDanger
Regular
Posts: 45
Joined: Wed Jun 27, 2012 11:36 pm
Contact:

Re: Persistent Data not saving after game closes

#7 Post by RangerDanger »

Anima wrote:Actually you don't need to initialize persistent variables at all. If the variable is not defined the persistent object always returns a False value.
This isn't totally true. If a persistent value is not defined it would be None, not False.

You can test this with this simple script

Code: Select all

label start:
    if persistent.not_def is None:
        "Is none."

    if persistent.not_def == False:
        "Is equal to false."

    return
Though None is a falsy value in Python, so using it in a context such as

Code: Select all

if not persistent.not_def:
    print "This is falsy"
is perfectly fine. But it is always good to be aware that None != False

Post Reply

Who is online

Users browsing this forum: elcharlo