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
User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

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

#1 Post by saguaro »

Updated 5/21, version 1.5 now compatible with Ren'Py 6.99.10:
Changes to screen language caused issues with the previous version of this framework. Thanks to all the updates made to Ren'Py over the past year I have been able to correct those errors and refactor the code. If you were using a previous version the only change you need to make is updating any inventory/vendor screen show statements (there is only one inventory screen now). Object classes are the same.

DEETS

I tried to make this inventory as readable and easy to use as possible. It has more features than most games will need, but you can easily ignore the features you don't want. Please note this code is provided as-is without any guarantee it will be maintained through future versions of Ren'Py.

Includes:
- Image or text items
- Quantities stack
- Create unlimited items and inventories
- Infinite scrolling grid or list layout
- Sorting by item name, quantity, value
- Vendor screen for buying/selling
- Functions for adding, removing, changing items, checking quantity, item use, item crafting, trade, and banking

crafting_screenshot.png
banking_screen.png
Inventory.rpy contains the classes and screens or you can download the demo files. Drop the demo files into a new project. Usage instructions provided in the demo are:

Code: Select all

## inventory 1.5 demo
      
label start:  
    ## If using the crafting feature, add an empty cookbook list after start to keep track of recipes
    $ cookbook = list() 
   
    ######### DEFINE INVENTORIES ##########    
    $ jane_inv = Inventory("Jane")
    
    ######### DEFINE ITEM OBJECTS ##########
    ### The format is name, description, icon image (if applicable), value (if applicable, selling/buying value), action (screen language action to be performed when icon is clicked on inventory screen), and recipe (if craftable).
    
    ### 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."))

    # An item that can be crafted has a recipe, which is a nested list of [ingredient, qty]
    $ necklace = Item("Penny Necklace", "Super magic.", "images/necklace.png", 50, recipe=[[coin,6],[yarn,1]])    
    $ doll = Item("Handmade Doll", "Guaranteed to bring luck. (Or not?) Very huggable, mind the needle.", "images/doll.png", 100000, recipe=[[but,2],[fabric,3],[yarn,1]])    
    
label demo:    
    # Display an inventory by using the inventory object name as the parameter  
    "For this demo the inventory_screen modal has been set to False (line 150 of inventory.rpy)."
    show screen inventory_screen(jane_inv)         
    
    "Let's add some items to Jane's inventory. The format is item, quantity."
    $ jane_inv.take(coin,4)
    $ jane_inv.take(sword)
    $ jane_inv.take(eye)
    $ jane_inv.take(but,2)
    $ jane_inv.take(fabric,3)
    $ jane_inv.take(yarn,2)        
      
    "You can hover over the items to see a description. If you click on the sword you will perform the action associated with that item (show a screen with a message).  You can sort inventory several ways and can switch between a grid and list view. If you're using text items you'll only want to enable the list view."  
    
    "Now, let's remove a coin."
    $ jane_inv.drop(coin)   
    
    "We can also check to see if Jane has a certain item.  The check function returns the quantity, if any."    
    if jane_inv.qty(coin): 
        $ qty = jane_inv.qty(coin)
        "Jane still has [qty] coins. Good job, Jane."
    else:
        "Jane doesn't have any coins. You must have changed this script!"   

    if jane_inv.qty(but):
        $ qty = jane_inv.qty(but)
        "Jane has [qty] buttons."
    else:
        "Jane doesn't have any buttons."
        
    "You can also change an item and modify the name, description, and icon if you need to."
    $ sword.change("Broken sword", "This sword is old and busted.", "images/broke_sword.png", 50, act=Show("inventory_popup", message="It's broken, be careful!"))
    
    "Now the sword is broken and you can't even wave it around anymore.  Let's sell it and buy something else."
    
    "We'll create a vendor named Mindy and give her money and inventory.  Mindy really likes eyes and buttons. Her barter percentage is 75, so she will only buy items from Jane at 75 percent of their value."
    $ mindy_inv = Inventory("Mindy", 500, 75)
    $ mindy_inv.take(eye,4)
    $ mindy_inv.take(but,3)
    $ mindy_inv.take(coin,2)    
    
    # vendor screen parameters are left-side inventory, right-side inventory
    show screen inventory_screen(jane_inv, mindy_inv)
    
    "Now we'll give Jane some walking-around money."
    $ jane_inv.money = 500    
    
    "The inventory screen can take two inventory parameters and display the inventories side-by-side. You can click an item to buy/sell between the two.  Neither character can buy items if they don't have enough money.  Trade mode allows you to exchange items without money and bank mode allows withdrawing and depositing money."    
    
    $ chest = Inventory("Storage Chest")

    "Using trade and bank modes together, you can create a storage chest."
    show screen inventory_screen(jane_inv, chest, trade_mode=True, bank_mode=True)    
    
    "That's it! Exit to end the demo when you are finished."    
Attachments
inventory.rpy
version 1.5
(13.88 KiB) Downloaded 1615 times
Inventory-Demo-1.5.zip
demo files, add to new project
(170.61 KiB) Downloaded 1921 times
Last edited by saguaro on Sat May 21, 2016 10:22 pm, edited 10 times in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Infinite, Stackable Inventory and Vendor Screens

#2 Post by xela »

Excellent job! Some really good ideas for structure and display as well...
Like what we're doing? Support us at:
Image

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#3 Post by saguaro »

Thanks, I appreciate that!

Updated to add a basic crafting feature. I tried to keep the styling to a minimum, but there is a custom style at the end of inventory.rpy so I could tidy things up a little bit. The gist of the demo is now in the first post for those curious about how functions are set up.

Sidji972
Regular
Posts: 107
Joined: Tue Jan 21, 2014 8:50 pm
Location: Canada
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#4 Post by Sidji972 »

Awesome guy! I tested it and your craft system is really good!

PS : there is a small issue with scroll i think, impossible to sell craft item :)

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#5 Post by saguaro »

Thanks for testing this! I try to test my code but it's always better for a different person to test it out. I set the value for the doll to 100000 so the vendor must have that much money to purchase it. But if the scroll problem persists let me know the screen and circumstance and I'll fix it.

User avatar
Skaiya
Regular
Posts: 95
Joined: Tue Sep 25, 2012 5:42 pm
Projects: My Lovely Pastry, Crystal Diamonds Shatter, IWSTBMTB
Tumblr: skaiya-art
Deviantart: skaiyaart
Location: The Netherlands
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#6 Post by Skaiya »

Thank you, so so so so SO much.
This is exactly what I was been looking for since November last year ;-;
My dA
My Tumblr
Currently working on:
My Lovely Pastry, Crystal Diamonds Shatter, IWSTBMTB.

Eligar
Newbie
Posts: 14
Joined: Thu Apr 24, 2014 11:55 am
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#7 Post by Eligar »

This couldn't have been more perfect, thank you so much!

User avatar
Alitza
Newbie
Posts: 7
Joined: Fri Jun 07, 2013 7:32 am
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#8 Post by Alitza »

Is there an easy way add to this nice inventory chance wearing inventory items such as another inventory but with items without prices?

thanks in advance

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#9 Post by saguaro »

It sounds like you are asking for a 'trade' function that allows items to be swapped between inventory screens without money being exchanged.

That's a good idea for a feature, so I have updated the inventory.rpy script with a "trade" function, which you can now download from the first post. You can summon the trade screen by adding a third "True" argument:

Code: Select all

    show screen vendor(jane_inv, mindy_inv, True)
This will allow you to trade between inventories, so if you had a character's "closet" or "treasure chest" inventory, for example, they could take an leave items at will. In the example posted, mindy_inv would be the "closet" inventory.

I hope that helps.

User avatar
Alitza
Newbie
Posts: 7
Joined: Fri Jun 07, 2013 7:32 am
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#10 Post by Alitza »

that's what I wanted

Thanks

User avatar
Stapper
Regular
Posts: 96
Joined: Wed Feb 27, 2013 9:54 pm
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#11 Post by Stapper »

I've taken this awesome script as a base for my game and have to say that you've made it insanely user-friendly to use and modify. Thank you very much for sharing this!

I have however one thing that I cannot figure out. I would like to remove the ascending and descending options, because I won't have enough items to make them useful. However when I hide them and use the defaults, the sort by quantity and value are showing correctly with the highest amount first but the sort by name is inverted and starts with the yarn instead of the awesome sword.

How can I remedy this little problem?

User avatar
saguaro
Miko-Class Veteran
Posts: 560
Joined: Sun Feb 12, 2012 9:17 am
Completed: Locked-In, Sunrise, The Censor
Organization: Lucky Special Games
itch: saguarofoo
Location: USA
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#12 Post by saguaro »

Thank you, I'm glad you've found it useful!

Sorting ascending/descending is controlled by the Inventory instance variable x.sort_order, which has a default of false (descending).

Since you want names to be ascending and numbers to be descending, you'll want to update the sort_name function of the Inventory class (line 43 of inventory.rpy) so it always sorts as ascending. Simply remove the reverse parameter.

Code: Select all

        def sort_name(self):
            self.inv.sort(key=lambda i: i[0].name)
            renpy.restart_interaction()

User avatar
Stapper
Regular
Posts: 96
Joined: Wed Feb 27, 2013 9:54 pm
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#13 Post by Stapper »

Works like a charm! Thanks a lot :)

storymasterq
Newbie
Posts: 12
Joined: Mon May 19, 2014 10:45 pm
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#14 Post by storymasterq »

Hi! This is an amazing system. So good that I've slightly changed my game just so I can use it :D

A question, is there an easy way to make selling to a vendor gains only half of an item's declared value? A second question, is there a way to transfer only the money to a vendor (or, a chest, actually)?

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Infinite, Stackable Inventory, Crafting, and Vendor Scre

#15 Post by noeinan »

This looks really cool! Thanks for making it-- I am trying to make a game centering around smithing so it will be very useful to me. :) I noticed that if you hide the recipe screen there doesn't seem to be a way to show it again, but I think that should be an easy fix on my part. Thanks again!
Image

Image
Image

Post Reply

Who is online

Users browsing this forum: No registered users