[Solved] changing persistent variables in a function

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
GNVE
Regular
Posts: 39
Joined: Wed Sep 12, 2018 4:11 pm
Completed: ShSt - Bad Day
Projects: ShSt - Afterparty, Collings University
itch: https://gnve.itch.io
Contact:

[Solved] changing persistent variables in a function

#1 Post by GNVE »

So I have a preferences screen with several options to customize a players experience. I did want to add a couple of quick select buttons. Now I could use screen language to make the quickselects but it has some pitfalls and makes it all very unreadable. So I wanted to put everything in a function. I have found out however that Ren'Py ignores all logic in the function and takes whatever is set last as the new setting. It won't even allow the buttons to change the persistent variables anymore.
I have tried googling what I've been doing wrong and tried several permutations to get it to work but I don't seem to get anywhere. What am I missing? Is it even possible? The intention is to encourage several playthroughs so I don't want the player to setup preferences multiple times hence the choice for persistent variables.

(just a small sample)

Code: Select all

default persistent.var1 = True 
default persistent.var2 = True 
default persistent.var3 = True 
default persistent.var4= 'off' 
and the accompanying code for the function (again just a sample for clarity):

Code: Select all

Python:
    def otherswitch(setting,perma='',switch=''):

        if setting == 'off':
            persistent.var1 = False
            persistent.var2 = False
            persistent.var3 = False
            persistent.var4 = 'off'
        elif setting == 'level2':
            persistent.var1 = True
            persistent.var2 = True
            persistent.var3 = True
            persistent.var4 = 'off'
Last edited by GNVE on Sat Sep 11, 2021 7:23 pm, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: changing persistent variables in a function

#2 Post by hell_oh_world »

How are you calling this function?
If through the screen you need to use Function().

Code: Select all

screen test():
  button action Function(otherswitch, "off")

GNVE
Regular
Posts: 39
Joined: Wed Sep 12, 2018 4:11 pm
Completed: ShSt - Bad Day
Projects: ShSt - Afterparty, Collings University
itch: https://gnve.itch.io
Contact:

Re: changing persistent variables in a function

#3 Post by GNVE »

That solved it OMG Thank you! I called the function as a regular function in screen language. I had been stumped for about a week now! Never would have caught that. I am happy it was a relatively small issue.

Post Reply

Who is online

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