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.
# 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:
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:
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.
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?
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?
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 .....