Updating persistent dict

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
User avatar
Mhyrria
Newbie
Posts: 10
Joined: Wed Jan 24, 2018 8:55 am
Contact:

Updating persistent dict

#1 Post by Mhyrria »

Here's the rough breakdown. I have persistent.collecteditem dict that has item1: 0, item2: 0, etc.. all under define declaration. Now if I add additional keys to the dict, it doesnt update ingame, ergo if I access say lastitemadded key, I'd get a key error. Restarting the game from the beginning still doesnt add the key. Only way for the new key to be actually added, not just in the script but actually workable ingame, is to delete persistents. This fine for me, but not for the end-user as I am planning to release the game incrementally via updates instead of the complete game right off the bat. The end-user has to delete everything and has to start from scratch every time there is an update. Not a very good experience for them.

Now, why do i use persistent when a normal dict will just do? My game uses the rollback feature as a form of game mechanic akin to traveling back to past of some sort. So certain collectible items can only be collected after reaching a certain flag then rolling back a few lines and collectable item is now clickable. This can only be done with persistent as far as I know. I maybe wrong.

So is there any way to force update the persistent dict specifically without deleting them? I can add new keys to the persistent dict via python's dict.update(), but that would mean that I have to use dict.update() everytime I introduce a new key. One problem I see with this is end-user may rollback too far and trigger a dict.update() which resets the value of the key to its default, but it can remedied with a conditional. All those extra steps bloats up the code though. So is there a way to add new keys for an exisiting persistent dict at an init level?

Alternatively, is there a way to use the rollback feature to exclude rolling back certain variables that are non-persistent?

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Updating persistent dict

#2 Post by Kia »

I think as long as the persistent variable is updated with code and not by editing the .rpy file the added data should show up fine, usually the problem appears when you try to edit the variable through it's definition stage.
for updates, you can simply add an "if" statement to check if the variable is changed before editing it.

Code: Select all

if not persistent.collecteditem[2]:
    $ persistent.collecteditem[2] = 3

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]