Page 1 of 7

Inventory Screen

Posted: Tue Sep 03, 2013 6:17 pm
by leon
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).

Re: Inventory Screen

Posted: Fri Sep 06, 2013 3:29 am
by xela

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?

Re: Inventory Screen

Posted: Fri Sep 06, 2013 10:16 am
by RavenBelmont
You sir are a genius. Thank you for helping us so much! Please keep 'em coming! :)

Re: Inventory Screen

Posted: Fri Sep 06, 2013 11:34 am
by leon
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.

Re: Inventory Screen

Posted: Fri Sep 06, 2013 2:08 pm
by xela
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...

Re: Inventory Screen

Posted: Wed Oct 23, 2013 7:20 pm
by Mole-chan
Thank you SO MUCH, this is exactly what I needed. 8'D I could probably do my own but this looks so much nicer.

Re: Inventory Screen

Posted: Sun Dec 29, 2013 11:30 am
by i1abnrk
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.

Re: Inventory Screen

Posted: Mon Apr 28, 2014 2:16 pm
by TheOneAndOnly-K
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.

Re: Inventory Screen

Posted: Thu Jul 10, 2014 2:54 am
by JaxxyLupei
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.

Re: Inventory Screen

Posted: Sat Jul 12, 2014 12:31 pm
by leon
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.

Re: Inventory Screen

Posted: Sat Nov 15, 2014 11:14 pm
by Deilicate
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!

Re: Inventory Screen

Posted: Fri Jan 02, 2015 9:32 am
by leon
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]) 

Re: Inventory Screen

Posted: Fri Jan 02, 2015 5:40 pm
by momotaros
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:

Re: Inventory Screen

Posted: Sat Jan 03, 2015 1:52 pm
by leon
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)

Re: Inventory Screen

Posted: Sat Jan 03, 2015 7:14 pm
by momotaros
you are awesome! thank you very much :D