Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Message
Author
xplosion
Newbie
Posts: 10
Joined: Sat Jul 21, 2018 3:49 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#61 Post by xplosion »

Does not work argh
EDIT
Seems like there is no issue if i move to another label then save, but IF i do move items to chest then immediatly save/load it doesnt save.

Code: Select all

init:
    default cookbook = list()
    default inv = Inventory("Inventory")
    default jane_inv = Inventory("Jane")
    default chest = Inventory("Storage Chest")
    default mindy_inv = Inventory("Mindy", 500, 75)

    ### Items without icons are created like this:      
    #$ quarter = Item("Quarter", "A new quarter)
    
    ### Items with icons are created like this:
    $ eye = Item(name="Eyeball", desc="A human eyeball, how creepy!", icon="images/eye.png", value=250)

    # Items that can be used in crafting
    $ but = Item("Button", "A shiny button", "images/button.png", 100, act=Show("inventory_popup", message="This item is only used in crafting"))
    
    $ yarn = Item("Yarn", "Yarny yarny yarn.", "images/yarn.png", 30, act=Show("inventory_popup", message="This item is only used in crafting"))  
    
    $ fabric = Item("Fabric", "You know, cloth.", "images/fabric.png", 100, act=Show("inventory_popup", message="This item is only used in crafting"))
    
    $ coin = Item("Coin", "An old coin", "images/coin.png", 1, act=Show("inventory_popup", message="This item is only used in crafting"))
    
    # An item with a unique action (shows screen with custom message)
    $ sword = Item("Awesome Sword", "An awesome sword.", "images/sword.png", 500, Show("inventory_popup", message="You wave the sword around wildly but nothing happens."))
    

label start:
    $ cookbook = list() 
    $ jane_inv = Inventory("Jane")
    
    #GIVE ITEMS START GAME#
    $ jane_inv.take(sword)
    $ jane_inv.take(eye)
    $ jane_inv.money = 500  
    $ mindy_inv = Inventory("Mindy", 500, 75)
    $ mindy_inv.take(eye,4)
    $ mindy_inv.take(but,3)
    $ mindy_inv.take(coin,2)
    #############
 

User avatar
enrohk
Newbie
Posts: 11
Joined: Tue Jun 19, 2018 6:32 pm
Location: Spain
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#62 Post by enrohk »

Hi !
I'm new to python but I'm using your Inventory System for my project. It works very well with others things I'm implementing.

Learning still the basic of this language (moving from C#) but i was in need of a method for returning True or False if a item exists in the inventory (for example , make a button sensitive if you have THAT item in the inventory) . And the only methods I see is for returning ID's and those stuff.
I want to share if someone find useful or even can be better written or even that method already exists and I'm missing something Hahah.
Well... Open Inventory.rpy and inside Inventory class add :

Code: Select all

def check_item(self, item):
            if self.qty(item):
                return True
            else:
                return False
This can allow to check inventory and make options or buttons if player or other inventory has that item like

Code: Select all

sensitive inv_player.check_item(item_example)
Sorry for my noobish english and noobish code ;)
Do I live in a friendly or a hostile universe? Which is it? Is it a universe that is filled with hostility and anger and people wanting to hate each other and people wanting to kill each other, is that what you see?Because when you see the world that way that’s exactly what you will create for yourself in your life.

User avatar
niannn
Regular
Posts: 57
Joined: Tue Jun 27, 2017 1:04 am
Completed: Keynote, Die.Alone?, The Villaintine
Projects: The Villaintine: Sepia
Tumblr: niaaannn
itch: niannn
Location: Philippines
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#63 Post by niannn »

xplosion wrote: Sat Jul 28, 2018 9:51 am Does not work argh
EDIT
Seems like there is no issue if i move to another label then save, but IF i do move items to chest then immediatly save/load it doesnt save.
Hey, xplosion. I'm sorry, I can't help you with that. I didn't use the merchant-like part of the system (if I understood correctly, that's what you're having troubles with, right? if I'm wrong, I'm sorry again) so I don't really know how I can help you. I hope someone can help with your issue though (alternatively, you can try posting about this problem on the 'Ren'Py Questions and Announcements' board and attach some of the code you've done so far).
Image Image ImageImage

xplosion
Newbie
Posts: 10
Joined: Sat Jul 21, 2018 3:49 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#64 Post by xplosion »

niannn wrote: Tue Jul 31, 2018 9:02 pm
xplosion wrote: Sat Jul 28, 2018 9:51 am Does not work argh
EDIT
Seems like there is no issue if i move to another label then save, but IF i do move items to chest then immediatly save/load it doesnt save.
Hey, xplosion. I'm sorry, I can't help you with that. I didn't use the merchant-like part of the system (if I understood correctly, that's what you're having troubles with, right? if I'm wrong, I'm sorry again) so I don't really know how I can help you. I hope someone can help with your issue though (alternatively, you can try posting about this problem on the 'Ren'Py Questions and Announcements' board and attach some of the code you've done so far).
I don't really know I haven't looked for a solution since it's not a gamebreaking bug I guess.
niannn wrote: Sat Jan 06, 2018 11:07 am just wondering if there's a way to solve this one?

Image

As you can see from the image, the description of an item upon hovering doesn't show up where it's supposed to be. I don't have any idea how to fix this and I would really appreciate help. Also, thank you in advance!

sidenote: I am not so well-versed with python and modified the code only a little based on the basic guidelines on how to use it (I did try to fix this with my basic understanding but I think all my attempts are not getting me anywhere and I need help). So, I'm still a rookie. I might ask again if I don't understand the answer so I hope you'll be patient with me. Once again, thank you very much in advance.
Check Inventory.rpy for this code and modify the numbers to your choice, this code fix the issue. from yalign 1.0 to 0.9.

Code: Select all

screen tooltip(item=False,seller=false):      
    if item:
        hbox:
            xalign 0.5 yalign 0.9
            if seller:
                text ("[item[0].name]: [item[0].desc] (Sell Value: " + str(calculate_price(item, seller)) + ")")
            else:
                text "[item[0].name]: [item[0].desc] (Value: [item[0].value])" 
enrohk wrote: Tue Jul 31, 2018 6:25 am Hi !
I'm new to python but I'm using your Inventory System for my project. It works very well with others things I'm implementing.

Learning still the basic of this language (moving from C#) but i was in need of a method for returning True or False if a item exists in the inventory (for example , make a button sensitive if you have THAT item in the inventory) . And the only methods I see is for returning ID's and those stuff.
I want to share if someone find useful or even can be better written or even that method already exists and I'm missing something Hahah.
Well... Open Inventory.rpy and inside Inventory class add :

Code: Select all

def check_item(self, item):
            if self.qty(item):
                return True
            else:
                return False
This can allow to check inventory and make options or buttons if player or other inventory has that item like

Code: Select all

sensitive inv_player.check_item(item_example)
Sorry for my noobish english and noobish code ;)
That's a nice addition thank you, It made checking for items much simplier, since I was planning to add stuff like a Fridge with an inventory and have a character notices you that we're out of certain items.

Code: Select all

    if fridge_inv.check_item(beer):
        "We still have some beer on the fridge if you'd like to grab one."
    else:
        "Oh snap we're out of beer, I gotta run to the store for a pack."
 
But I wish I know how to make an action happen after closing the inventory, like if I bought the beer from store, a check will happen

Code: Select all

    if myinventory_inv.check_item(beer):
        "Thanks for your purchase!."
    else:
        "See you next time!"
 

User avatar
enrohk
Newbie
Posts: 11
Joined: Tue Jun 19, 2018 6:32 pm
Location: Spain
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#65 Post by enrohk »

xplosion wrote: Sun Aug 05, 2018 6:51 am But I wish I know how to make an action happen after closing the inventory, like if I bought the beer from store, a check will happen

Code: Select all

    if myinventory_inv.check_item(beer):
        "Thanks for your purchase!."
    else:
        "See you next time!"
 
Hi! I like you find that useful ;-) I'm thinking on what you asked . And is viable ... I think :lol:

One "tricky method" is to save info on your inventory money.
For example , create a "stamp" each time you visit the shop and save the amount of money you have BEFORE BUYING.
If you have 3000$ , create a stamp like

Code: Select all

$ money_stamp = myinventory_inv.money
When the shop window is closed , make a statement to look if you have LESS money AFTER . Something like :

Code: Select all

	if money_stamp < myinventory_inv.money:
		"Thanks for your purchase!."
    	else:
        	"Purchase something!"
That should save check for individually items .... i think. Is viable that method for you ?
Do I live in a friendly or a hostile universe? Which is it? Is it a universe that is filled with hostility and anger and people wanting to hate each other and people wanting to kill each other, is that what you see?Because when you see the world that way that’s exactly what you will create for yourself in your life.

xplosion
Newbie
Posts: 10
Joined: Sat Jul 21, 2018 3:49 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#66 Post by xplosion »

enrohk wrote: Sun Aug 05, 2018 9:00 pm
xplosion wrote: Sun Aug 05, 2018 6:51 am But I wish I know how to make an action happen after closing the inventory, like if I bought the beer from store, a check will happen

Code: Select all

    if myinventory_inv.check_item(beer):
        "Thanks for your purchase!."
    else:
        "See you next time!"
 
Hi! I like you find that useful ;-) I'm thinking on what you asked . And is viable ... I think :lol:

One "tricky method" is to save info on your inventory money.
For example , create a "stamp" each time you visit the shop and save the amount of money you have BEFORE BUYING.
If you have 3000$ , create a stamp like

Code: Select all

$ money_stamp = myinventory_inv.money
When the shop window is closed , make a statement to look if you have LESS money AFTER . Something like :

Code: Select all

	if money_stamp < myinventory_inv.money:
		"Thanks for your purchase!."
    	else:
        	"Purchase something!"
That should save check for individually items .... i think. Is viable that method for you ?
I wanted something for items giving a check for a certain item if it was sold after closing the inventory

Edit

@niannn Fixed the saving issue I had using "renpy.retain_after_load()" everytime I wanted to open inventory.

also make sure to have "default mc_inv = Inventory("[mc]")" before label start.

Code: Select all

action renpy.retain_after_load(), ToggleScreen("inventory_screen", first_inventory=mc_inv, second_inventory=lucas_inv)

User avatar
enrohk
Newbie
Posts: 11
Joined: Tue Jun 19, 2018 6:32 pm
Location: Spain
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#67 Post by enrohk »

xplosion wrote: Tue Aug 07, 2018 6:36 am
I wanted something for items giving a check for a certain item if it was sold after closing the inventory
I think your answer is in the method "transaction" inside Inventory class:

Code: Select all

def transaction(seller, buyer, item):
        price = calculate_price(item, buyer)
        if buyer.money >= price:   
            seller.sell(item, price)
            buyer.buy(item, price)
        else:
            message = "Sorry, %s doesn't have enough money!" % (buyer.name)
            renpy.show_screen("inventory_popup", message=message)
Idk your level of python but mine is very basic and I just started, but maybe you can do something like that:

First create a List to add what items you buy each time you shop (this method can work with all shops also)

Code: Select all

$ buyed_items = []
and in the transaction method do something like

Code: Select all

def transaction(seller, buyer, item):
        price = calculate_price(item, buyer)
        if buyer.money >= price:   
            seller.sell(item, price)
            buyer.buy(item, price)
            #mod- Add the item to a list
            buyed_items.append(item)
        else:
            message = "Sorry, %s doesn't have enough money!" % (buyer.name)
            renpy.show_screen("inventory_popup", message=message)
Just remember to add your list to the globals for use in inventory script.
later you can add a mehod that checks for items in that list. something like the mine for check for true :

Code: Select all

def check_item(item):
	global buyed_items
                for i in buyed_items:
                    if i[0] == item:        
                        return True
                    else:
                        return False

I just wrote that code without checking , but the concept is that :
- Add the item to a list if you buy
- When shop is closed run the check item method looking for the item
- Do something if find
- Clear list (for don't take problems next time you buy)

With that you can have custom actions sayed if items are buyed , something like : "Remember to put those in the fridge !" or "That cheese comes from a local place, enjoy it ! "
Do I live in a friendly or a hostile universe? Which is it? Is it a universe that is filled with hostility and anger and people wanting to hate each other and people wanting to kill each other, is that what you see?Because when you see the world that way that’s exactly what you will create for yourself in your life.

User avatar
niannn
Regular
Posts: 57
Joined: Tue Jun 27, 2017 1:04 am
Completed: Keynote, Die.Alone?, The Villaintine
Projects: The Villaintine: Sepia
Tumblr: niaaannn
itch: niannn
Location: Philippines
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#68 Post by niannn »

xplosion wrote: Sun Aug 05, 2018 6:51 am Check Inventory.rpy for this code and modify the numbers to your choice, this code fix the issue. from yalign 1.0 to 0.9.

Code: Select all

screen tooltip(item=False,seller=false):      
    if item:
        hbox:
            xalign 0.5 yalign 0.9
            if seller:
                text ("[item[0].name]: [item[0].desc] (Sell Value: " + str(calculate_price(item, seller)) + ")")
            else:
                text "[item[0].name]: [item[0].desc] (Value: [item[0].value])" 
Been busy and unable to log in here, but I'm glad I did now! Thank you very much for helping me out with this one! I shall keep this code for future use :)
xplosion wrote: Tue Aug 07, 2018 6:36 am Edit

@niannn Fixed the saving issue I had using "renpy.retain_after_load()" everytime I wanted to open inventory.

also make sure to have "default mc_inv = Inventory("[mc]")" before label start.

Code: Select all

action renpy.retain_after_load(), ToggleScreen("inventory_screen", first_inventory=mc_inv, second_inventory=lucas_inv)
And I'm glad that it worked out for you! This code looks useful, thanks a lot for sharing :D
Image Image ImageImage

Mr Peanutbutter
Newbie
Posts: 2
Joined: Thu Nov 01, 2018 4:42 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#69 Post by Mr Peanutbutter »

Thanks for the great code.

I am having an issue and not sure why I am having this issue and not other people. The problem is when I save the inventory, when I reload it the "==" operate gives false for the same item. So for the recipe checks it fails even if I have the right items in the inventory.

I believe what is happening is that when the save is loaded back up it references a different object (memory location) than the objects being defined. So without saving and loading it works because it is referencing the same item object in memory but when it reloads it's pointing to two different ones and then the "==" for the item fail.

if I add an "__eq__" function that checks the names, then it works. However I would rather not do it this way because I would like the ability to change items in the future. So what I could do is instead make a look up table and have the just an item key in the inventory then would look the item up in a global lookup table that could then be changed in the future updates. Just they keys would have to remain the same.

But it keeps bugging me that no one is having a similar issue with this reload from save? Could I be doing something wrong? Is it just a bug no one else has noticed?

** Update**
Looking into this more I believe I know why I am having this problem. I am using "define" for initializing my items but "default" for initializing the inventory.

I am not too sure what would be the best solution to fix this issue. The demo code just initializes everything after the "start:" label (which my understanding is the same as using "default") and I would not want to do that because I want to later make new versions of my game where I can then modify the items and if those get saved with the game it would make that tricky.

So seems to me my best bet is to use "define" to make a lookup dictionary for the items(what I talked about previously) and have the inventory use keys to then look these up. So then I could change the lookup dictionary of items while the inventory list would save with the keys to look up the items that could be changed in the future.

At least I understand why my code wasn't working and other people's code is.
Last edited by Mr Peanutbutter on Fri Nov 02, 2018 3:15 am, edited 1 time in total.

Mr Peanutbutter
Newbie
Posts: 2
Joined: Thu Nov 01, 2018 4:42 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#70 Post by Mr Peanutbutter »

also I think "check recipe" would be more clearer and faster like this:

Code: Select all

        
        def check_recipe(self, item): # verify all ingredients are in inv
            for i in item.recipe:
                if self.qty(i[0]) < i[1]:
                    return False
            return True

lynx13
Newbie
Posts: 2
Joined: Sun May 05, 2019 12:37 am
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#71 Post by lynx13 »

Hello.
I want to thank you for this great inventory code! :)

And since I'm new to programming, I ask for your help ..

1. How to make sure that some items are not sold in the trade mode?
setting the value of 0 as well as the complete absence of value does not help, and the values False, None give an error ..

2. Is it possible to make an inventory limit on the weight of items?
I think that the weight will need to be written in the line “$ sword = Item (" Awesome Sword "," An awesome sword. "," images / sword.png ", weight==150, 500, Show (" inventory_popup ", message =" You wave the sword around wildly but nothing happens. "))”, but I don’t know where and how to indicate the meter itself.

thank you in advance for your help :)

p.s. sorry my english i use online translator

lynx13
Newbie
Posts: 2
Joined: Sun May 05, 2019 12:37 am
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#72 Post by lynx13 »

Hello again :)

I want to share with you the fact that I managed to independently deal with my first question about the ban on selling items at a price = 0 (maybe someone else will come in handy).
I changed this code snippet a bit by adding a cost check.

Code: Select all

    def transaction(seller, buyer, item):
        price = calculate_price(item, buyer)
        if buyer.money >= price:   
            seller.sell(item, price)
            buyer.buy(item, price)
        else:
            message = "Sorry, %s doesn't have enough money!" % (buyer.name)
            renpy.show_screen("inventory_popup", message=message)
Now the code looks like this, and I like how it works :)

Code: Select all

    def transaction(seller, buyer, item):
        price = calculate_price(item, buyer)
        if price <= 0:
            message = "it is not for sale"
            renpy.show_screen("inventory_popup", message=message)
        else:
            if buyer.money >= price:
                seller.sell(item, price)
                buyer.buy(item, price)
            else:
                message = "Sorry, %s doesn't have enough money!" % (buyer.name)
                renpy.show_screen("inventory_popup", message=message)
but my second question (about inventory limitation by weight of items) I still cannot solve ..
I tried to do the same as the money works in the inventory, but nothing happened .. :(
it is possible that this idea was not originally correct, but I would be completely satisfied if the weight of the objects worked this way.
maybe you can suggest other ideas?

camzgr8game
Newbie
Posts: 12
Joined: Fri Sep 16, 2022 9:32 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#73 Post by camzgr8game »

This code is pretty great, I was hoping for some help, I want to have a chest (Called sellchest) that the user can put items into and then at the end of the day it auto sells off every item of a specific type. In this case gift. I was hoping that the the chest could sell off the contents matching the item type gift but nothing else that's in the chest that isn't item type gift. And it then has the money in it the next day. Ie On Monday you put in an apple (worth 5 coin), and on Tuesday it will be empty but will have 5 coins available to take.

I tried creating an "autosell" def in the inventory file:

Code: Select all

def autosell(seller, cat):
        for item in seller.inv:
            value = item[0].value
            qty = str(item[1])
            type = item[0].type
            if item[0].type == cat:
                price = item[0].value
                return int(price)
                self.withdraw(price)
                self.drop(item[0])
(cat being category of item)

And then what I have is when the day ends it calls a label that does a bunch of other calculations. I use:

Code: Select all

$ Function(autosell(sellchest,"gift"))
$ generalstoredayprofit = sellchest.money
to try and sell off the items and then later in that end of day label, it calls a screen that displays the value of the sellchest inventory by using the [generalstoredayprofit].

Thanks all, I'm just scratching my head as this is my first step into python and I'm not quiet there yet.

camzgr8game
Newbie
Posts: 12
Joined: Fri Sep 16, 2022 9:32 pm
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#74 Post by camzgr8game »

camzgr8game wrote: Mon Nov 21, 2022 6:33 am I tried creating an "autosell" def in the inventory file:

I found a solution, its probably dodgy, but seems to work for me. I haven't tested it thoroughly yet, but initial tests seem to point to it working.

Code: Select all

def autosell(seller, selltype):
        for item in seller.inv:              
            amount = item[1]
            price = item[0].value * amount
            if item[0].type == selltype:
                seller.withdraw(price)
                seller.drop(item[0])

thexerox123
Regular
Posts: 127
Joined: Fri Jan 20, 2023 3:21 pm
itch: thexerox123
Contact:

Re: Infinite, Stackable Inventory/Crafting/Vendor - UPDATED v1.5

#75 Post by thexerox123 »

This code is wonderful, I've implemented it in a game that I'm putting together.

I'm wondering, would there be an easy way to implement an ability to disassemble crafted items (or rather, items that have a recipe defined?)

I have most items give a description pop-up, but I've also made a dev tool watch item that brings up a menu that allows us to change day cycle variables, so I know that act=Jump can be used for presenting multiple options with an item's use, if that needs to be the way it's done.

But I was also thinking it would be neat if I could have a hammer item in the inventory that, when used on an item with a recipe, will disassemble it.

I'm just not quite sure yet how to code that.

Post Reply

Who is online

Users browsing this forum: No registered users