How to auto-generate instances of 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
JaydenAndressen
Newbie
Posts: 20
Joined: Wed Jul 15, 2020 11:18 am
Contact:

How to auto-generate instances of a class?

#1 Post by JaydenAndressen »

How to add Class properties to Auto Generated Variables?

Similar question to what I asked here: viewtopic.php?f=8&t=60226&hilit=PinkEng ... 8e7ae08de6

How do I do the same thing, but apply it to Class Variables? Example:
I have this Class:

Code: Select all

    class obj(store.object):
        def __init__(self, name, img, xpos, ypos):
            self.name = name
            self.img = img
            self.xpos= xpos
            self.ypos= ypos
And I want assign this onto all of the "Automatically Generated Variables" How do I do this? And how do I assign different variables to each one? (Like giving each variable different xpos and ypos)

This is the code from my prior question provided by hell_oh_world:

Code: Select all

init python hide:
  for i in range(10):
    c = "c{}".format(i + 1) # names the variable such as c1, c2, c3 ~ c10
    setattr(store, c, Character(c))

label start:
  c1 "I'm [c1]"
  c2 "I'm [c2]"
Last edited by JaydenAndressen on Fri Oct 02, 2020 5:07 pm, edited 1 time in total.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: add Class properties to Auto Generated Variables?

#2 Post by hell_oh_world »

I think a better title for this is "How to auto-generate instances of a class?", that's if I understood the problem correctly.
Basically you want to create many instances of this class? Is that what you want? Like...

Code: Select all

var1 = obj("name", "image", 0.5, 0.5)
var2 = obj("name", "image", 0.75, 0.5)
var3 = obj("name", "image", 0.45, 0.35)
...
If that's the case, then you actually going to change just one part of the code that I gave in order to achieve that and that's quite trivial. Just in case that if some part of the code is quite blurry to understand, I might as well explain the code that I did back there for you to understand it well.

Code: Select all

init python hide:
  for i in range(10):                                       # we are looping in this part, so the code below this block is expected to run 10 times because of range(10)

    c = "c{}".format(i + 1)                                 # the string name of the variable, such as c1, c2, c3 ~ c10

    setattr(store, c, Character(c))                         # we use setattr function to dynamically create a variable or attribute of the store
                                                            # setattr is called in this kind syntax: setattr(object, attribute_name, attribute_value)
                                                            # store is where all the variables that you create in renpy get stored, store is actually an object that got instantiated (e.g. store = StoreModule())
                                                            # attribute_name is a string name for the supposed to be created/set attribute of the object
                                                            # attribute_value is any object/value that we want to assign to the attribute of that object
As long as you know what the parameters are for the setattr function, you wont get lost.
So to answer the question (If i understood it right), this is what you actually need to do (something like)...

Code: Select all

setattr(store, c, obj("name", "image", 0.5, 0.5))
And how do I assign different variables to each one? (Like giving each variable different xpos and ypos)
As for that, how do you plan to assign the xpos and ypos though? you mean like give them a random xpos and ypos? If yes, then you can use the random module of renpy.

Code: Select all

rxpos = renpy.random.choice([0.0, 0.25, 0.5, 0.75, 1.0])    # randomly get a random xpos from the list
rypos = renpy.random.choice([0.35, 0.45, 0.55])
setattr(store, c, obj("name", "image", rxpos, rypos))

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: add Class properties to Auto Generated Variables?

#3 Post by Remix »

Note:

Characters should be in the character namespace, not store... So:

setattr( character, c, Character(c) )
Frameworks & Scriptlets:

User avatar
JaydenAndressen
Newbie
Posts: 20
Joined: Wed Jul 15, 2020 11:18 am
Contact:

Re: add Class properties to Auto Generated Variables?

#4 Post by JaydenAndressen »

Interesting, now if I wanted to add all of those generated variables into a list automatically, how would I get them in there?
Instinctually I want to add this in there: mylist.append(TL)
But this obviously doesn't work. How do I make it work?

Code: Select all

init python hide:
    mylist = []
    for i in range(10):                            
        c = "c{}".format(i + 1)                              
        rxpos = renpy.random.choice([0.0, 0.25, 0.5, 0.75, 1.0]) 
        rypos = renpy.random.choice([0.35, 0.45, 0.55])
        setattr(store, c, obj("name", "img", rxpos, rypos))
        mylist.append(TL)
        
screen display:
    for i in mylist: 
        imagebutton idle i.img xpos i.rxpos ypos i.rypos at tilesize_small
        label _("[i.name][i.rxpos][i.rypos]") ypos i.rxpos xpos i.rypos 
hell_oh_world, I'll take you up on the name change for the thread, "How to auto-generate instances of a class?"

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to auto-generate instances of a class?

#5 Post by hell_oh_world »

What is TL anyway? I don't see any part of your code where you defined it, and you are trying to append it even if that's the case, it would throw an error if you'll do that.

Code: Select all

init python hide:
  store.mylist = []
  ...
     setattr(store, c, obj("name", "img", rxpos, rypos))
     store.mylist.append(getattr(store, c))
And if this is what you are planning all along to contain them inside a list so that you can loop through them and not to access them individually through name like var1, var2, then might as well to remove setattr, and just append the instance directly,

Code: Select all

store.mylist.append(obj("name", "img", rxpos, rypos))

User avatar
JaydenAndressen
Newbie
Posts: 20
Joined: Wed Jul 15, 2020 11:18 am
Contact:

Re: How to auto-generate instances of a class?

#6 Post by JaydenAndressen »

My bad, I've been using 'TL' instead of 'c' in my game. I try to stick with 'c' on the forum so not to confuse people. But it would've been mylist.append(c)

And your guess is 100% correct. I was wanting to loop through all the variables.

Now by applying this new info, the code now looks like this:

Code: Select all

init python hide:
    mylist = []
    for i in range(10):                            
        c = "c{}".format(i + 1)                              
        rxpos = renpy.random.choice([1,2,3,4,5,6,7,8,9]) 
        rypos = renpy.random.choice([1,2,3,4,5,6,7])
	store.mylist.append(obj("name", "img", rxpos, rypos))

screen display:
    for i in mylist: 
        imagebutton idle i.img xpos i.rxpos ypos i.rypos
        label _("[i.name][i.rxpos][i.rypos]") ypos i.rypos xpos i.rxpos 
I'm testing it now, works perfectly. Thank you.

Post Reply

Who is online

Users browsing this forum: voluorem