[bug] define not changing the saved variable

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
Victorius
Regular
Posts: 30
Joined: Thu Jan 14, 2021 7:02 pm
Projects: Astral Lust
Github: Victorius-Astral
Contact:

[bug] define not changing the saved variable

#1 Post by Victorius »

So I've encountered a bug (I think it is, according to docs). I assigned a variable as

Code: Select all

 default x = "some value"
, then I saved the game. In later game version I've changed the code to

Code: Select all

define x = "some other value"
after loading the game x was "some value" so it didn't change. At first I've thought it was supposed to work like this, but later I've read docs about define & default, according to docs it should be changed.

Ren'Py version: 7.4.2.1292

Victorius
Regular
Posts: 30
Joined: Thu Jan 14, 2021 7:02 pm
Projects: Astral Lust
Github: Victorius-Astral
Contact:

Re: [bug] define not changing the saved variable

#2 Post by Victorius »

It also works like that with assigning variable inside of a label.

Code: Select all

$x = "some var"
and then removing it and adding

Code: Select all

define x = "some other var"

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: [bug] define not changing the saved variable

#3 Post by zmook »

I can't find code or documentation that says for sure, but I think the problem is that init-time code (like define statements) is executed before the save is loaded. Then the value of the variable read in from the save file overwrites the init (defined) value, just like what would happen if you had

Code: Select all

define a = 1

label start:
    $ a = 2 
`Defines` sound like they should be constants but they're not; they are recorded in the store just like any other variable and can be changed later. The principal (only?) difference is that their values are assigned at init time and are not recorded in save files unless they're changed.
Last edited by zmook on Mon Mar 01, 2021 5:25 pm, edited 1 time in total.
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
zmook
Veteran
Posts: 421
Joined: Wed Aug 26, 2020 6:44 pm
Contact:

Re: [bug] define not changing the saved variable

#4 Post by zmook »

If you have a variable declared version 1 in a default,

Code: Select all

# v1
default a = 'hello'
and want to fix it in version 2 including retroactively to people playing from saves, I think you have to use the after_load label to correct it:

Code: Select all

# v2
default a = 'hi'

label after_load:
	$ a = 'hi'  	# to fix old saved values
It's kind of ugly and error-prone having to set the value in two places, though. Alternately, you could give up on the `default` statement altogether and do:

Code: Select all

# v2a
label set_defaults:
    if not hasattr(store, 'a') or a = 'hello':
        a = 'hi'
    return

label start:
    call set_defaults

label after_load:
    call set_defaults
Or, possibly best, just throw away the variable you need to change and replace it with a new one.

Code: Select all

# v2b
default a2 = 'hi'

eileen "[a2] there, fellow sprite!"
colin r
➔ if you're an artist and need a bit of help coding your game, feel free to send me a PM

User avatar
_ticlock_
Miko-Class Veteran
Posts: 910
Joined: Mon Oct 26, 2020 5:41 pm
Contact:

Re: [bug] define not changing the saved variable

#5 Post by _ticlock_ »

This post does not exactly answer, but give an idea that saved variables overwrite the variables defined with define statements as zmook mentioned.
This problem should appear only when loading save file that stores the variable. Basically, it is a compatibility problem.

If you want to ensure that the value of the variable is correct for old save files you can explicitly check it in its value in the after_load label:

Code: Select all

label after_load:
    python:
        if x != "some other var":
            x = "some other var"

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3794
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: [bug] define not changing the saved variable

#6 Post by Imperf3kt »

Is this because you wanted to use the variable you had chosen, for something else, or because you decided on a more appropriate variable name?

If the original variable is no longer in use, you can simply default the variable as the new variable.

Code: Select all

default new_variable = "some other variable"
default old_variable = new_variable
Make sure you default the new variable before the old one, so it exists when renpy tries to set a value to the old variable.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

Victorius
Regular
Posts: 30
Joined: Thu Jan 14, 2021 7:02 pm
Projects: Astral Lust
Github: Victorius-Astral
Contact:

Re: [bug] define not changing the saved variable

#7 Post by Victorius »

Thanks for replies!

I've changed variable name to a new one, I had a list of enemies hard coded inside a label, to give modders ability to add new enemies without changing vanilla files I moved it outside of a label so they can append to it at init time. I've written about this because I thought it might be a bug, so I wanted to provide feedback.

Post Reply

Who is online

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