Ren'Py (solved) Get color list from characters list

Danv

Well-Known Member
Aug 21, 2020
1,545
2,357
428
trying to make two lists - one with all characters, other with colors of those characters

using
Python:
    char_list = []
    for name, value in store.__dict__.items():
        if isinstance(value, renpy.store.ADVCharacter) and hasattr(value, 'name') and isinstance(value.name, basestring):
            char_list.append(str(name))
i can get list of all characters, in format ['mc', 'npc1', 'npc2', ...]

now i can test grabbing name and color from the list
Python:
    test1 = None
    test2 = None
    if char_list:
        test1 = char_list[0]
        test2 = str((renpy.python.py_eval(char_list[0])).who_args["color"])
which returns test1 = 'mc' and test2 = '#FF0000', so everything still working

but when i'm trying to make list of colors
Python:
    color_list = []
    for char in char_list:
        color_list.append(str((renpy.python.py_eval(char)).who_args["color"]))
renpy dies with KeyError: u'color' error
what i'm missing here?

edit:
ffs took me two days to figure it out and after i finally decided to ask for help - 20 minutes later i found solution :FacePalm:
Python:
    for char in char_list:
        try:
            color_list.append(str((renpy.python.py_eval(char)).who_args["color"]))
        except:
            color_list.append('#adadad')
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
12,934
21,513
1,026
Python:
    for char in char_list:
        try:
            color_list.append(str((renpy.python.py_eval(char)).who_args["color"]))
        except:
            color_list.append('#adadad')

While perfectly legit code, it's better to simply test if the key "color" exist in the dictionary, than to rely on an exception:
Python:
for char in char_list:
        if "color" in renpy.python.py_eval(char).who_args:
            color_list.append(str((renpy.python.py_eval(char)).who_args["color"]))
        else:
            color_list.append('#adadad')
Also, you don't need to rely on renpy.python.py_eval() for this. You can get the ACVCharacter object directly with getattr( store, char ).


This being said, an even better approach would be to not separate the two parts, since you would then already get the ADVCharacter object:

Python:
   char_list = []
   color_list = []

   for name, value in store.__dict__.items():
        if isinstance(value, renpy.store.ADVCharacter) and hasattr(value, 'name') and isinstance(value.name, basestring):
            char_list.append(str(name))
            if "color" in value.who_args:
                color_list.append(value.who_args["color"])
            else:
                color_list.append('#adadad')

Finally, unless you intent to use this code on Ren'Py 7.x, you should replace basestring with str.
basestring do not exist in Python 3.x, and only exist in Ren'Py 8.x for compatibility reason. But there's no guaranty that it will continue to exist after PyTom will definitively drop the 7.x branch.