Saving variables, how to do right?

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
Nikola
Newbie
Posts: 4
Joined: Thu Sep 01, 2011 6:13 am
Contact:

Saving variables, how to do right?

#1 Post by Nikola »

How do you save variable? If during Test screen below i change Show/Colors by clicking buttons, then save, then load, i get initial settings (false, white text).
Checked wiki, searched forum, feeling blind/dumb.

And, just in case, any more not just saving/loading errors/better ways for code below?

Code: Select all

init python:
  ShowLabels=False
  LabelsColors=["#FFF","#FFF","#FFF"]

  def toggle_labels():
    global ShowLabels
    ShowLabels=not ShowLabels
    renpy.restart_interaction()

  def colorize_label(ID):
    global LabelsColors
    LabelsColors[ID]=(renpy.random.randint(0,255),renpy.random.randint(0,255),renpy.random.randint(0,255),255)
    renpy.restart_interaction()
  Colorize=renpy.curry(colorize_label)

screen Test:
  frame:
    vbox:
      text "Labels([ShowLabels])"
      textbutton "Show/Hide" action toggle_labels
      if ShowLabels:
        hbox:
          textbutton "C" action Colorize(0)
          text "Label One" color LabelsColors[0]
        hbox:
          textbutton "C" action Colorize(1)
          text "Label Two" color LabelsColors[1]
        hbox:
          textbutton "C" action Colorize(2)
          text "Label Three" color LabelsColors[2]
      textbutton "Done" action Return("OK")

label start:
  "Started"
  call screen Test
  "Done [_return]"
  return

Nikola
Newbie
Posts: 4
Joined: Thu Sep 01, 2011 6:13 am
Contact:

Re: Saving variables, how to do right?

#2 Post by Nikola »

All saved/loaded fine now, but not while inside Test screen.
How to change code so if i set counter to 5, then rollback one step i get to counter = 4 state, not "Started"? I guess this will solve save/load problem too.

Code: Select all

label InitVariables:
  python:
    Counter=0
  return

init python:
  def IncCounter():
    global Counter
    Counter+=1
    renpy.restart_interaction()

  def DecCounter():
    global Counter
    if Counter>=1:
      Counter-=1
    renpy.restart_interaction()

screen Test:
  frame:
    vbox:
      text "Counter: [Counter]"
      hbox:
        textbutton "Inc" action IncCounter
        textbutton "Dec" action DecCounter
      textbutton "Done" action Return("OK")

label start:
  call InitVariables
  "Started"
  call screen Test
  "Done [_return]"
  return

Keul
Regular
Posts: 49
Joined: Wed May 06, 2009 3:42 am
Location: France - Strasbourg
Contact:

Re: Saving variables, how to do right?

#3 Post by Keul »

I think that first, it would be great to explain what is the goal you are searching for (do you want to change the style of something, like the color of the text during a user's choice ingame, for example, user white or red, accordinf to the character the player have choose, or is it more like you want to change the background depending of user-preference)

Next, you have to understand how renpy works and save data.

Renpy have:
- fixed data (your default font, background, ...) that never change
- persistent data (witch picture in the gallery, or music in the music room is unlocked, the selected language, preference detail like volume, ...), that will stay the same even if you load an other savegame
- savegame related data: the choice done by a user during game, that is different and recorded in savegame, knowing that user can make different saves, using slots.

If you want to make a skin that is selected in the preference/option panel, you should save it using persistent data (http://www.renpy.org/wiki/renpy/doc/ref ... stent_Data )
If you want the skin to change depending on user choice for example, you will have to save it using local variable (http://www.renpy.org/wiki/renpy/doc/tut ... er_Choices ), and declare that variable in the begening of the game (after label start:). Don't forget that local variables must be declared after "label start:" and not in "init:" block

Keul
Regular
Posts: 49
Joined: Wed May 06, 2009 3:42 am
Location: France - Strasbourg
Contact:

Re: Saving variables, how to do right?

#4 Post by Keul »

For your second message:
1) You can replace "call InitVariables" with "$ Counter=0"
2)

Code: Select all

"Started"
  call screen Test
  "Done [_return]"
Renpy rollback trough what's defined in label status. not for each action() defined in your "screen Test".

Nikola
Newbie
Posts: 4
Joined: Thu Sep 01, 2011 6:13 am
Contact:

Re: Saving variables, how to do right?

#5 Post by Nikola »

>You can replace "call InitVariables" with "$ Counter=0"

I was aware of it, just like structured way, anyway there will be more than just one variable and i rather have them at separate file.

My goal right now is:
- call screen
- change something there, using ui controls
- save/load/rollback/rollforward during called screen
- return from screen to start label

Right now i get this:
-call Test screen
-inc/dec Counter to 57
-save game
-i go to save dir, unpack save file, hex edit it and see store.Counter=57, so it save value
-but when i load game i get Counter=0

>Renpy rollback trough what's defined in label status. not for each action() defined in your "screen Test".

This is my issue i guess. And no way to force renpy put breaks/checkpoints at specific moments? If i make somewhat long minigame and want user to be able to save their progress without exiting minigame, i just can't?

Sorry if my questions sounds dumb, i'm trying to learn new engine and new language, so i have mostly abstract questions now. Plus i'm not native english speaker.

Nikola
Newbie
Posts: 4
Joined: Thu Sep 01, 2011 6:13 am
Contact:

Re: Saving variables, how to do right?

#6 Post by Nikola »

Got what i wanted :mrgreen:

Code: Select all

label InitVariables:
  python:
    Counter=0
  return

init python:
  def IncCounter():
    global Counter
    Counter+=1
    return("Inc")

  def DecCounter():
    global Counter
    if Counter>=1:
      Counter-=1
    return("Dec")

screen Test:
  key "+" action IncCounter
  key "-" action DecCounter
  frame:
    vbox:
      text "Counter: [Counter]"
      hbox:
        textbutton "Inc" action IncCounter
        textbutton "Dec" action DecCounter
      textbutton "Done" action Return("Done")

label start:
  call InitVariables
  "Started"
  while _return!="Done":
    call screen Test
  "Done [_return]"
  return
Closed/Solved i guess, unless there is better way to do it.

Theodosius
Newbie
Posts: 9
Joined: Fri Oct 07, 2016 8:24 pm
Contact:

Re: Saving variables, how to do right?

#7 Post by Theodosius »

Ahem... have the same problem, but it don't work for me :( I have one script for all screens (realtime + changing values depending on time values). Anyone knows how to save changed values?


User avatar
oza
Newbie
Posts: 17
Joined: Sat Sep 17, 2011 7:27 am
Projects: 60000 Thou
Location: Empire
Contact:

Re: Saving variables, how to do right?

#9 Post by oza »

Is there a clear and direct way to tell the engine to save data I need?

For example, RenPy don't save lists like:

Code: Select all

define gift_random_books = ["rbook1", "rbook2", "rbook3", "rbook4", "rbook5"]
and if it changed

Code: Select all

$ gift_random_books.remove("rbook3")
After load the list will be full again as 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: Saving variables, how to do right?

#10 Post by Remix »

If you are happy using a python object, this Cookbook Class patches into the Ren'py store and saves/loads/rolls any altered value of its attributes. Though it is generally built to extend characters it could easily be used for any game variables... (or just to get ideas of how to access the store)
Frameworks & Scriptlets:

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Saving variables, how to do right?

#11 Post by trooper6 »

If your variable is going to change, you must use default rather than define.

define is for things that won’t change
default is for things that will change
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

Post Reply

Who is online

Users browsing this forum: No registered users