Help Making Inventory in Screen Language [SOLVED]

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
Abeiramar
Regular
Posts: 198
Joined: Wed Jul 28, 2010 10:37 am
Location: Portugal
Contact:

Help Making Inventory in Screen Language [SOLVED]

#1 Post by Abeiramar »

All right,
I have been spending lots of time (*cough* days*cough* ) trying to figure out how to code an inventory with images instead of text and I came up with this:

(By the way, the text is long but the question is very simple)

Code: Select all

screen picture:

    #the background with a pretty detailed frame
    add "black_background.jpg"
    add "frame.png"

    #a sexy grid
    grid 4 2:
        
        spacing 28
        xpos 330
        ypos  113

        #The items, those are images instead of text!!                 
        imagebutton idle"001.png" hover "002.png" hovered Show("description") unhovered Hide("description") action Jump("itemmenu")                    
        imagebutton idle"003.png" hover "003.png" hovered Show("description") unhovered Hide("description") action Jump("itemmenu")
        
        imagebutton idle"007.png" hover "007.png" hovered Show("description") unhovered Hide("description") action Show("itemmenu")
        imagebutton idle"008.png" hover "008.png" hovered Show("description") unhovered Hide("description") action Show("itemmenu")
        
        imagebutton idle"009.png" hover "009.png" hovered Show("description") unhovered Hide("description") action Show("itemmenu")
        imagebutton idle"001.png" hover "002.png" hovered Show("description") unhovered Hide("description") action Show("itemmenu")
        
        imagebutton idle"012.png" hover "012.png" hovered Show("description") unhovered Hide("description") action Show("itemmenu")
        imagebutton idle"011.png" hover "011.png" hovered Show("description") unhovered Hide("description") action Show("itemmenu")
As you see, this is not the inventory system per se but a representation of what I want the items to behave like:
>I want the inventory to have 8 items max
>When hover the imagebutton/item shows screen description
>When clicked they show a small screen menu with options like "Use", "Send them back to storage/Delete", "Return"(hide itemmenu).

Ok, so I have about 30 different obtainable items with different descriptions and uses... :lol:

I calculated two different solutions:
:arrow: Use lots of IF statements:
Something like:

Code: Select all

        if item 001: 
            imagebutton idle"001.png" hover "002.png" hovered Show("description") unhovered Hide("description") action Jump("start")
Except that if I don't have the item Ren'py goes "HURR DURR GRID ISN'T FULL" . Should I use "else"? Should I use vbox instead?
This might be the messiest and the longest path though...

:arrow: Use lists
This is the best path I found and the one I'm working on.
However... Apparently I can't python at all. I need help...

Let's start,
I create a list:

Code: Select all

label start:
    $ item= [] #The list is empty
Hmn... I guess I need to use classes since the items are not just text.
Then before the label start I put this code I snatched from here:

Code: Select all

init python:
    class Item:
        def __init__(self, name, cost, description, displayable):
            self.name = name 
            self.cost = cost
            self.description = description 
            self.displayable = displayable #image.png goes there, I hope this is possible

    class Inventory: #I guess this is the money...
        def __init__(self, money=10):
            self.money = money
            self.items = []

        def buy(self, item):
            if self.money >= item.cost:
                self.money -= item.cost
                self.items.append(item)
                return True
            else:
                return False

        def earn(self, amount):
            self.money += amount

        def has_item(self, item):
                if item in self.items:
                    return True
                else:
                    return False 
Then after the label start:

Code: Select all

label start:
    $ item= []
    
    python:
        inventory = Inventory() #HUN? What does this mean?

        spaghetti = Item("Spaghetti", 3, "Spaghetti: This pasta is good for cooking", "00a.png")
        olives = Item("Olives", 4, "Olives: They taste good. ", "00b.png")
        chocolate = Item("Chocolate", 11, "Chocolate: A sweet milk chocolate tablet", "00c.png")
        cabbage = ("Cabbage", 27, "Cabbage: Cabbages are healthy!", "00d.png")

#those are examples

Ok, ok, whatever, the biggest question is HOW DO I DISPLAY THIS ON A SCREEN?

I have to use "for" but how :| ? I think the code I want is similar to the one used to display the save/load screens:

Code: Select all

            # Display ten file slots, numbered 1 - 10.
            for i in range(1, columns * rows + 1):

                # Each file slot is a button.
                button:
                    action FileAction(i)
                    xfill True

                    has hbox

                    # Add the screenshot.
                    add FileScreenshot(i)
                    
                    # Format the description, and add it as text.
                    $ description = "% 2s. %s\n%s" % (
                        FileSlotName(i, columns * rows),
                        FileTime(i, empty=_("Empty Slot.")),
                        FileSaveName(i))

                    text description

                    key "save_delete" action FileDelete(i)
...What does (i) mean anyway?

Thanks for reading. Any help appreciated :) !
Last edited by Abeiramar on Thu Dec 01, 2011 8:01 am, edited 1 time in total.

KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: Help Making Inventory in Screen Language

#2 Post by KimiYoriBaka »

solution to attempt with lots of if statements: make an empty slot image use it to fill in the empty spaces or use the null statement if you don't want empty slots displayed. use whatever variable you used to limit the number of items to also check how many slots should be empty.

you can also just use two hbox's.

for the other method, I suggest something like this:

Code: Select all

#using the format [idle image, hover image, description] for the items

screen description:   #<--because you'll need this if you don't want to use tooltips
    default item = 0
    vbox:
        text inventory[item][2]
        #insert positional stuff here

screen picture:

    #the background with a pretty detailed frame
    add "black_background.jpg"
    add "frame.png"

    on "hide" action Hide("description")

    #a sexy grid
    grid 4 2:
       
        spacing 28
        xpos 330
        ypos  113

        for i in range(len(inventory)):
            imagebutton: 
               idle inventory[i][0] 
               hover inventory[i][1] 
               hovered Show("description", item = i) 
               unhovered Hide("description") action Jump("itemmenu")

        for i in range(8 - len(inventory)):
            null

label exampleItem:
    #how to check the number of items and how to add an item
    if len(inventory) < 8:
        $ inventory.append("001.png", "002.png", "Insert description here")

Abeiramar
Regular
Posts: 198
Joined: Wed Jul 28, 2010 10:37 am
Location: Portugal
Contact:

Re: Help Making Inventory in Screen Language

#3 Post by Abeiramar »

Whoa, thank you for the code :D :D !! You're truly helpful!

However I receive this error:
File "game/script.rpy", line 113, in python
for i in range(len(inventory)):
AttributeError: Inventory instance has no attribute '__len__'
What did I do wrong :o ?

Abeiramar
Regular
Posts: 198
Joined: Wed Jul 28, 2010 10:37 am
Location: Portugal
Contact:

Re: Help Making Inventory in Screen Language

#4 Post by Abeiramar »

Forget about it, I think it's working now XD !
I will send a more elaborate message tomorrow, I'm very sleepy now

For now, THANK YOU VERY MUCH KimiYoriBaka :D !!

Edit:
After playing around with your code, I discovered that the inventory.append() is missing a pair of brackets, so it should be:

Code: Select all

label exampleItem:
    #how to check the number of items and how to add an item
    if len(inventory) < 8:
        $ inventory.append(["001.png", "002.png", "Insert description here"])
And it's show ("itemmenu") instead of jump ("itemmenu") but that's my mistake XD

Now it works like a charm. Thank you for spoon feeding me with this code bro, it will definitely be useful for generations to come.
*brofists* I will credit you 8) .

wiimote
Regular
Posts: 38
Joined: Fri Dec 09, 2011 12:13 pm

Re: Help Making Inventory in Screen Language

#5 Post by wiimote »

I've been trying to build an inventory system based on what I found in this thread.
(I used the exact same code, only changing the image paths.)
Now I'm getting the same error as Abeiramar:
AttributeError: Inventory instance has no attribute '__len__'
So how DO you fix this?

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Help Making Inventory in Screen Language [SOLVED]

#6 Post by PyTom »

Looking at this - but not very hard - it seems like inventory is being used both as a list and an instance of Inventory. You probably want to rename one of those uses.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

wiimote
Regular
Posts: 38
Joined: Fri Dec 09, 2011 12:13 pm

Re: Help Making Inventory in Screen Language

#7 Post by wiimote »

Sorry to bump this thread again but the code posted in here does not work. I've tried everything.
How am I going to build my inventory now? I'm not experienced enough to code one of my own from scratch.
Is there another way? Perhaps a cookbook that I missed? Or maybe somebody else is willing to have another look at Abeiramar's code (this time a little harder) and correct it if necessary?

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

Re: Help Making Inventory in Screen Language [SOLVED]

#8 Post by Showsni »

Which code are you using? Just copy the code exactly as in KimiYoriBaka's post, and not any other code.
And have inventory as a list, with the items in the format [idle image, hover image, description] within it.
(Something like
$ inventory = [["cat1.jpg", "cat2.jpg", "A cat."], ["dog1.png", "dog2.png", "A dog."]]
for instance.)
Use show screen picture to show the inventory (and hide screen picture to hide it).

You'll need a label for itemmenu in the current code to decide what to do once you've clicked on an item.

wiimote
Regular
Posts: 38
Joined: Fri Dec 09, 2011 12:13 pm

Re: Help Making Inventory in Screen Language [SOLVED]

#9 Post by wiimote »

Yeah, well... I've been experimenting all day and I've solved many of my inventory problems by now.
I stumbled upon this thread http://lemmasoft.renai.us/forums/viewto ... =8&t=15475 and used that code instead.
For some reason that code, albeit looking kinda similar at first glance, was way easier for me to wrap my head around - plus it actually works.

The only real problem I'm having right now is "dynamic image paths" or whatever you want to call it.
I'm using imagebuttons instead of textbuttons for my items since it is going to be a monkey-island-style inventory.
However every item has, of course, it's own image. So you have to somehow mention what item it is you're
adding at the "for-i"-passage or else they'll all look the same.
I've tried implementing a variable to define the image. But of course this isn't working.

Code: Select all

$ itemname = "apple"
screen inventory:
    bla bla bla
        for i in items:
            imagebutton idle "images/items/[itemname].png" and so on...          # trying to get apple.png

Is there a way to do this?

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

Re: Help Making Inventory in Screen Language [SOLVED]

#10 Post by Showsni »

Ah, sure. The way I do it is to have various variables for each item, so that you end up with something like for apple in your inventory list:

$ items = [apple, orange]

$ apple.name = "Apple"
$ apple.picture = "apple.png"

$ orange.name = "Orange"
$ orange.picture = "orange.png"

then in the code use i.picture to refer to the picture name rather than just i.
for i in items:
imagebutton idle i.picture and so on
text i.name

or something similar to that, anyway. If you go down that path, you can make defining all the bits easier by setting up a class, which can also include other information, such as the description of the item, how many of them you have, and so on.

You'd put this in a python init block:

Code: Select all

class thing:
    def __init__(self, name, picture, description):
        self.name = name
        self.picture = picture
        if description == "":
            self.description = "A random thing."
        else:
            self.description = description
and then define an item like so after the start label:

$ apple = thing("Apple", "apple.png", "A delicious red apple.")

Then you can use i.name i.picture and i.description exactly as before. (You'd still need to add apple to the item list when it was in your inventory too).

wiimote
Regular
Posts: 38
Joined: Fri Dec 09, 2011 12:13 pm

Re: Help Making Inventory in Screen Language [SOLVED]

#11 Post by wiimote »

Thank you. Works like a charm. I'm beginning to like these "class"-thingies.
No more (serious) inventory-related problems for the moment.
I'm gonna let this old thread rest in peace now.

Post Reply

Who is online

Users browsing this forum: No registered users