Ren'Py [SOLVED]Class iteration ?

GoldenD

Member
Sep 30, 2018
102
70
Hi,
have a class of gameEvents with for sample a flag which shows if the event already played today
Python:
.../...
class gameEvent:

    .../...

    self.flag = 0


default myEvent01 = gameEvent(...)

default myEvent02 = gameEvent(...)

default myEvent03 = gameEvent(...)

.../...
In game, when the event is played we update it for sample $myEvent01.flag=1

Each end of day we control all the events and reset the flag to 0
It's why i create a list of for iterate

Python:
default listEvents = [myEvent01, myEvent02, myEvent03...]

.../...

label dailyResetEvents:

    For....
My questions are :
- is there another way to do like a class iteration...
- if no, is it better to do a dico directly like this for sample
Python:
    default dicoEvents = {

    "myEvent01" : gameEvent(),

    "myEvent02" : gameEvent(),

    "myEvent03" : gameEvent(),

    .../...

    }
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,957
16,191
- is there another way to do like a class iteration...
Even if there were a class iteration, it would still be better to have you own list.

Firstly, because this iteration would have to pass through a list of [whatever] to find the objects, then discriminate them to ensure that they have the right type. And in this particular case, "[whatever]" would be all the variables handled by Python, which would be a list of around 10 000 entries for Ren'py.

Technically you can create your own class iteration, but it would like this :
Code:
init python:
    def classIter( what ):
        found = []
        for a in globals():
            if isinstance( a, what ): 
                found.append( globals()[a] )
        return found

label whatever:
    # Ren'py do NOT have a /for/ statement.
    python:
        for obj in classIter( myClass ):
           obj.whatever()
But to build the list you'll have to pass through all the global variables, which include many of Ren'py internals.

An alternative could be to use the list of savable variables to lower the number of elements in the iteration loop :
Code:
init python:
    def classIter( what ):
        found = []
        for a in renpy.python.store_dicts["store"].ever_been_changed:
            if isinstance( a, what ): 
                found.append( getattr( store, a ) )
        return found

label whatever:
    # Ren'py do NOT have a /for/ statement.
    python:
        for obj in classIter( myClass ):
           obj.whatever()
But you'll still have to deal with all the variables that aren't the expected object.

For information, with the SDK, globals() (which is the content of store have 734 entries, while there's only 8 entries in the list of savable variables.


Secondly, because in the end you'll still just iterate through a list. Except that here the list would need to be dynamically built each time you need it ; instead of be built once when you manually declare it. You would have :
Code:
for obj in classIterationCommand( myClass ):
    obj.whatever()
in place of :
Code:
for obj in myList:
    obj.whatever()
which change nothing.



- if no, is it better to do a dico directly like this for sample
It totally depend of how you intend to works with the objects.

If you intend to always blindly process them, a list is enough :
Python:
default myObjects = [ myClass(), myClass(), myClass() ]

label whatever:
    python:
        for obj in myObjects:
            obj.whatever()
But if you have to sometime discriminate the objects, then a dict will be better :
Python:
default myObjects = { "girl 1": myClass(), "girl 2": myClass(), "boy": myClass() }

label whatever:
    python:
        for k in myObjects.keys():
            # proceeed only the "girl x" objects
            if k.startswith( "girl" ):
                myObjects[k].whatever()
 
  • Like
Reactions: GoldenD

GoldenD

Member
Sep 30, 2018
102
70
Dear AON,
like always : waouh !!!

Thanks a lot for all your answers, sincerly.

From my point of view and as i understand it, working with my own "class iteration" is a lot of trouble for not much, and especially not necessarily clear in terms of code.

Actually, i thought about dict for "discriminate the objects " but each time i need discriminate, i can use the class variable directly.

Heu, in fact, i read again your answer and notice this line :
Python:
default myObjects = [ myClass(), myClass(), myClass() ]
and i noticed the difference with my code.

I create each class object for discriminate access plus a list of those object for iterate.
When you, you seem to be advising me to just create the dict (for discriminate and iterate).

Do I understand you correctly?
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,957
16,191
I create each class object for discriminate access plus a list of those object for iterate.
When you, you seem to be advising me to just create the dict (for discriminate and iterate).
Both approach are valid, once again it totally depend of what you intend to do with the objects ; the best approach being the one that will feel the easiest for you.