Renpy not storing status of python lists for the purposes of saving and rollback

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
possiblyanon
Newbie
Posts: 1
Joined: Sat Apr 28, 2018 2:07 am
Contact:

Renpy not storing status of python lists for the purposes of saving and rollback

#1 Post by possiblyanon »

I created an event system to use for my game, the system is fairly straight forward. As the player makes choices available events are added to a python list:

Code: Select all

init python:
  events = []
 
label start:
  me "I talked to a girl, that means I get to add an event."
  $ events.append("girlTalk")
  
 ...
 
if "girlTalk" in events:
  jump girlTalk
   
label girlTalk:
  me "This is the event that corresponds to the added event."
  $ events.remove("girlTalk") # Event has been completed, no longer needed.
Let me be clear: playing through normally has no issues. However, if I save the game before I hit the conditional that plays the event and I load from that save, the event will not trigger.

Is this expected behavior? Is it possible to fix this?

Thank you for your time.

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Renpy not storing status of python lists for the purposes of saving and rollback

#2 Post by kivik »

Rookie mistake (I made that in my first game): Use default outside of a label block

Code: Select all

default events = []

label start:
   # your code
Essentially any code inside an init block is run before the game starts, so you're reseting the events to a blank list every time you load a save game.

Use default to declare any variables that will change over the course of the game, and define for any variables that won't change. Read these two links for reference:
https://www.renpy.org/doc/html/python.h ... -statement (read define and default)
https://www.renpy.org/doc/html/save_load_rollback.html

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2400
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Renpy not storing status of python lists for the purposes of saving and rollback

#3 Post by Ocelot »

kivik wrote: Sat Apr 28, 2018 5:24 am Essentially any code inside an init block is run before the game starts, so you're reseting the events to a blank list every time you load a save game.
This is slightly incorrect. Loading variable values from saved game happens after init phase.
The actual problem is that events is not saved in the first place: RenPy adds to list of saved variables only references which were changed after game start. events never chnges after setting it in int block, so it is not saved at all. Object it refers to is mutated, but RenPy does not care about that.
Essentially variable is saved only if it is assigned something directly after game start: events = . . ..
< < insert Rick Cook quote here > >

kivik
Miko-Class Veteran
Posts: 786
Joined: Fri Jun 24, 2016 5:58 pm
Contact:

Re: Renpy not storing status of python lists for the purposes of saving and rollback

#4 Post by kivik »

Ocelot wrote: Tue May 01, 2018 5:06 am
kivik wrote: Sat Apr 28, 2018 5:24 am Essentially any code inside an init block is run before the game starts, so you're reseting the events to a blank list every time you load a save game.
This is slightly incorrect. Loading variable values from saved game happens after init phase.
The actual problem is that events is not saved in the first place: RenPy adds to list of saved variables only references which were changed after game start. events never chnges after setting it in int block, so it is not saved at all. Object it refers to is mutated, but RenPy does not care about that.
Essentially variable is saved only if it is assigned something directly after game start: events = . . ..
Ah I realised where my logic fell apart! I thought since he did events.append() in his start label that the variable did get modified and therefore should be saved - but the fact that it wasn't made me think it must have been overwritten by the init process.

But thinking about it - I remember documentation saying something about differences with pointers and values, and since events is a list, it's a pointer which wasn't set after init and thus wasn't saved. Evidently still confuses me at times, that's why I always say declare everything in default to be safe for saving :D

Post Reply

Who is online

Users browsing this forum: Andredron