What is the easiest way or the best way to make an inventory?

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
Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

What is the easiest way or the best way to make an inventory?

#1 Post by Zherot »

Like the tittle says I want to make an inventory, i kinda think of an array but i'm kinda lost on how to link it with a visual interface.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: What is the easiest way or the best way to make an inventory?

#2 Post by Milkymalk »

Maybe I should continue my cookbook tutorial and actually do the example how to make an actual screen for the inventory. This has been asked a few times now.

Super-simple inventory:
Make a list in python (using "$ inventory = []" or without "$" if inside a python block) and add strings with inventory.append("ball"). Access the inventory with inventory[placenumber] and delete items with inventory.pop[placenumber].

To display the contents, you make a for-loop inside a screen that iterates through your inventory:

Code: Select all

screen invscreen():
    vbox:
        for i in inventory:
            text i
An a little more complex system:
viewtopic.php?f=51&t=44730
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: What is the easiest way or the best way to make an inventory?

#3 Post by Zherot »

Milkymalk wrote: Thu May 24, 2018 7:21 pm Maybe I should continue my cookbook tutorial and actually do the example how to make an actual screen for the inventory. This has been asked a few times now.

Super-simple inventory:
Make a list in python (using "$ inventory = []" or without "$" if inside a python block) and add strings with inventory.append("ball"). Access the inventory with inventory[placenumber] and delete items with inventory.pop[placenumber].

To display the contents, you make a for-loop inside a screen that iterates through your inventory:

Code: Select all

screen invscreen():
    vbox:
        for i in inventory:
            text i
An a little more complex system:
viewtopic.php?f=51&t=44730
Yeah, you should, it is kinda complicated how to make visual interfaces with renpy, like there is really not much info about it, mostly about inventory interfaces, like i want to make my items to be displayed visually in the inventory and all that but i just don't really know how.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: What is the easiest way or the best way to make an inventory?

#4 Post by Milkymalk »

Visual display is a little more complicated, but not much. I can give you some pointers so you know where to look. You will understand Ren'Py much better after you have done this :)

It doesn't hurt to read the tutorial I linked. It explains how classes, methods and lists work.

If you want to make any kind of custom display, you need Screen Language: https://www.renpy.org/doc/html/screens.html This is in a way the "HTML" of Ren'Py (it mostly tells the game how a screen should look), but you can partially use Python inside it for dynamic screens.

Understand how for-loops work: https://wiki.python.org/moin/ForLoop They work just like that inside screens. My screen example just before is a for-loop that displays each element inside the list (with errors if it's not a string). "vbox" means "vertical box" and makes all elements appear in a column. Without it, each name would be written over the last one and it turns into a unreadable mess.

You can use "add" (displays images in screens) with dynamic image names:

Code: Select all

add "images/items/"+itemname+".png"
So you either want to use the name of the item itself as a filename, or store a filename that will be used for this with each item in a class.
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

Zherot
Regular
Posts: 91
Joined: Tue Aug 08, 2017 9:10 pm
Contact:

Re: What is the easiest way or the best way to make an inventory?

#5 Post by Zherot »

I did checked your tutorial, it is good, i do know though how classes,methods and lists work, i just have trouble on how to actually make that in a visual mannerso i can display it for the player.

User avatar
Milkymalk
Miko-Class Veteran
Posts: 753
Joined: Wed Nov 23, 2011 5:30 pm
Completed: Don't Look (AGS game)
Projects: KANPEKI! ★Perfect Play★
Organization: Crappy White Wings
Location: Germany
Contact:

Re: What is the easiest way or the best way to make an inventory?

#6 Post by Milkymalk »

Thanks :)

You will need screens for so many things that it's worth reading their docs thoroughly. But I can give you a barebones screen that you can modify to your needs:

Code: Select all

screen show_inventory():
    modal True # <- this screen prevents lower screens from taking input
    fixed:
        xpos 100 # <- adjust these to where you want your inventory
        ypos 100
        xsize 300
        ysize 500
            frame:
                padding 10 # <- how many pixels between borders and content?
                background "images/inventorybackground.png" # <- whatever you want as a background image
                for i, item in inventory.enumerate(): # <- creates tuples of (number, item)
                    add "images/items/"+item.filename+".png" xpos (i%5)*50 ypos (i//5)*80 # <- displays images every 50 pixels with 5 images per row
                    text item.name  xpos (i%5)*50 ypos (i//5)*75+5 # <- displays each item's name below
                imagebutton idle "images/close.png" action Return() # <- closes the "call"ed screen
                
# to display the screen:
label blah:
    call screen show_inventory()
This code expects inventory to contain elements of a class that has "name" and "filename" as fields:

Code: Select all

class Item(object):
    __init__(self, name, filename):
        self.name = name
        self.filename = filename
        
inventory = []
inventory.append(Item('a ball', 'yellow_ball')
Crappy White Wings (currently quite inactive)
Working on: KANPEKI!
(On Hold: New Eden, Imperial Sea, Pure Light)

berklamer
Newbie
Posts: 1
Joined: Tue May 07, 2019 2:03 am
Contact:

Re: What is the easiest way or the best way to make an inventory?

#7 Post by berklamer »

check this simple...Python Tutorial

Post Reply

Who is online

Users browsing this forum: Google [Bot]