[SOLVED] Help Checking a List from a Class Instance inside a Function

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
LateWhiteRabbit
Eileen-Class Veteran
Posts: 1867
Joined: Sat Jan 19, 2008 2:47 pm
Projects: The Space Between
Contact:

[SOLVED] Help Checking a List from a Class Instance inside a Function

#1 Post by LateWhiteRabbit »

Okay, so I'm not even sure if I'm phrasing this question correctly, but my Google-fu and Python terminology have failed to net me any information that works for what I am trying to do, and you all were super helpful with my last coding roadblock, so here goes:

Code: Select all

init python:
    import renpy.store as store
    
    class Clothing(store.object):

        def __init__(self, name, description, aspects, location, amount = 1):
            self.name = name
            self.description = description
            self.aspects = []              
            self.location = location       
            self.amount = amount
            
     class Inventory(store.object):

        def __init__(self, name):
            self.name = name

        def equip(self, clothing_item):
            if clothing_item.location == "head":
                store.head_slot_name = clothing_item.name
                store.head_slot_descrip = clothing_item.description
            if clothing_item.location == "torso":
                store.torso_slot_name = clothing_item.name
                store.torso_slot_descrip = clothing_item.description
                
default ball_cap = Clothing("ball cap", "a worn baseball cap", ["casual"], "head", 1)
default tshirt = Clothing("T-shirt", "a plain yellow shirt", ["casual"], "torso", 1)
default top_hat = Clothing("top hat", "a posh top hat", ["fancy", "silly"], "head", 1)
default head_slot_name = ""
default head_slot_descrip = ""
default player_style = ""
default player_outfit = Inventory("player")

label start:
  
    $ player_outfit.equip(ball_cap)

    "The thing on your head slot is [head_slot_name]. It is [head_slot_descrip]."
That all works, thanks to Ocelot from my last thread.

The problem I am having is that I can't seem to find a way to check what is inside the Aspects list in my Clothing class instances when I am passing them into a function like equip up there.

Code: Select all

def style_check(self, clothing_item):
            if "casual" in clothing_item.aspects:
                store.player_style = "Relaxed"
            else:
                store.player_style = "This didn't work."
The conditional statement in the function always returns false. I just don't know HOW to correctly check for certain values inside a list INSIDE a class instance that I've passed to a method. I have tried all the ways I know to check a list:

Code: Select all

def style_check(self, clothing_item):
            for i in clothing_item.aspects:
            	if (i == "casual"):
                    store.player_style = "Relaxed"
            else:
                store.player_style = "This didn't work."
Doesn't work. It returns false.

Code: Select all

def style_check(self, clothing_item):
            if ("casual" in clothing_item.aspects):
                store.player_style = "Relaxed"
            else:
                store.player_style = "This didn't work."
Ditto. Doesn't work. Returns false.

Ideally I'd like to be able to check the aspects list in every equipment slot worn by the player, tally the number of times a certain value like "casual" appears, and set the player's style based on the value that appears the most between them all.

But I'm apparently missing something fundamental about how to check list values from a passed instance inside a method.

Code: Select all

label start:

    $ player_outfit.equip(ball_cap)

    "The thing on your head slot is [head_slot_name]. It is [head_slot_descrip]."

    $ player_outfit.style_check(ball_cap)

    "Your style is [player_style]."

Results in:

Code: Select all

"The thing on your head slot is ball cap. It is a worn baseball cap."

"Your style is This didn't work.."
So, what I am missing here?
Last edited by LateWhiteRabbit on Sun Nov 28, 2021 11:36 pm, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Help Checking a List from a Class Instance inside a Function

#2 Post by philat »

self.aspects = []

self.aspects = aspects

User avatar
LateWhiteRabbit
Eileen-Class Veteran
Posts: 1867
Joined: Sat Jan 19, 2008 2:47 pm
Projects: The Space Between
Contact:

Re: Help Checking a List from a Class Instance inside a Function

#3 Post by LateWhiteRabbit »

philat wrote: Sun Nov 28, 2021 11:02 pm self.aspects = []

self.aspects = aspects
That works . . . .
Image

I can't believe how much time I spent working on the wrong end of the problem. It never occurred to me that my initializing of the class attribute had an error in it.

I can't describe the emotion I am feeling with the solution to my problem being so easy you could solve it with four words. :shock:

Thanks, philat.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: [SOLVED] Help Checking a List from a Class Instance inside a Function

#4 Post by philat »

No worries, that's just programming. Debugging for hours until you realize that there was a typo.

Post Reply

Who is online

Users browsing this forum: No registered users