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.
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