Define and Default vs. Lint and Saves

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
Apa
Regular
Posts: 103
Joined: Fri Dec 23, 2016 2:26 am
Location: NYC
Contact:

Define and Default vs. Lint and Saves

#1 Post by Apa »

This is sort of follow up from Save and objects and Insert variables into code instead of into strings?
In short:
To be able lint an initialized variable, it has to be "Define", not "Default"
To be able to save/rollback a variable should be "Default".
To marry two, I’ve aliased "Default" variable to "Define" one.

Code: Select all

# for lint
define rooms_init = { # "e" - enabled, "n" - nodes
    home :   { "e" : True,  "n": [yard, city], },
}

# for save
default rooms = rooms_init
But PyTom wrote:
PyTom wrote:You're not allowed to alias an init time variable with a non-init-time variable.
I’m lost completely... :?

1. Am I right that:
"Define" == init time variable
"Default" == non-init-time variable

2. What breaks exactly, if they are aliased?

3. What is the proper way to handle such aliasing?

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Define and Default vs. Lint and Saves

#2 Post by nyaatrap »

Define overwrite default everytime when game loads, so you can't alias mutable object.
Instead, you can copy objects using the copy module so these two objects become different.
from copy import copy, deepcopy: default rooms = deepcopy(rooms_init) #deepcopy if variable is nested

Apa
Regular
Posts: 103
Joined: Fri Dec 23, 2016 2:26 am
Location: NYC
Contact:

Re: Define and Default vs. Lint and Saves

#3 Post by Apa »

nyaatrap wrote:Define overwrite default everytime when game loads, so you can't alias mutable object.
Ok.
When I had "Define" only variable - load didn’t restore it. And it won’t show up in Variable viewer either.
Right after I added "Default" as alias - load starts working properly.
By "load" I meant, "quit game, start and load" sequence.
I.e. I’m seeing different behavior than the one you described... :?
Would you suggest a simple test (with print statements to a file?) to see, what’s going on under the hood during load, please?
"Seeing == Believing", right? :)
nyaatrap wrote:Instead, you can copy objects using the copy module so these two objects become different.
from copy import copy, deepcopy: default rooms = deepcopy(rooms_init) #deepcopy if variable is nested
Yep, it’s clear.
Would it be a good idea to let GC pick up "Define" object as it served its purpose already (and could be fairly big!)?
And if the answer is yes, would it be the same to do:

Code: Select all

default rooms = rooms_init
rooms_init = None
instead of

Code: Select all

default rooms = deepcopy(rooms_init) #deepcopy
I’m terribly sorry for very newbie questions... :oops:

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Define and Default vs. Lint and Saves

#4 Post by nyaatrap »

It looks I misunderstand a bit.

Code: Select all

define a =[]
default b= a

label:
  $b.append(0)
  "[b] [a]" #shows [0], [0]
  #reload here
  "[b] [a]" #shows [0], []
If you creates an alias of a mutable object (list or dict), then changing one also changes another in the current play. If you reload data, define re-creates same object, but it's a new different thing so two object becomes different.
If you understand this behavior, it might work on your code.
Last edited by nyaatrap on Tue Jan 10, 2017 2:06 am, edited 1 time in total.

Apa
Regular
Posts: 103
Joined: Fri Dec 23, 2016 2:26 am
Location: NYC
Contact:

Re: Define and Default vs. Lint and Saves

#5 Post by Apa »

nyaatrap wrote:It looks default creates copy of object, not alias of it. So I think your code works.
Do you mean RenPy "Default" statement is "syntactic sugar" for python deepcopy()? :?
And again, anyway to prove it easily?

User avatar
nyaatrap
Crawling Chaos
Posts: 1824
Joined: Mon Feb 13, 2012 5:37 am
Location: Kimashi Tower, Japan
Contact:

Re: Define and Default vs. Lint and Saves

#6 Post by nyaatrap »

I edited my previous post (that was wrong).
Ren'py saves original object data, but define always creates a new data that will not saved, so they behave like copies.

Apa
Regular
Posts: 103
Joined: Fri Dec 23, 2016 2:26 am
Location: NYC
Contact:

Re: Define and Default vs. Lint and Saves

#7 Post by Apa »

nyaatrap wrote:I edited my previous post (that was wrong).
Ren'py saves original object data, but define always creates a new data that will not saved, so they behave like copies.
You code shows, it’s perfectly fine to that (or I don't understand PyTom statement):
PyTom wrote:You're not allowed to alias an init time variable with a non-init-time variable.
They’ll be aliased until 1st load only.

And you didn’t show rollback behavior... nothing would break either, right?

Post Reply

Who is online

Users browsing this forum: snotwurm