Just wanna know how can I create a saveable variable after start() that I can access it from python and change its value. Example, I create a variable called "variable = False" after start(). Then, I want to change its value to True but in another file, calling in init python.
I know there is "default", I use it, but I can't change it's value inside python blocks, only in renpy. I searched a lot, but couldn't find a solution. Tried it in many ways. Is that simple, just want to add a new variable and don't have to start a new game, but in a way that I can change it's value in renpy or in python.
There's a lot going on within your post that doesn't seem clear to me, so let me go through a couple of things that "may be" the source of some confusion to you.
init:
and
init python:
blocks are run as the game is starting up. It really doesn't matter which source code
.rpy
file they exist in... RenPy searches all source code for stuff like these and processes them well before anything else happens.
So when you say you want to create a variable AFTER
label start:
- that doesn't really tie into the whole use of
init:
or
init python:
.
From a little testing I just did now, it seems that variables created/set using
default
are processed after both
init:
or
init python:
. Again, RenPy doesn't care where the
default
statements are located within your source code - they are all processed BEFORE the game really gets started
(but seemingly later than init blocks).
So if you had code that looked like this:
Python:
init:
$ myVar = 3
init python:
myVar = 6
label start:
"myVar value is [myVar]"
return
default myVar = 99
What you would see is "
myVar value is 99".
So if you want to override the value of a variable during initialization, don't use
default
- because you're effectively duplicating the variable creation/initialization process in two places and then misunderstanding why the value isn't what you wanted.
However, let's assume it's not a coding issue you are discussing... and instead a practical one.
Perhaps you are talking about how to add a variable AFTER the game has already been released and played by the community. Perhaps you wrote version 0.1, released it and are now working on version 0.2.
The simple answer is that is exactly why
default
exists. Because RenPy processes all
default
statements during game startup... if you add a
default myVar2 = False
during development of version 0.2 - then it will simply become available for use by your code. No need to do anything else, it's just "there" to be used. It doesn't matter that the variable didn't exist while someone was playing version 0.1 or even if they load a version 0.1 save file. The fact there is a
default
statement somewhere is enough.
By way of explanation, because
default
statements are processed before save files could be loaded - the value would normally be set to its default value. Then if you load a savegame from 0.1 (where the new variable doesn't exist as part of the save file), it just keeps using its original value. However, if you load a savegame from 0.2 (or later presumably), the value within the save file overwrites the initial value set by the
default
statement.
Which brings me to my last alternative interpretation of your post.
If you really do what want do something clever to the value of your variable AFTER the player has already started playing... there is
label after_load:
.
label after_load:
is an optional bit of RenPy code that is invoked
every time the player loads a saved game. Use it with care... that run "every time" can really trip up inexperienced programmers
(experienced ones too) - since you might put something in there to fix/repair something during 0.2, but you have to keep in mind that code will be run each and every time, even when loading version 0.8 save files.
You might do something like this:
Python:
default myVar3 = 0
label after_load:
if myVar3 == 0:
if renpy.seen_image("v3_bathroom7"):
$ myVar3 = 6
else:
$ myVar3 = 3
return
label start:
# blah, blah, blah....
Skip this bit if you like... it's just highlighting how even simple solutions may not quite work for all circumstances...
The flaw of course is that you'll need to add a $ myVar = 0
just before the first use of the myVar3
variable. You'd think that because there is a default myVar3 = 0
you wouldn't need it. The reason you might is that if you release this code, then a brand new player comes along, starts the game, maybe puts in their name and immediately saves and quits because, well "reasons". They come back later, load their saved game and this code runs, finds the current value is 0 and immediately sets it to either 3 or 6. It's not the "normal player doing normal things" that trips you up - it's the stuff like this that come back to bite you in the ass, because you wrote it to correct things for "most" players, without considering that future new players could do something you weren't worried about. It will of course depend on how you use label after_load:
, my point is merely that it isn't always as straight forward as it first appears.
Finally, keep in mind that any statement after a dollar sign ($) in RenPy
IS python code. So the idea of writing something in python after the the
label start:
is kinda moot - since it's all happening in python anyway.
However, as
anne O'nymous has pointed out with his example, there are certain levels of python code that require you to qualify where the variable is stored by adding
store.
before the variable name
(maybe even renpy.store.
?) in order to correctly address the variable in the deep dark depths. There are of course other potential containers other than
store.
, but honestly - it you are that deep into the dark arts... then you probably wouldn't be posting beginner level questions here in this forum.
I hope some of that helps.
If we have ultimately misunderstood your question... it may help if you posted the source code you are having problems with.