Python: How modificate a value of all the objects from a class ?

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Python: How modificate a value of all the objects from a class ?

#1 Post by renardjap » Sat Apr 25, 2020 4:06 pm

Hello,

I have a question about python's classes. Is it possible to modificate a value of all the objects from a class ???

Exemple

Code: Select all

class Perso:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def older(self):
        self.age += 1


patrick = Perso("Patrick", 20)
michel = Perso("Michel", 25)
#and more guys

Perso.older() # I want the value age increment for all the characters who belongs to the class Perso... but doesn't work... 

print(patrick.age)
print(michel.age)
I want to create a function to let increment the self.age value of all the characters of the class Perso.

Is it possible ?
How can I do ?

Thanks for your answers :D

User avatar
MaydohMaydoh
Regular
Posts: 165
Joined: Mon Jul 09, 2018 5:49 am
Projects: Fuwa Fuwa Panic
Tumblr: maydohmaydoh
Location: The Satellite of Love
Contact:

Re: Python: How modificate a value of all the objects from a class ?

#2 Post by MaydohMaydoh » Sat Apr 25, 2020 4:51 pm

If it was me, I would stick all the new instances into a dictionary and run through them all in a for loop.

Code: Select all

perso_dict = {
    "patrick" : Perso("Patrick", 20),
    "michel" : Perso("Michel, 25)
    }
    
for k in perso_dict:
    perso_dict[k].older()

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Projects: The Button Man
Organization: NILA
Github: hell-oh-world
Location: Philippines
Contact:

Re: Python: How modificate a value of all the objects from a class ?

#3 Post by hell_oh_world » Sat Apr 25, 2020 8:56 pm

renardjap wrote:
Sat Apr 25, 2020 4:06 pm
Hello,

I have a question about python's classes. Is it possible to modificate a value of all the objects from a class ???

Exemple

Code: Select all

class Perso:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        
    def older(self):
        self.age += 1


patrick = Perso("Patrick", 20)
michel = Perso("Michel", 25)
#and more guys

Perso.older() # I want the value age increment for all the characters who belongs to the class Perso... but doesn't work... 

print(patrick.age)
print(michel.age)
I want to create a function to let increment the self.age value of all the characters of the class Perso.

Is it possible ?
How can I do ?

Thanks for your answers :D

Code: Select all

class Perso:

    old = 0 # you declare class variables outside the methods.

    def __init__(self, name, age):
        self.name = name
        self._age = age + self.old
        
    @classmethod
    def older(cls, value=1):
        cls.old += value
       
patrick = Perso("Patrick", 20)
michel = Perso("Michel", 25)

# print(patrick.age)
# >>> 20
# patrick.older() # or michel.order() or Perso.older() or patrick.old += 1 ....
# print(patrick.age)
# >>> 21
# print(michel.age)
# >>> 26
You use class attributes for that. They are shared across all instances of a class. object attributes are those variables that you declared inside the constructor / init method. Then you can make a classmethod or a normal method whichever you prefer that will increment the class attribute. In this case, a classmethod makes more sense. You can also directly increment it by accessing the class attribute directly.

drKlauz
Veteran
Posts: 237
Joined: Mon Oct 12, 2015 3:04 pm
Contact:

Re: Python: How modificate a value of all the objects from a class ?

#4 Post by drKlauz » Sat Apr 25, 2020 10:43 pm

I would use something like

Code: Select all

default global_age=0

init python:
  class Perso(object):
    def __init__(self,name,age):
      self.name=name
      self._age=age
    @property
    def age(self):
      return global_age+self._age
    @age.setter
    def age(self,age):
      self._age=age

label new_year:
  $global_age+=1
  "Happy New Year!"
  return
Having global dict of all characters also good idea, tho this may depend on how you structure your game.
Class attributes shouldn't be used for such things, as RenPy do not save them.
I may be available for hire, check my thread: viewtopic.php?f=66&t=51350

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: Python: How modificate a value of all the objects from a class ?

#5 Post by renardjap » Sun Apr 26, 2020 4:13 am

Thank you very much all of you for your answers !

@ MaydohMaydoh: I have the same idée but I search something more Object Oriented Programming

@ hell_oh_world: It 's very interesting to use this @classmethod

@ drKlauz: It's a problem if renpy doesn't save it ! I need to keep it in the memory

Post Reply

Who is online

Users browsing this forum: Google [Bot]