Inventory Screen

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
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Inventory Screen

#1 Post by leon » Tue Sep 03, 2013 6:17 pm

While there are a couple of inventory examples in the cookbook already, they don't address using pictures for inventory items. So here's one that does that, completed with hover effects, tooltips and selectable items. The inventory system here is using classes, so you might want to check Money and Inventory Systems for more detailed explanation on how these work.
fancy_inventory_screen.png
The code is taken from Time Labyrinth and simplified for this example. Feel free to use the code however you like (same goes for Time Labyrinth code).
Attachments
Inventory.zip
(1.1 MiB) Downloaded 3926 times
Last edited by leon on Sun Jul 26, 2015 3:53 pm, edited 1 time in total.

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

Re: Inventory Screen

#2 Post by xela » Fri Sep 06, 2013 3:29 am

Code: Select all

    import renpy.store as store
    import renpy.exports as renpy # we need this so Ren'Py properly handles rollback with classes
Why is this relevant outside of pure python module? All python from .rpy files is already being executed in store namespace or am I misunderstanding the issue?
Like what we're doing? Support us at:
Image

User avatar
RavenBelmont
Regular
Posts: 36
Joined: Tue Jul 16, 2013 10:47 am
Contact:

Re: Inventory Screen

#3 Post by RavenBelmont » Fri Sep 06, 2013 10:16 am

You sir are a genius. Thank you for helping us so much! Please keep 'em coming! :)

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Inventory Screen

#4 Post by leon » Fri Sep 06, 2013 11:34 am

xela wrote:

Code: Select all

    import renpy.store as store
    import renpy.exports as renpy # we need this so Ren'Py properly handles rollback with classes
Why is this relevant outside of pure python module? All python from .rpy files is already being executed in store namespace or am I misunderstanding the issue?
You are right. That most likely isn't needed. I was reading the documantation that's out of date.

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

Re: Inventory Screen

#5 Post by xela » Fri Sep 06, 2013 2:08 pm

leon wrote: You are right. That most likely isn't needed. I was reading the documantation that's out of date.
Well, doesn't hurt either :) I was afraid I messed something up in my head again...
Like what we're doing? Support us at:
Image

User avatar
Mole-chan
Veteran
Posts: 324
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
Contact:

Re: Inventory Screen

#6 Post by Mole-chan » Wed Oct 23, 2013 7:20 pm

Thank you SO MUCH, this is exactly what I needed. 8'D I could probably do my own but this looks so much nicer.

User avatar
i1abnrk
Regular
Posts: 38
Joined: Wed Nov 20, 2013 1:50 pm
Projects: Critter Corral, Haremu Kikkemu Mo
IRC Nick: i1abnrk
Deviantart: i1abnrk
Github: i1abnrk
Location: Wisconsin
Contact:

Re: Inventory Screen

#7 Post by i1abnrk » Sun Dec 29, 2013 11:30 am

This was an excellent study guide for learning how to code my own solution in renpy. You did a solid job of commenting the code and gave me a basis for early hack tests into writing my game. Kudos and thanks.

User avatar
TheOneAndOnly-K
Regular
Posts: 78
Joined: Mon Apr 07, 2014 10:33 am
Contact:

Re: Inventory Screen

#8 Post by TheOneAndOnly-K » Mon Apr 28, 2014 2:16 pm

I want something like this, but this is rather complex for my liking.

I don't want the wording, and the .zip files contents are confusing to find out what my game needs in particular, not what has already been decided.

What I'm aiming for is for it to act like a store/mall idea.

JaxxyLupei
Regular
Posts: 30
Joined: Tue Apr 08, 2014 10:34 pm
Projects: Dance With Me? (A date sim)
Location: NY
Contact:

Re: Inventory Screen

#9 Post by JaxxyLupei » Thu Jul 10, 2014 2:54 am

I almost ripped my hair out trying to incorporate this into my game.

First I just wanted to delete the mp and element bits. When I tried it out, ren'py had a hissy fit about indentation mismatches, so I tried that, still kept happening. Eventually I just copied and pasted the entire thing and see if I could edit things bit by bit, but even with the full code, untouched, ren'py is telling me that "inventory" is not defined.

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Inventory Screen

#10 Post by leon » Sat Jul 12, 2014 12:31 pm

You probably forgot to create the inventory object:

Code: Select all

inventory = Inventory()
You need to create an inventory object after "label start:" and before using it. Check out the code in script.rpy just after label start.

Removing properties (like element and mp) is pretty straightforward. Indentation mismatch means you are either missing a space or have too many. Check out the line that the error message reports.

Deilicate
Newbie
Posts: 1
Joined: Sat Nov 15, 2014 11:01 pm
Contact:

Re: Inventory Screen

#11 Post by Deilicate » Sat Nov 15, 2014 11:14 pm

Hey, im a little new to coding and ive been trying to use your code and I read the cookbook about the inventory system to figure it out myself but...

Is there any way i can put a variable on the items when you classify them? so when used it would trigger an event (just like if you add the attribute mp=40, it adds onto your mp.)

For instance:
a quest event where you need to retrieve a chocolate bar from a person. It goes into your inventory and If you try using it in the inventory you get a message saying " I can't use this here."
Then when you get to the person and use the chocolate bar you get the message "You gave the chocolate bar."

basically... is it possible to have an item trigger a certain event when used in the right time? then anywhere else in the game if its used its not thrown out/used/ basically inactive?

the things i tried:
Ive been trying to use if statements but im not sure if theres a 'global' if statement. Basically whenever it came across the if statement it would work but then once passed that statement further along in the story if i tried clicking the item again it would crash it.

please help!

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Inventory Screen

#12 Post by leon » Fri Jan 02, 2015 9:32 am

Sorry for the late reply. Change the item's use method like this:

Code: Select all

def use(self):
    if self.name=="Chocolate":
        renpy.call_in_new_context("chocolate_event")
And then add the event like this.

Code: Select all

label chocolate_event:
    if renpy.showing("sprite_name"):
        "You gave the chocolate bar."
        $inventory.drop(chocolate)
    else:
        "I can't use this here."
    return
I got this message in PM. I'll reply here in case anyone else may need this.
Roxie wrote:Hello Leon!

I wanted to thank you for sharing the inventory screen code in the Renpy Cookbook. It really helped me better understand classes as well. I was wondering if you could give me some programming advice to add inventory features. I was hoping to be able to combine items, break them apart to more items, and to use the item with a scene or person, aka make a VN style adventure game. For the Item class, I have element (items released) and recipe (items needed) to the items able to be taken apart or combined. How would I allow up to two items in the inventory to be selected at the same time? Would I need to make another class that can only hold two items and use methods to affect that class only? I guess that brings up the issue on how do I clear items from both lists after two items merge to one. Or is there a way to do it all under the Inventory class? Classes are a hit and miss for me as you can tell :(

Here's a visual image http://altabestudio.com/wp-content/gall ... entory.png
I wanted the selected items to appear in the bottom two boxes which would involve another screen is my guess.

Thanks for reading!
- Roxie
First change the Item class like this:

Code: Select all

    class Item(store.object):
        def __init__(self, name, player=None, hp=0, mp=0, element="", image="", cost=0, recipeItem=False):
            self.selected=False
            self.recipeItem=recipeItem
            #...
        def use(self): #here we define what should happen when we use the item
            if self.recipeItem:
                self.selected=not self.selected
            #...

When defining the items, set the parameter recipeItem to True like this:

Code: Select all

banana = Item("Banana", mp=20, image="gui/inv_banana.png", recipeItem=True)

Code: Select all

Also change the imagebutton for the item in inventory_screen like this:
            if item.recipeItem:
                imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), SetVariable("item", item), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
            else:
                imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), Show("inventory_button"), SetVariable("item", item), Hide("inventory_screen"), item_use] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
And make all items selectable:

Code: Select all

            if item.selected:
                add "gui/selected.png" xpos x ypos y anchor(.5,.5)

Add a button that will handle combining the items to inventory_screen:

Code: Select all

textbutton "Combine" action [Function(combine_items), Hide("inventory_screen"), Show("inventory_button")] align (.80,.80)

Add the function to combine the items somewhere:

Code: Select all

init python:    
    def combine_items():
        for item in inventory.items:
            for recipe in recipes:
                recipe_tmp = recipe
                if item in recipe and item.selected:
                    recipe_tmp.remove(item)
                    if len(recipe_tmp)==1:
                        for tmp_item in recipe:
                            pass
                            inventory.drop(item)
                        inventory.add(recipe[0])
        return
Finally add some recipes somewhere after the item definitions:

Code: Select all

        recipes = [] 
        recipes.append([meat_with_condiment, stewed_meat, yellow_petals]) 
        recipes.append([chocolate_gun, chocolate, gun]) 

User avatar
momotaros
Newbie
Posts: 3
Joined: Fri Jan 02, 2015 5:26 pm
Contact:

Re: Inventory Screen

#13 Post by momotaros » Fri Jan 02, 2015 5:40 pm

wow, this is fantastic! thank you very much for sharing those codes!

i got a question, im want make a game like phoenix wrigth games, and i need know one thing:

in those games, when you click one item, shows you another image with a long description
for example:

you click " match box", then shows another image of the item and a longer description about that item.

¿how can i do that in this code?
and
¿how can i make a special option, like .. if X item is in the inventory: X option will be showed?

thank you very much, i hope dont be a bother to you with thes questions.
i hope you a happry 2015 :wink:

User avatar
leon
Miko-Class Veteran
Posts: 554
Joined: Sun Oct 09, 2011 11:15 pm
Completed: Visual Novel Tycoon, Night at the Hospital, Time Labyrinth, The Buried Moon, Left of Center, Super Otome Quest
Projects: Lemon Project, Porcelain Heart, Dream's Dénouement
Organization: Team ANARKY
Contact:

Re: Inventory Screen

#14 Post by leon » Sat Jan 03, 2015 1:52 pm

Create a screen like this:

Code: Select all

screen inventory_zoom(item):
    if item.name=="Chocolate":
        add "gui/inv_chocolate.png"
    #...
    textbutton "Back" action Return
And change the imagebutton in inventory_screen to call it:

Code: Select all

imagebutton idle pic hover pic xpos x ypos y action [Hide("gui_tooltip"), SetVariable("item", item), Hide("inventory_screen"), Show("inventory_button"), Show("inventory_zoom", item=item)] hovered [ Play ("sound", "sfx/click.wav"), Show("gui_tooltip", my_picture=my_tooltip, my_tt_ypos=693) ] unhovered [Hide("gui_tooltip")] at inv_eff
momotaros wrote:¿how can i make a special option, like .. if X item is in the inventory: X option will be showed?

Code: Select all

    menu:
        "Do nothing.":
            "..."
        "Give chocolate." if chocolate in inventory.items:
            "You gave the chocolate away. :("
            $inventory.drop(chocolate)

User avatar
momotaros
Newbie
Posts: 3
Joined: Fri Jan 02, 2015 5:26 pm
Contact:

Re: Inventory Screen

#15 Post by momotaros » Sat Jan 03, 2015 7:14 pm

you are awesome! thank you very much :D

Post Reply

Who is online

Users browsing this forum: No registered users