Setting persistent data

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
mard
Regular
Posts: 50
Joined: Thu Nov 03, 2016 3:38 am
Location: Standing right behind you.
Contact:

Setting persistent data

#1 Post by mard » Tue Nov 29, 2016 7:28 am

So I'm planning to make a VN with route locking, and have been messing around trying to figure out how to get it to write to the persistent data. Here is the code I've got so far.

Code: Select all

## here we check labels to decids which route to show next
    label check:
        if persisent.ending_1:
            if persistent.ending_2:
                if persisent.ending_3:
                    jump arc_4
                else:
                    jump arc_3
            elif persistent.ending_3:
                jump arc_2
                
            elif persistent.sc_1_answer_1:
                jump arc_2

            elif persistent.sc_1_answer_2:
                 jump arc_3
                 
            else:
                jump arc_2
    label arc_1:
        scene bg room
        show eileen happy
        menu:
            "I am me":
                $ persistent.sc_1_answer_1 = True
                $ renpy.save_persistent()
                jump end
            "I am you":
                $ persistent.sc_1_answer_2 = True
                $ renpy.save_persistent()
                jump end
    label arc_2:
        scene bg room
        show eileen happy
        "e" "Who are you?"
        
        "P" "I am me."
        
        $ persistent.ending_2 = True
        $ renpy.save_persistent()
        jump end
    
    label arc_3:
        scene bg room
        show eileen happy
        "p" "Who am I?"
        
        "e" "You are you."
        $ persistent.ending_3 = True
        $ renpy.save_persistent()
        jump end

    label arc_4:
        scene bg room
        "e, p" "We are us"
        $ persistent.sc_1_answer_1 = False
        $ persistent.sc_1_answer_2 = False
        $ persistent.ending_1 = False
        $ persistent.ending_2 = False
        $ persistent.ending_3 = False
        $ renpy.save_persistent()
        jump final

    label end:
        "goodbye"
        return
        
    label final:
        "You win"
        return
and heres the code which directs to this block

Code: Select all

label start:

    if persistent.ending_3:
        jump check
    elif persistent.ending_2:
        jump check
    elif persistent.ending_1:
        jump check
    else: 
        jump arc_1
I feel like I'm probably missing something really obvious, but whatever it is just isn't clicking.
Tend to be a bit quiet, but will help where I can.

Enjoy the drinks folks.

mard
Regular
Posts: 50
Joined: Thu Nov 03, 2016 3:38 am
Location: Standing right behind you.
Contact:

Re: Setting persistent data

#2 Post by mard » Tue Nov 29, 2016 9:02 am

I figured it out. Forgot to set ending_1 to true after selecting one of the two choices.
Tend to be a bit quiet, but will help where I can.

Enjoy the drinks folks.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1458
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Setting persistent data

#3 Post by xavimat » Tue Nov 29, 2016 9:46 am

You don't need the line: $ renpy.save_persistent()
Ren'py does it for you.
All data reachable through fields on persistent is saved when Ren'Py terminates
https://www.renpy.org/doc/html/persistent.html
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

mard
Regular
Posts: 50
Joined: Thu Nov 03, 2016 3:38 am
Location: Standing right behind you.
Contact:

Re: Setting persistent data

#4 Post by mard » Tue Nov 29, 2016 10:15 am

That only takes effect when the script closes though. While the script is still running it won't register the changes, as renpy won't reload persistent data outside of the init phase unless it registers a change to the file on disk while the script is running.
Tend to be a bit quiet, but will help where I can.

Enjoy the drinks folks.

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

Re: Setting persistent data

#5 Post by Ocelot » Tue Nov 29, 2016 10:39 am

While the script is still running it won't register the changes
Which changes? What does it mean "won't register"?

As soon as you assign something to persistent variable, all subsequent reads will read assigned value. There is no "stale" values anywhere.
And RenPy won't reload persistent data even if file will change. It only reads persistent file on init and writes on saving it.

The only way to lose persistent data is to either get a hard crash (either by killing game process or finding a bug in interpreter) or sudden power-off.
And the only way to actually see that data is not written is to launch another copy of the game while first one is still running.
< < insert Rick Cook quote here > >

mard
Regular
Posts: 50
Joined: Thu Nov 03, 2016 3:38 am
Location: Standing right behind you.
Contact:

Re: Setting persistent data

#6 Post by mard » Tue Nov 29, 2016 12:10 pm

The static values within the persistent save file itself.

While the script is running, it reads values as active, and when the script exits it writes them as static. Thats what I meant when I said it doesn't register. It isn't until renpy either exits out, or you call renpy.save_persistent() that the values become static within the file. Until the, they are registered as active changes.

Forgive me, I've never been the best at putting my thought processes into coherent words.
Tend to be a bit quiet, but will help where I can.

Enjoy the drinks folks.

Post Reply

Who is online

Users browsing this forum: _ticlock_