Ren'Py [SOLVED]Dictionary, define and default

GoldenD

Member
Sep 30, 2018
102
70
Hi,
when i use a simple dictionary, there's no problem. I can declare by define or default, like i want.
But i decide to have a dictionary including a class and then, i 'can't declare with default ! An idea ?

Python:
define listNpcs = {
    "John"   : myClass("Friend", "John"),
    "Barbie" : myClass("Mother", "Barbie")
}
works properly, but :

Python:
default listNpcs = {
    "John"   : myClass("Friend", "John"),
    "Barbie" : myClass("Mother", "Barbie")
}
result in :
NameError: name 'listNpcs' is not defined
The Error is when I use my dico in a string :

Python:
" %s myString" % ( listNpcs["John"].name)
And of course I would change some datas in my class...
 
Last edited:

the66

beware, the germans are cumming
Modder
Donor
Respected User
Jan 27, 2017
7,668
23,783
Python:
init python:
    class cls(object):
        def __init__(self, a=None, b=None):
            self.a = a
            self.b = b

default dct = {
    "John":cls("brother", "John"),
    "Jane":cls("sister", "Jane")
    }

label start:
    $ st = "Hi little %s." % dct["Jane"].a
    dct["John"].b "[st]"
    return
works absolutely as intended.
 

GoldenD

Member
Sep 30, 2018
102
70
Ok, maybe i have to give you more infos.
In fact the "string" which initiate error is a part of another class in another list and yes, I can already imagine the lightning that I will attract.

Ok, there it is :

Python:
default dlgIntro001 = [
(anotherClass( "IntroGame", Hero.profile, "Welcome...%s...."  % listNpcs["John"].name , locIntro.picture)),
.../...
]
Sorry the comma was my fault, in fact there was others params and i try to simplify sample.
 
Last edited:

the66

beware, the germans are cumming
Modder
Donor
Respected User
Jan 27, 2017
7,668
23,783
if you want to have listNpcs["John"].name as argument for "Welcome...%s....", you have to remove the comma in between.
it's a syntax error that doesn't throw an error but results in a non-processing of the default statement.
 

GoldenD

Member
Sep 30, 2018
102
70
if you want to have listNpcs["John"].name as argument for "Welcome...%s....", you have to remove the comma in between.
it's a syntax error that doesn't throw an error but results in a non-processing of the default statement.
Sorry the comma was my fault, in fact there was others params and i try to simplify sample.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,368
15,282
The Error is when I use my dico in a string :

Python:
" %s myString" % ( listNpcs["John"].name)
The problem is surely due to the fact that those statements aren't proceeded at the same time.
define is proceeded at init time, before the main menu is displayed. But default is proceeded when the game start and right after a save is loaded.

I assume that the string that cause the error is also proceeded at init time. Which imply that it works fine with define (because the dictionary already exist), but not with default (because the dictionary is yet to create).
 

GoldenD

Member
Sep 30, 2018
102
70
Hi AON, i'd read your and threads and i'm worried.

For my thread, i think you're right, it's the proceed time that block, and Maybe I’ll take care of it later.

But i'm very worried about what you tell with
the variables created in an init block are not saved by default
and
it's a problem if the variable is an object, because they will never been saved
.

I suddenly wonder if I should not replace my classes with simple lists ? hoping that I could save the lists, which are also objects!!!
 

GoldenD

Member
Sep 30, 2018
102
70
Holã,
I just understand the cause of my problems with default and define and this comes from an info I didn’t give you.
Yes, because I code with a lot of files and no long code lines in one.
So, I'll do more tests with declarations and in the same time with save values, and i'll come back to give more infos.
 

GoldenD

Member
Sep 30, 2018
102
70
Ok, after some others tests.

For data backup, no worries from the moment when the object variables are default, either when starting or loading a backup.

For default and define
If i create 2 files, a.rpy and b.rpy
IF a.rpy uses one variable which is define in b.rpy then you obtain an error like non defined variable...
IF i reverse my files, all is working fine

So, i tried to define my variables in labels
in the a.rpy file, i create a label loadFileA and in the b.rpy file, i create a label loadFileB
And in start label, i call loadFileB first, then loadFileA
and i obtain a poor "variable is being given a default a second time."

Now i create 2 folders A and B
with a.rpy in A folder and b.rpy in B folder, you have the same error of course like when you have no folder
with a.rpy in B folder and b.rpy in A folder, all works fine

I searched in renpy documentation and it seems you can control the order of declaration with different ways of init, but just for the "define" declarations.
Nothing for default variables.

For the moment I will just rename my files and folders, but if you have another solution, I am taker
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,368
15,282
Hi AON, i'd read your and threads and i'm worried.
[...]
I suddenly wonder if I should not replace my classes with simple lists ? hoping that I could save the lists, which are also objects!!!
You can use classes, you just need to declare them at the right moment, like explain in the How-To. Sometimes it will need a refactoring of your code, because "like this" it would conflict with the savability, but it's never impossible to use them.


Yes, because I code with a lot of files and no long code lines in one.
Here it's way more simple than it look at first :
The init process is split by level. Each init block (or init statement, like define among others), will be proceeded according to its level ; an init -10 block will be proceeded before an init -9 one.
And for each level, the block will be proceeded according to the alphabetical order of the file where they are. "[game/]myScript.rpy" will be proceeded before "[game/]script.rpy", that will be proceeded before "[game/]scripts/init.rpy".

Basically speaking, each time an init level is finished, Ren'py start a new init loop, processing the files in alphabetical order for the next init level.

Therefore, if you have :
myScript.rpy
Code:
init 10 python:
    _log( "myScript - level 10" )

init python:
    _log( "myScript - level 0" )

init -1 python:
    _log( "myScript - level -1" )
scripts/init.rpy
Code:
init -1 python:
    _log( "init - level -1" )

init python:
    _log( "init - level 0" )

init 1 python:
    _log( "init - level 1" )
scripts.rpy
Code:
init -100 python:
    logFile = renpy.os.path.join( config.basedir, 'myLog.txt' )

    def _log( msg ):
        FH = open( logFile, "a" )
        FH.write( msg + "\n" ) )
        FH.close()

init -10 python:
    _log( "script - level -10" )

init python:
    _log( "script - level 0" )

init 1 python:
    _log( "script - level 1" )
This will give you the following order :
  • script - level -10
  • myScript - level -1
  • init - level -1
  • myScript - level 0
  • script - level 0
  • init - level 0
  • script - level 1
  • init - level 1
  • myScript - level 10


So, i tried to define my variables in labels
in the a.rpy file, i create a label loadFileA and in the b.rpy file, i create a label loadFileB
And in start label, i call loadFileB first, then loadFileA
and i obtain a poor "variable is being given a default a second time."
default (like define) create its own context. Something like :
Code:
label whatever:
    default myVar = "its value"
Have absolutely no meaning for Ren'py. It will still proceed the default statement at the start of the game and after each save loading.


I searched in renpy documentation and it seems you can control the order of declaration with different ways of init, but just for the "define" declarations.
Nothing for default variables.
Because, like I said yesterday, default is proceeded at a very particular time.
There's one and only one moment when the game start, like there's one and only one moment when a save file end being loaded.

What you need is to delay the moment you use the variable, not the moment you declare it.[/code]
 
Last edited:

GoldenD

Member
Sep 30, 2018
102
70
Ok,
just need to familiarize myself with the init process .
Thanks a lot.

I take a nap, in real life and i continue.