Characterstats class and data persistence

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
JuicyEliot
Newbie
Posts: 6
Joined: Wed Sep 23, 2020 8:04 am
Contact:

Characterstats class and data persistence

#1 Post by JuicyEliot » Wed Sep 23, 2020 8:57 am

Hi !

I'm new to Ren'py and i'm really sorry if the answer of my question is very obvious. I tried to find answers on several previous post, documentation and youtube tutorials but can't get my answer. Do not hesitate to redirect me on another existing topic.

Here is the thing. I'd like to create a stats system. Basically this system will store points of love, trust or lust the NPC will have toward the player character.

So I tried to create a class like this:

Code: Select all

init python:
    class CharacterStats(object):
        def __init__(self, mood, trust, love, lust):
            self.mood = mood
            self.trust = trust
            self.love = love
            self.lust = lust
Then, when declaring my first NPC "Nina" I wrote this :

Code: Select all

define character.nina = DynamicCharacter("nina_name", color="#F4B1F9", who_xpos = 260)
define nina = CharacterStats("Neutral", 0, 0, 0)
Edit : I made all of this juste before the label start

Then, when talking to Nina, I incremented trust points like this :

Code: Select all

menu:
        "I'm so sorry for your loss !":
            e "Poor thing ! I'm sure she's really proud of you from where she is..."
            $ nina.trust += 2
Or even outside of dialog menu like this:

Code: Select all

nina "I don't know how to thank you !"
$ nina.trust += 3
But if I understand the documentation and several posts I read, Ren'py will not save those variables the way I declared it.
In deed, using the debug prompt using shif + O and typing nina.trust after loading a previous save when nina's trust was 2 for exemple, it turns 0 again when re-loading the game.
Same question for rollingback, when I increment 2 to nina's trust, then rollback before incrementing, it stays 2. I'd like the variable to decrement 2 when rollingback. I've read the https://www.renpy.org/doc/html/save_load_rollback.html page but it still a bit blurry :(

Maybe I'm doing all of these all wrong ? Again, I'm really sorry, my questions must be very stupid and easy to solve :/

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Characterstats class and data persistence

#2 Post by Remix » Wed Sep 23, 2020 9:32 am

define will create a value for a variable and (because it presumes that value will be static) sets it as non saved.
default on the other hand presumes the value may change so adds it immediately to the store (each checkpoint/save stores its value)

So, you want:

Code: Select all

# define 
# here we use define, as Character objects do not store their internal state (though they can contain dynamic elements like [name])
#
define character.nina = Character("[nina_name]", color="#F4B1F9", who_xpos = 260)

# default
# this one we want to save when it changes, so default it is
#
default nina = CharacterStats("Neutral", 0, 0, 0)
Frameworks & Scriptlets:

JuicyEliot
Newbie
Posts: 6
Joined: Wed Sep 23, 2020 8:04 am
Contact:

Re: Characterstats class and data persistence

#3 Post by JuicyEliot » Wed Sep 23, 2020 9:54 am

Ok, just test it. That is awesome, solving my 2 problems of persistence and rolling back ! Thank you so much Remix :)

Post Reply

Who is online

Users browsing this forum: Bing [Bot]