1

in a Ren'Py game I'm coding, I'm trying to optimize my code, and I'm stuck at finding a way to reset a the same value for all instances of a same class.

Here's my code :

init python:
    class Girl():
        def __init__(self,name,age):
            self.name = name
            self.age = age
            self.place = "Nowhere"
            self.appear = False

   Bree = Girl("Bree",26)
   Sasha = Girl("Sasha",27)

label reset_appear():
    Bree.appear = False
    Sasha.appear = False

For now I only have a few instances of that class, but I'm planning on adding something like 50 more, and I wanted to fix that before continuing.

I thought of doing something like this (still while in class):

def reset_appear(self):
    self.appear = False

But you'll still need to call it for each instance. I also thought of the same function outside of the class, but I don't know how to code it.

2 Answers2

1

You can use a class attribute to store all the instances and a classmethod to iterate over all the instances:

class Girl:
    _instances = []
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.place = "Nowhere"
        self.appear = False
        self._instances.append(self)

    @classmethod
    def reset_appear(cls):
        for instance in cls._instances:
            instance.appear = False

    def __repr__(self):
        # added for sake of example
        return '{} {}'.format(self.name, self.appear)


Bree = Girl("Bree", 26)
Bree.appear = True
Sasha = Girl("Sasha", 27)
Girl.reset_appear()
print(Girl._instances)
#  [Bree False, Sasha False]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • The added `classmethod` is nice. – Graipher Jan 06 '19 at 17:07
  • Thanks for your answer, but since I'm a beginner in Python, I'm not yet familiar with @classmethod; __repr__; (cls);and .format By comparing your answer with @Graipher's answer, I'm thinking : isn't that the "almost" same method as he listed ? I'll need to work more on this concept for a better understanding :) – Phoenix iz Fire Jan 06 '19 at 18:51
0

I'm not familiar with Ren'Py, so my examples are in normal Python.

You need somewhere a collection of all instances of the Girl class. This could be either with a plain list:

class Girl():
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.place = "Nowhere"
        self.appear = False

Bree = Girl("Bree", 26)
Sasha = Girl("Sasha", 27)
girls = [Bree, Sasha]

for girl in girls:
    girl.appear = False

Or you could have the class handle it:

class Girl():
    instances = []

    def __init__(self,name,age):
        Girl.instances.append(self)
        self.name = name
        self.age = age
        self.place = "Nowhere"
        self.appear = False

for girl in Girl.instances:
    girl.appear = False
Graipher
  • 6,891
  • 27
  • 47
  • Ren'Py can use Python code anytime just by using "init python" or "python" (for calls), which is why I'm using Python :) Anyway, your second method is the simplest and it works perfectly. Thanks a lot ! Your first method looks great, but since I'll require to add over 50 girls, it would have been a source of mistake to use a manual method to create a list. You see, I try to make my code the more automatic possible to make it easier for future developpments :) (at first, I wasn't even using the Class system, and I've like 20 parameters for that class, so this was a complete mess ^^) – Phoenix iz Fire Jan 06 '19 at 18:47