How to make a screen of a list in a class object

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
Deimos96
Regular
Posts: 26
Joined: Tue Nov 15, 2022 5:34 am
Contact:

How to make a screen of a list in a class object

#1 Post by Deimos96 »

I have two classes - Girl which I only pass self & Trait which I pass a name and effect parameters which I then assign to the Girl:

Code: Select all

class Girl(object):
        def __init__(self):
            self.gen_girl() # I generate the girl's name and stats here
            self.traits = []

class Trait(object):
        def __init__(self, name, effects):
            self.name = name
            self.effects = effects
then I define a list of traits:

Code: Select all

default TRAITS = [
	Trait("Beautiful", "effect"),
	Trait("Charming", "effect"),
	Trait("Witty", "effect"),
	Trait("Elegant", "effect")
]
I assign a randomizer using:

Code: Select all

self.traits = [renpy.random.sample(TRAITS, 2)] 
to take two random traits from the TRAITS list.

I want to get a list for the traits that were assigned, girl1 is an instance of Girl:

Code: Select all

screen test:
	vbox:
        	xalign .5
        	yalign .5
		for trait in girl1.traits:
            		text "Traits: {}.".format(trait.name)
But whenever I try to run the game it gets an error:
AttributeError: 'list' object has no attribute 'name'.

I'm still learning the ropes so if there are any tips on how to fix this I would appreciate it a lot. Thank you.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to make a screen of a list in a class object

#2 Post by Ocelot »

Code: Select all

self.traits = [renpy.random.sample(TRAITS, 2)] 
sample returns a list. You are wrapping it in another list.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: No registered users