My variable is resetting to 0 on load or game close.

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
BigBucket
Regular
Posts: 26
Joined: Wed Aug 21, 2019 9:19 pm
Contact:

My variable is resetting to 0 on load or game close.

#1 Post by BigBucket »

I'm using a variable to determine what will show in a phone chat. The way it works is that there's a Vbox and things show up based on the variable value, which is incremented by 1 everytime you click on the screen outside of the chat.

I'm declaring my variable in the script file, before anything else, at the top of the file as:

Code: Select all

default var_chat = 0
Then this is the way it's incremented:

Code: Select all

key "dismiss" action [(SetVariable ("var_chat", var_chat+1))]
It does increment and stuff show up on the Vbox, but everytime I load the save file or close the game, the variable is set back to 0.

What am I doing wrong?

EDIT: I'm using this variable inside a screen and not through labels.

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: My variable is resetting to 0 on load or game close.

#2 Post by Remix »

So, you start a new game and the value is 0 (as expected)
You load a save and the value is 0 (we'd expect it to retain the value it was when the save was done, so this is possibly expected too). You've not confirmed the save holds a different value so we do not know.

If you wanted it to maintain state between different play throughs, make it persistent. Pretty sure you do not want that though. I'd search your entire script(s) for var_chat and check you are not altering it elsewhere... Other than that, expected behaviour...
Frameworks & Scriptlets:

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: My variable is resetting to 0 on load or game close.

#3 Post by Remix »

Note though that saving from a screen area (without an interaction) will not automatically update the variables value... Ren'Py only natively puts checkpoints at places in label flow that cause interactions... Perhaps close the screen, add a dialogue line and try saving after that is reached.
Frameworks & Scriptlets:

BigBucket
Regular
Posts: 26
Joined: Wed Aug 21, 2019 9:19 pm
Contact:

Re: My variable is resetting to 0 on load or game close.

#4 Post by BigBucket »

Hmm, it might be the problem with checkpoints then. Will try it.
And yes, I'm making it go up and saving and then closing but it's not saving the final value, but it might be the checkpoint thing. Any dialogue would do it?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: My variable is resetting to 0 on load or game close.

#5 Post by Remix »

Even a pause 0.01 should do it, so if you have no other logical way around it you could theoretically call a label that incremented the variable, paused minutely and returned...
(note that progression through labels should control game progression, not progression through screens... screens should really be for displaying things, not to control game progress)
Frameworks & Scriptlets:

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: My variable is resetting to 0 on load or game close.

#6 Post by rayminator »

you can use this if everything you tried and didn't work

Code: Select all

$ renpy.retain_after_load()

User avatar
SypherZent
Veteran
Posts: 362
Joined: Fri Sep 02, 2016 3:14 am
Completed: Multiverse Heroes, Space Hamster in Turmoil
Location: Puerto Rico
Contact:

Re: My variable is resetting to 0 on load or game close.

#7 Post by SypherZent »

Are you using 'show' for the screen, or 'call'?

If you use 'call' then you're passing the control to the screen and eventually you need to use the Screen Action called Return() to go back to the labels. Until Ren'Py hits the next label in the script, the variables won't save. If you use 'call' then perhaps rayminator's suggestion may help.

The way I force save when my game is fully based on screens, is to use 'Return' after the statement, then call the screen again from a centric label that I name 'continue_gameplay'. So the flow goes like this:

1) call screen
2) From screen, do action and Return()
3) in the lines after 'call screen' in the script, I put 'jump continue_gameplay'
4) I define label continue_gameplay to call the appropriate screen based on variable setting

Every time the label is visited, Ren'Py internally saves the data, even if you're always jumping to the same label.

Hope this helps.

BigBucket
Regular
Posts: 26
Joined: Wed Aug 21, 2019 9:19 pm
Contact:

Re: My variable is resetting to 0 on load or game close.

#8 Post by BigBucket »

Thank you all for the information. I understood how it works and why it wasn't saving.

I tried incrementing the variable through labels with this code

Code: Select all

label mica_chat_1:
    call screen chat

    $ var_chat +=1

    $ var_chat +=1

    $ var_chat +=1

    $ var_chat +=1

    $ var_chat +=1

    $ var_chat +=1


    menu:
        "Send Picture":
            $mica_send_choice_1 = True
        "Don't send":
            jump chat_screen
and with "pause" between every increment but both ways just kept incrementing with every click infinitely ,instead of just 6 times, and the menu never showed up.

I know this isn't the topic main subject but if it's a simple fix could anyone explain what I should do?

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: My variable is resetting to 0 on load or game close.

#9 Post by rayminator »

how are you returning from the call are you hiding the screen chat when you close or return?

try using hide screen chat under call screen chat

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: My variable is resetting to 0 on load or game close.

#10 Post by PyTom »

Code: Select all

key "dismiss" action [(SetVariable ("var_chat", var_chat+1))]
I'd be a little shocked if this ever ran. Generally, the SayBehavior - which is what handles the dismiss key - will terminate the interaction before the screen processes this. As a result, SetVariable will never be called, and so var_chat will not increment.

I'd suggest using a character callback for this, perhaps something like:

Code: Select all

init python:
    def bump_var_chat(event, interact=True, **kwargs):
        global var_chat

        if not interact:
            return

        if event == "end":
             var_chat += 1

      config.all_character_callbacks.append(bump_var_chat)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Google [Bot]