Save games

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
Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Save games

#1 Post 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 :)

User avatar
Empish
Veteran
Posts: 221
Joined: Thu Jan 14, 2016 9:52 pm
Projects: Efemural Hearts, It Ends With Graduation
itch: empish
Contact:

Re: Save games

#2 Post 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.

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: Save games

#3 Post 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
Frameworks & Scriptlets:

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Save games

#4 Post 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

start:
    call declarations
return

label declarations:
       python:
        class NPC(object):
            def __init__(self, name, affection, lust, corruption, location, ispresent, job, cash, intro, status, home):
                self.name = name
                self.affection = affection
                self.lust = lust
                self.corruption = corruption
                self.location = location
                self.ispresent = ispresent
                self.job = job
                self.cash = 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
   
return

as an example

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

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: Save games

#5 Post 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.
Frameworks & Scriptlets:

Thundy
Regular
Posts: 88
Joined: Tue Dec 05, 2017 9:08 am
Contact:

Re: Save games

#6 Post 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!

Post Reply

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot]