[Solved] How to use 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
Ayael
Regular
Posts: 82
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Imperial Grace (on going), Autumn Spirit, Ballads at Midnight
Contact:

[Solved] How to use persistent data?

#1 Post by Ayael »

Hello, I have been reading the documentation and old thread (for example this one : viewtopic.php?t=39481), but it seems I am too bad to use persistent data, correctly. I am note a native english (so sometimes the explications are a bit complicate to me), plus I am kinda new to coding and ren'py so please be patient and full of explanations with me.

I want to create a persistent data to know if the player had already played the game once or not.

So I create something like that

Code: Select all

init:
     $randomothervariableIneed = 0
     define persistent.gameplayed = None

label start:

     if persistent.gameplayed = true:
         "True"
     else:
         "false"

[BLABLABLA THE GAME]

     persistent.gameplayed = true

 
     return
So the idea is : I have a persistent date who is per default set on none (I understood it was better than false?), then I check if it's on true, or not, so if it's the first game, it should still be none. At the end of the game, I set it on true so if it's the second or more game, it should be set on true.

But I got this error :
Image
(Line 33 is where there is the "if").

Thank you to everyone, I look forward, to soluce my problem.


EDIT : It's weird, because it seems, that it's the "true"/"false"/"none" that doesn't work, because if I try with 0/1 for example, it actually work

Something like that (I put the code working instead it is actually a terrible optimisation, or if someone has the same problem than me !

Code: Select all

define m = Character("[player_name]", color="#F7DD9E", who_outlines=[ (3, "#78462D") ])
default persistent.gamefinished = 0

init python:
     l_adrala = 0

# Le jeu commence ici
label start:

     if persistent.gamefinished == 0:
         "C'est la première fois que le jeu est joué"
     else:
         "C'est la deuxième fois"
[blabla jeu]

     "fin"
     $ persistent.gamefinished = 1

     return
Last edited by Ayael on Fri May 17, 2019 3:48 pm, edited 2 times in total.
Image Image

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to use persistent data?

#2 Post by Alex »

Ayael wrote: Thu May 16, 2019 1:00 pm ...EDIT : It's weird, because it seems, that it's the "true"/"false"/"none" that doesn't work, because if I try with 0/1 for example, it actually work...
Yes, they must be True / False / None (with capital letter).

As for persistent variables, just set them using default statement (not define), like

Code: Select all

default persistent.gameplayed = False

label start:

    if persistent.gameplayed == True:
        "True"
    else:
        "False"

    "\[BLABLABLA THE GAME\]"

    $ persistent.gameplayed = True
 
    return
Also, note that single equal sign (=) is used for setting values, for checking values used double equal sign (==).

And when you check if value is True or False you can do it like

Code: Select all

if var: # the same as if var == True
if not var: # the same as if var == False
Aand setting value of a variable is a python code, so it must be either prefixed with $-sign (means a singl-line python code), or put inside a python block.

Code: Select all

label start:
    python:
        var = 1
    "var = [var]"
    $ var += 1
    "var = [var]"
    "?!"
Useful links:
https://www.renpy.org/doc/html/index.html
https://www.renpy.org/doc/html/quickstart.html
https://www.renpy.org/doc/html/python.html

Ayael
Regular
Posts: 82
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Imperial Grace (on going), Autumn Spirit, Ballads at Midnight
Contact:

Re: How to use persistent data?

#3 Post by Ayael »

Thank you for answering me. But I don't really understand this part :
Alex wrote: Thu May 16, 2019 4:19 pm

And when you check if value is True or False you can do it like

Code: Select all

if var: # the same as if var == True
if not var: # the same as if var == False
Aand setting value of a variable is a python code, so it must be either prefixed with $-sign (means a singl-line python code), or put inside a python block.

Code: Select all

label start:
    python:
        var = 1
    "var = [var]"
    $ var += 1
    "var = [var]"
    "?!"
Useful links:
https://www.renpy.org/doc/html/index.html
https://www.renpy.org/doc/html/quickstart.html
https://www.renpy.org/doc/html/python.html

could you give me soome more information on it ?
Image Image

User avatar
Alex
Lemma-Class Veteran
Posts: 3094
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: How to use persistent data?

#4 Post by Alex »

Well, just try

Code: Select all

label start:

    if persistent.gameplayed:
        "True"
    else:
        "False"
and check the last link about using code in a python language in Ren'Py.

Ayael
Regular
Posts: 82
Joined: Fri Apr 07, 2017 2:17 pm
Projects: Imperial Grace (on going), Autumn Spirit, Ballads at Midnight
Contact:

Re: [Resolve] How to use persistent data?

#5 Post by Ayael »

Okay, I think I got it ! Anyway, it's perfectly working so, thank you very much for your help.
Image Image

Post Reply

Who is online

Users browsing this forum: No registered users