[SOLVED]How can I save global variables without advancing the main script?

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
Thailandian
Regular
Posts: 30
Joined: Tue Jun 01, 2021 6:24 am
Contact:

[SOLVED]How can I save global variables without advancing the main script?

#1 Post by Thailandian »

I have several global boolean variables that are set to true if the player correctly answers questions that are asked and answered in a screen. The variables are global, rather than screen variables because checking the answers is quite complex so needs to be handled by python functions.

The problem I realised, is that if the player saves before or immediately after the screen is closed, the new value of the variables is not saved. Provided the player advances the main script at least one statement after closing the screen, the variables are saved properly.

So I'd like to find some way to trigger these variables to be saved if the player saves immediately after closing the screen.

I doubt that this will help much, but just in case, here is a sample of the code I use to set the variables

Code: Select all

def processG01Qs():
        global g01_q_response, g01_text, g01_q01_answers, g01_q02_answers, g01_q03_answers, g01_q04_answers, g01_q05_answers, g01_q06_answers, g01_q01_complete, g01_q02_complete, g01_q03_complete, g01_q04_complete, g01_q05_complete, g01_q06_complete, g01_rose, g01_cassy, g01_olivia, g01_wbb_name
        g01_q_response = g01_q_response.strip().lower()
        if g01_text == g01Q_t_q01:
            if g01_q01_answers[0] in g01_q_response and g01_q01_answers[1] in g01_q_response:
                g01_text = g01Q_fbpos_q01
                g01_q01_complete = True
                renpy.show ("tr c_cas_01 dubious")
            else:
                g01_text = g01Q_fbneg_q01
                renpy.show ("tr c_cas_01 negative")
        elif g01_text == g01Q_t_q02:
            if g01_q02_answers.count(g01_q_response) > 0:
                g01_text = g01Q_fbpos_q02
                g01_q02_complete = True
                renpy.show ("tr c_cas_01 smiling")
            else:
                g01_text = g01Q_fbneg_q02
                renpy.show ("tr c_cas_01 negative")
As always any ideas or suggestions would be most appreciated.
Last edited by Thailandian on Sun Sep 12, 2021 2:12 am, edited 2 times in total.

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

Re: How can I save global variables without advancing the main scrip?

#2 Post by Alex »

Try either put this line right before calling your screen

Code: Select all

$ renpy.retain_after_load()
or this line in your screen

Code: Select all

on 'show' action Function(renpy.retain_after_load)
https://www.renpy.org/doc/html/save_loa ... after-load
https://www.renpy.org/doc/html/screens.html#on
https://www.renpy.org/doc/html/screen_a ... l#Function
Last edited by Alex on Sun Sep 12, 2021 6:30 am, edited 1 time in total.

Thailandian
Regular
Posts: 30
Joined: Tue Jun 01, 2021 6:24 am
Contact:

Re: How can I save global variables without advancing the main scrip?

#3 Post by Thailandian »

Thank you Alex!

That's exactly what I was looking for.

Post Reply

Who is online

Users browsing this forum: goldo, Google [Bot], Semrush [Bot], VESTED