help! values aren't preserving

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
bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

help! values aren't preserving

#1 Post by bozance » Mon Apr 11, 2011 10:56 pm

I have a game where experience points go up sequentially. I tested it and have $mp.xp for the experience points. When I get a few xp, save, exit, and then load the game, the xp count resets to 0!

What am I doing wrong? Everything works as long as I stay in the same session, but once I quit and load my progress, it's all back to 0!!

Any help would be appreciated...

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#2 Post by bozance » Mon Apr 11, 2011 11:05 pm

Upon further testing I'm guessing that multipersistent variables don't really preserve like normal variables. I tried making the variable non-mp and it did preserve the correct numbering.

User avatar
PyTom
Ren'Py Creator
Posts: 15454
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: help! values aren't preserving

#3 Post by PyTom » Mon Apr 11, 2011 11:07 pm

You need to run

Code: Select all

$ mp.save()
To save multipersistent data to disk.
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

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#4 Post by bozance » Mon Apr 11, 2011 11:25 pm

Tried it but no dice... I must not know what I'm doing.

$mp.xp+=1
$mp.save()
show screen stats1

I also put the save after "show screen stats" but it still resets to 0 on load.

Bear in mind that I don't really know what I'm doing beyond simple jump and call statements.

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#5 Post by bozance » Mon Apr 11, 2011 11:55 pm

I guess I should just be using the $mp.save() for persistence of data from one game to another. That does work. For persistence of variable values within one game, I'll stick with regular variables.

Does that make sense?

User avatar
PyTom
Ren'Py Creator
Posts: 15454
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: help! values aren't preserving

#6 Post by PyTom » Tue Apr 12, 2011 12:03 am

Well, what are you trying to use here. There's three levels of saving Ren'Py supports:

- Normal variables are specific to a single session of a game, including saves and loads of that session.

- Persistent variables are specific to a single project.

- Multipersistent varables are shared between projects.

From what you describe, it seems like you might want a persistent variable. It's hard to tell.
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

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#7 Post by bozance » Tue Apr 12, 2011 12:11 am

Hmm... I admit I don't know about persistent variables.

I'm interested in normal variables but also in making those normal variables persistent when the user has completed the game. Upon opening another project, a completely different game/sequel, those normal variables will have multi-persisted.

I thought that I could just put "mp." in front of all my variables and that would do the trick, but I think I oversimplified it.

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#8 Post by bozance » Tue Apr 12, 2011 9:52 am

To elaborate: I want a character's experience points to accumulate throughout a game, and then carry over to the next game. If I use $mp.xp, the xp value does not save in between the time I save and then load a game. A normal variable like $xp does the trick.

So I think if I want the xp to carry over, I use $xp throughout the game and then associate it with $mp.xp at the end of the game so that it carries over to another project.

If anyone can confirm this is or isn't a terrible idea, please let me know.

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: help! values aren't preserving

#9 Post by Aleema » Tue Apr 12, 2011 3:19 pm

How are you initializing the variable?

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#10 Post by bozance » Tue Apr 12, 2011 4:41 pm

I'm just putting the variables in like

$mp.xp=0
$mp.xpmax=200

Along with other regular variables, in the same way.

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: help! values aren't preserving

#11 Post by Aleema » Tue Apr 12, 2011 6:27 pm

Well, in order for persistent variables to work, you need to make sure they're only initialized once. For instance, this is how you would initialize a persistent variable:

Code: Select all

if persistent.seen_opening is None:
        $ persistent.seen_opening = False
Only if nothing is in your variable, will it set itself to False. It appears that you didn't do something like this, so every time Ren'Py inits (which is every instance), it will go through and see that it needs to be 0 again.

Also, where you initialize is important, I've found. Make sure all regular variables are initialized after the Start label or they won't be saved with that particular playthrough. *found out the hard way*

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#12 Post by bozance » Tue Apr 12, 2011 9:28 pm

Hmmm... I declare regular variables before Start and they seem to save/load normally?

As for something like $mp.xp... do you mean I would declare $mp.xp=0 before "Start" and then after "Start" do something like

if mp.xp = 0:
$ mp.xp =

I don't even know what I would put in there. Sorry to be an idiot.

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: help! values aren't preserving

#13 Post by Aleema » Tue Apr 12, 2011 9:30 pm

Okay, ignore what I said about the Start label, that was some bonus info. What I recommend you do is, in an init block:

Code: Select all

if mp.xp is None:
    $ mp.xp = 0
Try that? I've never used multipersistent before, but logically, that makes sense.

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#14 Post by bozance » Tue Apr 12, 2011 9:46 pm

Interesting... it makes the game start with the existing (non-zero) xp amount, even when I delete persistent.

I'm thinking I can make it all work with regular variables and just substitute in some $mp. variables at the end of the game? Couldn't I just have $xp=100 and then say $mp.xp=xp in the final label of the game?

bozance
Regular
Posts: 61
Joined: Thu Feb 12, 2009 12:19 am
Contact:

Re: help! values aren't preserving

#15 Post by bozance » Tue Apr 12, 2011 10:04 pm

Okay, scratch that. I had some extra test variables in there that were screwing things up.

If I use "if mp.xp is None..." the game saves with some additional xp that shouldn't be there; if I don't, it resets to zero.

Back to the drawing board I guess. I appreciate your help...

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], strayerror