How to print multiple instances of the same 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
Banana
Newbie
Posts: 14
Joined: Mon May 02, 2016 5:26 pm
Tumblr: waitingformycoffin
Contact:

How to print multiple instances of the same class?

#1 Post by Banana »

Code: Select all

init python: 
    class Cast:
        def __init__(self, name, location, pl, pp, ip):
            self.name = name
            self.location = location
            self.pl = pl
            self.pp = 0
            self.ip = 0
        
        def role(self, text):
            self.role = text
            
# The game starts here.
label start:
    
    menu:
        "Load the characters, please.":
            jump cast

label cast:
    "We're creating nine characters for now. We'll add more later."
    
    python: 
        sm = Cast("Shrine Maiden", "Shrine", 2, 0, 0)
        gs = Cast("Girl Student", "School", 3, 0, 0)
        bs = Cast("Boy Student", "School", 2, 0, 0)
        ow = Cast("Office Worker", "City", 2, 0, 0)
        po = Cast("Police Officer", "City", 3, 0, 0)
        do = Cast("Doctor", "Hospital", 3, 0, 0)
        rg = Cast("Rich Girl", "School", 1, 0, 0)
        pa = Cast("Patient", "Hospital", 2, 0, 0)
        ib = Cast("Information Broker", "City", 2, 0, 0)
        
    "Check to see if it worked."
    menu:
        "Check":
            "[sm.name] has been born in [sm.location.] with a Paranoia Limit of [sm.pl]. It has [sm.pp] Paranoia Points and [sm.ip] Intrigue Points."
            jump check2
I want to display a series of lines saying the following:

"Shrine Maiden has been born in Shrine with a Paranoia Limit of 2. It has 0 Paranoia Points and 0 Intrigue Points."
"Girl Student has been born in School with a Paranoia Limit of 3. It has 0 Paranoia Points and 0 Intrigue Points."

...and so on until I've described all the characters.

I know the obvious solution is to do the following:

Code: Select all

            "[sm.name] has been born in [sm.location.] with a Paranoia Limit of [sm.pl]. It has [sm.pp] Paranoia Points and [sm.ip] Intrigue Points."
            "[gs.name] has been born in [gs.location.] with a Paranoia Limit of [gs.pl]. It has [gs.pp] Paranoia Points and [gs.ip] Intrigue Points."
...and so on until I've described all the characters.

But is there at all a better way to do this? For example, if I want to add about 100 different instances of the Cast class, it would be a huge pain to manually copy and paste and then change all the variables.

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: How to print multiple instances of the same class?

#2 Post by theCodeCat »

Without getting too fancy you could add a function to your class like this:

Code: Select all

def getDescription():
    return "%s has been born in %s with a Paranoia Limit of %d. It has %d Paranoia Points and %d Intrigue Points." % (self.name,self.location,self.pl,self.pp,self.ip)
which generates the line (or "string" if you want to be more technical) and then print it by writing:

Code: Select all

$ tempLine = sm.getDescription()
"[tempLine]"

User avatar
theCodeCat
Regular
Posts: 62
Joined: Sun Sep 06, 2015 8:40 pm
Projects: Lucid9, Mystic Destinies: Serendipity of Aeons
Skype: theCodeCat
Contact:

Re: How to print multiple instances of the same class?

#3 Post by theCodeCat »

Actually on second thought, what I posted before doesn't address the main problem you have, which is there will be a lot of characters.

What you really need here is to put your character objects into a list and then loop over the list.
Something like:

Code: Select all

    python:
        sm = Cast("Shrine Maiden", "Shrine", 2, 0, 0)
        gs = Cast("Girl Student", "School", 3, 0, 0)
        bs = Cast("Boy Student", "School", 2, 0, 0)
        ow = Cast("Office Worker", "City", 2, 0, 0)
        po = Cast("Police Officer", "City", 3, 0, 0)
        do = Cast("Doctor", "Hospital", 3, 0, 0)
        rg = Cast("Rich Girl", "School", 1, 0, 0)
        pa = Cast("Patient", "Hospital", 2, 0, 0)
        ib = Cast("Information Broker", "City", 2, 0, 0)
        
        characterList = [sm,gs,bs,ow,po,do,rg,pa,ib]
        
    "Check to see if it worked."
    menu:
        "Check":
            $ i = 0
            while i < len(characterList):
                 $ currentCharacter = characterList[i]
                 "[currentCharacter.name] has been born in [currentCharacter.location.] with a Paranoia Limit of [currentCharacter.pl]. It has [currentCharacter.pp] Paranoia Points and [currentCharacter.ip] Intrigue Points."
                 $ i += 1
            jump check2
Which will print the line for every object in the characterList

Banana
Newbie
Posts: 14
Joined: Mon May 02, 2016 5:26 pm
Tumblr: waitingformycoffin
Contact:

Re: How to print multiple instances of the same class?

#4 Post by Banana »

Oh gosh, I thought I'd replied to this! Just wanted to say thank you so much for helping me out - I tried both ways and they worked great, but yes the second one is definitely much better for my purposes! Thanks to you I've gotten a better understanding of how to use loops and classes now! Thank you so, so much! :D

Post Reply

Who is online

Users browsing this forum: Bing [Bot]