0

I'm putting together an inventory system for a dressup game (Python/Ren'py) and I'm stuck at the very last hurdle. I have the Inventory object, Clothing object and Items all set up like so:

class Clothing(store.object):
    def __init__(self,name,desc,place):
        self.name = name
        self.desc = desc
        self.place = place
        place = []

class Inventory(store.object):
    def __init__(self, name):
        self.name=name
        self.wearing=[]


    def wear(self,item):
        self.wearing.append(item)
        return

    def is_wearing(self, item):
        if item in self.wearing:
            return True
        else:
            return False

    def remove(self,item):
        if item in self.wearing:
            self.wearing.remove(item)
        return

    def drop(self, item):
        self.wearing.remove(item)

    def remove_all(self):
        list = [item for item in self.wearing]
        if list!=[]:
            for item in list:
                self.wearing.remove(item)
        return

player_inv = Inventory("Player")
wardrobe_inv = Inventory("Wardrobe")

jeans = Clothing(name="Jeans", desc="blue jeans", place=["legs"])
tshirt = Clothing(name="T-shirt", desc="white tee", place=["torso"])
boxers = Clothing(name="Boxer shorts", desc="white boxer shorts", place=["crotch"])
dress = Clothing(name="Dress", desc="a pretty pink dress", place=["legs", "torso"])
socks = Clothing(name="Socks", desc="sports socks", place=["shins"])
sneakers = Clothing(name="Sneakers", desc="beat up old sneakers", place=["feet"])
heels = Clothing(name="High Heels", desc="sky high stilettos", place=["feet"])
towel = Clothing(name="Towel", desc="a basic towel", place=["torso", "chest", "legs", "crotch", "shins", "feet"])

wardrobe_inv.wear(towel)
wardrobe_inv.wear(heels)
wardrobe_inv.wear(dress)

player_inv.wear(jeans)
player_inv.wear(tshirt)
player_inv.wear(boxers)
player_inv.wear(socks)
player_inv.wear(sneakers)

So all going well so far. But a super important part of the above is the 'place' tags that are set up within each clothing object. Reason being, say a player was wearing jeans (place: legs) and a tee (place: torso), and they wanted to put on a dress, they'd have to take the jeans and tee off first (so in other words I need a function that scans through those place tags and sends items with any tag that matches from the player to the wardrobe before they put on the new item ...

And i've almost done it (Well, I say 'I' - a lot of the following was actually suggested by a kind soul on another forum). But I feel my goodwill is running out there and I'm sooo close to getting this working. So yeah, here's what I have so far:

def try_on(self,item):
        for all_clothes in self.wearing:
            for matching_tags in all_clothes.place:
                if matching_tags in item.place:
                    wardrobe_inv.wear(all_clothes)
                    self.remove(all_clothes)
        self.wearing.append(item)
        wardrobe_inv.remove(item)
        return

And it's very nearly fully functional. As intended it scans through the tags and send items back to the wardrobe. Only issue is, it only sends one item back, the first item with a matching tag that it encounters. I know there must be a really basic easy way of getting the code to loop through the items in that particular section, move each matching one over to the wardrobe, then carry on with the rest. But I can't for the life of me work out what it is! I've tried making "if matching_tags in item.place:" another for loop but that seemed to make all kinds of strange funky unintended stuff happen!

(Please bear in mind that i've cobbled the inventory together from a few different examples I found online and have taken what feels like an intensive crash-course in Python over the weekend, but am still a total beginner and really out of my depth here! I just want to get my inventory working while I still have some hair left on my head!) So somebody, please - is there some way of making sure that this try_on function works as intended and sends every item with a matching 'place' tag back to the wardrobe and not just the first one it encounters?

Thanks in advance and sorry for the wall of text. I really didn't know how to explain this connundrum more succinctly! x

  • One mistake often seen (also here) is to iterate over a list with a for-loop while modifying that list in the for-loop. Always iterate over a copy which can be created by appending `[:]` to the list. – Michael Butscher Jun 19 '20 at 20:40
  • Thank you Michael, but if i maybe wasn't clear enough in my post, I'm really out of my depth here! I tried using: for all_clothes in self.wearing[:] But i'm getting an error message! Is there somewhere else in the code I should be putting [:]? – Dana Savage Jun 19 '20 at 20:50
  • This should actually work. What error message? – Michael Butscher Jun 19 '20 at 20:59
  • I suspect that you are forgetting the : for the loop statement. i.e. `self.wearing[:]:` – eNc Jun 19 '20 at 21:02
  • Ugh! I'm trying to paste in the current version but i can't get the mini-markdown 'code' to come out as anything other than a mess! But yeah, I tried [:] just on self.wearing[:] as eNc kindly suggested - error message: File "game/wardrobe.rpy", line 61: invalid syntax for all_clothes in self.wearing[:] – Dana Savage Jun 19 '20 at 21:02
  • As @eNc guessed the final colon is missing. – Michael Butscher Jun 19 '20 at 21:06

0 Answers0