[Solved] Is it a bad idea to use variables as dictionary keys before runtime? Answer: No. (Read)

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] Is it a bad idea to use variables as dictionary keys before runtime? Answer: No. (Read)

#1 Post by henvu50 »

Is this a bad idea? I'm using variable names instead of a string when setting dictionary keys.

Code: Select all

# this only works if i set this to -1
init offset = -1
define var01 = "bank"
define var02 = "store"

define someDict = { 
     var01: ["1", "2", "3"]
     var02: ["1", "2", "3"]     

label start:
    ... bla ...
The reason why I'm doing this is because I have a bunch of dictionaries that use the same location name a lot, but anytime I want to change location name, I have to change it in five places. This way I can change one variable to change a location name.

If this is a bad idea, I could build the dictionaries at runtime by adding the entries then?

Here is some more info I found out about init offset:

Code: Select all

## The init offset statement causes the initialization statements in this file
## to run before init statements in any other file.
Last edited by henvu50 on Fri Jun 18, 2021 8:16 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Is it a bad idea to use variables as dictionary keys before runtime?

#2 Post by Ocelot »

As long as you keep proper order and never try to use object before assignment, it will work.
Using named constants to refer to the actual value is a textbook DRY compliance:
https://en.wikipedia.org/wiki/Don%27t_repeat_yourself
init offset statement can be replaced with init python block with specific init priority:

Code: Select all

init -200 python: # -200 is my standard priority for non-dependent constants
    var1 = "bank"
    var2 = "store"

init -190 python: # I usually place dependent constants 10 priority higher, unless they depend on result of function invocation
    someDict = { 
        var01: ["1", "2", "3"],
        var02: ["1", "2", "3"],
    }

In addition, you forgot to reset init offset back to 0, which might cause problems if you forget about it. It is impossible with explicit python blocks.
< < insert Rick Cook quote here > >

henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

Re: Is it a bad idea to use variables as dictionary keys before runtime?

#3 Post by henvu50 »

init offset statement can be replaced with init python block with specific init priority:

Code: Select all

init -200 python: # -200 is my standard priority for non-dependent constants
    var1 = "bank"
    var2 = "store"
That look really good, but does var1 and var2 need to be declared anywhere using define?


Code: Select all

init -190 python: # I usually place dependent constants 10 priority higher, unless they depend on result of function invocation
    someDict = { 
        var01: ["1", "2", "3"],
        var02: ["1", "2", "3"],
    }
I like this approach!


In addition, you forgot to reset init offset back to 0, which might cause problems if you forget about it.


Just or academic purposes, are you saying I should do something like this?

Code: Select all

init offset = -1
# these variables must be declared first
define highPriorityVar01 = "bla"
define highPriorityVar02 = "bla"
# restore init offset back to 0
init offset = 0

define normalVar01 = "ba"
define normalVar02 = "ba"

default someDict = { highPriorityVar01 : ["1","2"], highPriorityVar02 : ["1","2"] }

label start:

    jump bla
It is impossible with explicit python blocks.
Are you saying it's impossible to make the mistake of: forgetting to reset init offset back to 0, when using this approach? init -200 python .....

thanks btw, this clarification helped a lot.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: Is it a bad idea to use variables as dictionary keys before runtime?

#4 Post by Ocelot »

henvu50 wrote: Fri Jun 18, 2021 6:32 am That look really good, but does var1 and var2 need to be declared anywhere using define?
No, in Python variable lifetime starts as soon as you assign something to it. There isn't any special declaration phase like in ose other programming languages.
henvu50 wrote: Fri Jun 18, 2021 6:32 am Just or academic purposes, are you saying I should do something like this?

Code: Select all

init offset = -1
# these variables must be declared first
define highPriorityVar01 = "bla"
define highPriorityVar02 = "bla"
# restore init offset back to 0
init offset = 0

define normalVar01 = "ba"
define normalVar02 = "ba"

default someDict = { highPriorityVar01 : ["1","2"], highPriorityVar02 : ["1","2"] }

label start:

    jump bla
Yes, it is a good idea, because sometimes it can surprise you, how much stuff it affects: default statements, images, transforms, screens, styles...

Code: Select all

init python: # init priority 0
    def jitter( # . . .  a randomly non-linerar interpolation function 

init offset = -1
define m_pi = 3.14159265358

# Some code

image cockroach:
    "roach.png"
    xpos 0.0
    xanchor 1.0
    jitter 15.0 xpos 1.0 xanchor 0.0 # executes at init priority -1, before jitter is defined, causing an error. 
henvu50 wrote: Fri Jun 18, 2021 6:32 am Are you saying it's impossible to make the mistake of: forgetting to reset init offset back to 0, when using this approach? init -200 python .....
Yes, this is exactly what I mean.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]