Page 1 of 1

Save games

Posted: Sat Jan 20, 2018 5:48 am
by Thundy
Good morning all.

Im having some issues getting my head around save games. ive read a bit of documentation but the penny hasnt dropped yet.

I understand that in order to keep save files small and save time short, the game only saves variables whose value have changed since the game loaded.
I also understand that the default command was added to allow coders to tell renpy "if no other value is assigned to this variable, use this value"

I am having some issues getting my head around implementation though. In my mind, all of my variables are important, it's why i declared them in the first place, so when I load a game and S**t is going mental because half of the values the game needs are no longer defined it sorta throws me off.

I declare all of my variables and their starting values in one function which Is the first thing called in the start()
The function defines a few classes and then declares all the variables with their default values of which there are currently only about 40 but i need the values of all of these variables to be saved so that when the game loads, it is in exactly the same state as it was when it was saved otherwise there isn't much point saving.

Anyone with a good handle on this able to break it down a bit and maybe help explain what I need to do?

Much obliged :)

Re: Save games

Posted: Sat Jan 20, 2018 9:59 am
by Empish
If you're declaring a variable in a function I believe that due to scoping it will only exist in that function.

Try declaring your variables themselves at the top level of a file, that way they can all be found at the appropriate time.

Re: Save games

Posted: Sat Jan 20, 2018 11:00 am
by Remix
You could map the variable to a collection (python datatype) and just default that...

default var_map = {} # this gets saved / loaded

function something(): global var_map ... var_map[ 'pc_health' ] = var_map.get( 'pc_health', 50 ) # default 50, else saved value
# just remember to only init a value if it doesn't exist, like above

Re: Save games

Posted: Sun Jan 21, 2018 4:56 am
by Thundy
Ive done some more digging and i realise that I am still using older terminology. The variables are defined inside a call function i.e

Code: Select all

    call declarations

label declarations:
        class NPC(object):
            def __init__(self, name, affection, lust, corruption, location, ispresent, job, cash, intro, status, home):
       = name
                self.affection = affection
                self.lust = lust
                self.corruption = corruption
                self.location = location
                self.ispresent = ispresent
                self.job = job
       = cash
                self.intro = intro
                self.status = status
                self.home = home
  $ weekday = 0
    $ totaldays = 0
    $ day_values = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")    
    $ day_time = 16

as an example

what is happening is that when the player tries to load their saved game some of the variables are "not defined"

Re: Save games

Posted: Sun Jan 21, 2018 7:33 am
by Remix
default var = value
# outside any label or control flow block

is the advised way to set defaults that are saved (Ren'py basically uses the default if the value does not already exist on any load)

You could either rework the code to use that (like suggested above) or directly import the store and address that within __setattr__ and __getattr__ for your object.
Also: Personally I'd put the class within an "init python:" block rather than the start:->call label->declare route, it will still be available for instances just the same.

Re: Save games

Posted: Mon Jan 22, 2018 3:14 am
by Thundy
awesome thanks for the tip. Ive moved the class definitions into the init python block and it seems to have resolved my issue. As always I am really grateful for the help!