Walkthrough on inventory system

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
User avatar
lk09jh87gf65
Newbie
Posts: 8
Joined: Wed Jan 08, 2014 5:04 pm
Contact:

Walkthrough on inventory system

#1 Post by lk09jh87gf65 »

Hello, I've seen the codes for inventories but they confuse me quite a bit. I need a simplified way of understanding how to make it work. My game doesn't require anything really complicated, mostly single-use, combine item, and illustrations for the items in an inventory menu. No lifebar or anything like that. It's just seems like really complicated code.

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: Walkthrough on inventory system

#2 Post by Showsni »

It's easy enough to make an inventory; just store the items in it as a list.

Code: Select all

$ inventory = []
You can add items to it

Code: Select all

 $ inventory.append("hat")
and remove items from it

Code: Select all

 $ inventory.remove("hat")
Of course, the player can't see any of that going on; so you need some kind of representation for them to see the items and interact with them. It's probably best to use screens to do this.

Let's set up a new screen that will show our inventory.

Code: Select all

    screen inv:
        frame:
            xalign 0.0
            yalign 0.0
            hbox:
                for i in inventory:
                    $ iname = i + "_%s.png"
                    imagebutton auto iname action Jump(i)
This screen puts a frame in the top left of the screen which will then show all out inventory items horizontally next to each other, assuming we've set up pictures for them (where if the inventory item is "hat" the pictures are called "hat_insensitive.png" "hat_idle.png" "hat_hover.png" etc, as here: http://www.renpy.org/doc/html/screens.html#imagebutton
Clicking on the picture jumps you to a label called hat, where you can do whatever it is you wanted to do with the hat. You show and hide the screen with show screen inv and hide screen inv.

This is only a very simplistic version, of course, but hopefully it gives you some idea of where to start. For a slightly more advanced version you can look up how to use Classes for the inventory and inventory items. This will allow things like

Code: Select all

 if "hat" in inventory:

User avatar
lk09jh87gf65
Newbie
Posts: 8
Joined: Wed Jan 08, 2014 5:04 pm
Contact:

Re: Walkthrough on inventory system

#3 Post by lk09jh87gf65 »

Showsni wrote:It's easy enough to make an inventory; just store the items in it as a list.

Code: Select all

$ inventory = []
You can add items to it

Code: Select all

 $ inventory.append("hat")
and remove items from it

Code: Select all

 $ inventory.remove("hat")
Of course, the player can't see any of that going on; so you need some kind of representation for them to see the items and interact with them. It's probably best to use screens to do this.

Let's set up a new screen that will show our inventory.

Code: Select all

    screen inv:
        frame:
            xalign 0.0
            yalign 0.0
            hbox:
                for i in inventory:
                    $ iname = i + "_%s.png"
                    imagebutton auto iname action Jump(i)
This screen puts a frame in the top left of the screen which will then show all out inventory items horizontally next to each other, assuming we've set up pictures for them (where if the inventory item is "hat" the pictures are called "hat_insensitive.png" "hat_idle.png" "hat_hover.png" etc, as here: http://www.renpy.org/doc/html/screens.html#imagebutton
Clicking on the picture jumps you to a label called hat, where you can do whatever it is you wanted to do with the hat. You show and hide the screen with show screen inv and hide screen inv.

This is only a very simplistic version, of course, but hopefully it gives you some idea of where to start. For a slightly more advanced version you can look up how to use Classes for the inventory and inventory items. This will allow things like

Code: Select all

 if "hat" in inventory:
I'm sorry, but where do I enter these? I'm sure the last one is in the screens.rpy but where in the script(or otherwise) do the other ones go in. Also, don't I need like frame and inventory pictures for the inventory screen? Sorry, I'm very new to this.

User avatar
Showsni
Miko-Class Veteran
Posts: 563
Joined: Tue Jul 24, 2007 12:58 pm
Contact:

Re: Walkthrough on inventory system

#4 Post by Showsni »

I'd define the screens at the top; you can do it in screens.rpy, but it's actually fine to do it in any other script file as well. Defining the inventory list needs to be done immediately after the start label, and adding and removing things from an inventory come wherever you want to do it in a game. And for this example you would beed pictures, yes. If you use textbuttons instead you don't need pictures.

Here, I'll throw together something as an example:
SimpleInventory-1.0-all.zip
(20.53 MiB) Downloaded 143 times

Post Reply

Who is online

Users browsing this forum: Bing [Bot]