Page 1 of 1

Persistent Data not saving after game closes[solved]

Posted: Mon Jul 08, 2013 1:05 pm
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? :/

Re: Persistent Data not saving after game closes

Posted: Mon Jul 08, 2013 1:17 pm
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)

Re: Persistent Data not saving after game closes

Posted: Mon Jul 08, 2013 2:03 pm
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)

Re: Persistent Data not saving after game closes

Posted: Mon Jul 08, 2013 2:07 pm
by pwisaguacate
Also, unlike normal variables, condition statements don't seem to mind if persistent variables are "nonexistent".

Re: Persistent Data not saving after game closes

Posted: Mon Jul 08, 2013 2:08 pm
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.

Re: Persistent Data not saving after game closes

Posted: Tue Jul 09, 2013 7:04 pm
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.

Re: Persistent Data not saving after game closes

Posted: Tue Jul 09, 2013 9:10 pm
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