Creating a 'smart' Inventory by comparing lists (using Python) ...

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
danasavage
Newbie
Posts: 9
Joined: Thu Jun 11, 2020 9:28 am
Projects: Girl Games
Contact:

Creating a 'smart' Inventory by comparing lists (using Python) ...

#1 Post by danasavage »

Note: I tried asking an initial version of this question last night but ended up accidentally writing a novella, plus I faced some teething issues and I think my main question got lost in the word soup ... The original post is still active currently - Im happy to take it down, I wasn't quite sure what the etiquette was, but figured maybe somebody might find something useful in it so left it up for now ....

And now here is my shiny fresh second attempt at asking this question, which is basically a Python question - but in relation to Ren'py/Inventories.

So what I have so far is a working Inventory system which uses Objects and Classes. I've cobbled it together using parts from Saguaro's 'Infinite, Stackable Inventory' plus a little of Pippin123's Dynamic Dress Up Framework thrown in for good measure and so far it's doing what I want it to. I have a 'Wardrobe' screen that lists both what my Player(Inventory) is wearing and also what the Wardrobe(Inventory) contains, and if I click on any item in either list it automatically moves it from one inventory to the other ... Just as I intended.

Only issue is, say the player is wearing a t-shirt, and they click on another t-shirt from their wardrobe, it would put TWO shirts on them, when in reality they'd swap from one to the other. Or what if they were wearing pants and a shirt and they wanted to put a dress on - they'd need to take off BOTH things first, right?

Well, I have my solution for this conundrum (in theory) and now I just need to find a way of implementing it in using Python lists ... Which is where YOU come in! :)

So here's what I have so far in terms of code for my Inventory system:

Code: Select all

init python:
    import renpy.store as store


    class Clothing(store.object):
        def __init__(self,name,desc,place):
            self.name = name
            self.desc = desc
            self.place = place
            place = []   ### <--- I feel like this is going to be important later! 


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

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

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

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

    def trade(seller, buyer, item):
        seller.remove(item)
        buyer.wear(item)
And I'm creating each item of clothing like so:

Code: Select all

$ pants = Clothing(name="pants", desc="a pair of pants", place=["legs"])
$ tshirt = Clothing(name="shirt", desc="a boring old t-shirt", place=["torso"])
$ dress = Clothing(name="dress", desc="a pretty blue dress", place=["legs","torso"])
As you can see, in the Clothing objects I'm adding in a "place" list, which specifies which place on the body the item is to be worn. Which means that in theory, if my player was wearing pants and a shirt and they put on a dress - I could use Python to make sure that any items the player was currently wearing with those matching tags got sent to the wardrobe!

At present, i'm using the 'trade' function from the list above to move each item back and forth between the player and wardrobe inventories, but I know I need to make a new, more complex function - let's say it's called clothes_check.

And I know that in theory, it needs to do (roughly) the following things:

1) check the "place" tags of the new item selected
2) compare them to the "place" tags of all items the Player is currently wearing
3) move any items with matching "place" tags to the Wardrobe
4) move the new item onto the Player

Only problem is, I don't know HOW to do this. I only have a really basic beginner's knowledge of Python. I've been reading up on lists and I know that there's a number of functions that might work, ways of comparing two lists, like this StackOverflow question about comparing the common elements in two lists using Python ... But even so, translating that into a working inventory function just seems like one step beyond me!

I mean, I obviously want to figure this out on my own, but I feel like it's a three-week Python learn-at-home course away and I really wanted to get my Inventory system up and running asap! If anyone has any ideas of how to create this function, that would be totally amazing! And in the spirit of trying to figure it out on my own, I'm posting below my totally awful not-working attempt at writing the function myself, just so you can see just how far along (or not) i am in trying to wrap my head around all this!!

Code: Select all

def clothes_check(player, wardrobe, item)
        player.wear(item)
        for [place] in player.wear(item):
            if [place] in player.wearing:
                 wardrobe.wear(item)

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Creating a 'smart' Inventory by comparing lists (using Python) ...

#2 Post by trooper6 »

You might want to look into Layered images, because layered images are set up with tags and swap layers out. It is newer than Dynamic Displayables and might be useful. I don't have time to make the code for you (I'm moving soon)...but I'd encourage you to just check out the docs I link below, make a test game where you just get a layered image working and swap out some clothing parts and then look at your code and see how you might experiment to change things around. I know it isn't code, but I hope it can point you in a good direction.

Here is the Patreon tutorial on them: https://patreon.renpy.org/layeredimage-conversion.html
and here's the documentation on them: https://www.renpy.org/doc/html/layeredimage.html
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

danasavage
Newbie
Posts: 9
Joined: Thu Jun 11, 2020 9:28 am
Projects: Girl Games
Contact:

Re: Creating a 'smart' Inventory by comparing lists (using Python) ...

#3 Post by danasavage »

Haha, turns out I'm already going down that path! I've just knocked up a very basic layered image style dressup page and hoping this can do what I want it to ... Although I'm guessing I'll be back on here before long with a whole new set of questions. :) Good luck with your move!

danasavage
Newbie
Posts: 9
Joined: Thu Jun 11, 2020 9:28 am
Projects: Girl Games
Contact:

Re: Creating a 'smart' Inventory by comparing lists (using Python) ...

#4 Post by danasavage »

UPDATE: So I have looked into layered images and some of this sounds good while some of it ... I dont' know. Basically, my game is all revolving around the clothing the player wears. I need the player and other characters to react to the various clothes/outfits the player is wearing if that makes sense. So if I was using layered images, I'd either need some way for those to be triggered by a seperate wardrobe too, or I would need some way of the image choices themselves having extra true/false flags attached to them. Which I thought would be fine but I tried fooling around with the main example like so:

Code: Select all

layeredimage ethan:

    always "player"


    if outfit == 1:
        "towel"
        $ towel = True

    if outfit == 2:
        "boy_mode"
        $ towel = False


The idea being that if the player was wearing a towel they could take a shower. But unfortnately that didn't seem to work. Am I right i thinking there's no way to store other variables/flags along with each outfit choice?

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]