Page 1 of 1

help! values aren't preserving

Posted: Mon Apr 11, 2011 10:56 pm
by bozance
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...

Re: help! values aren't preserving

Posted: Mon Apr 11, 2011 11:05 pm
by bozance
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.

Re: help! values aren't preserving

Posted: Mon Apr 11, 2011 11:07 pm
by PyTom
You need to run

Code: Select all

$ mp.save()
To save multipersistent data to disk.

Re: help! values aren't preserving

Posted: Mon Apr 11, 2011 11:25 pm
by bozance
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.

Re: help! values aren't preserving

Posted: Mon Apr 11, 2011 11:55 pm
by bozance
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?

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 12:03 am
by PyTom
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.

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 12:11 am
by bozance
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.

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 9:52 am
by bozance
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.

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 3:19 pm
by Aleema
How are you initializing the variable?

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 4:41 pm
by bozance
I'm just putting the variables in like

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

Along with other regular variables, in the same way.

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 6:27 pm
by Aleema
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*

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 9:28 pm
by bozance
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.

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 9:30 pm
by Aleema
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.

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 9:46 pm
by bozance
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?

Re: help! values aren't preserving

Posted: Tue Apr 12, 2011 10:04 pm
by bozance
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...