Explain to me like I'm 5: 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.
Message
Author
JaxxyLupei
Regular
Posts: 30
Joined: Tue Apr 08, 2014 10:34 pm
Projects: Dance With Me? (A date sim)
Location: NY

Explain to me like I'm 5: Inventory System

#1 Post by JaxxyLupei »

So for the game my friend and I are working on, we've pretty much got everything covered...except one thing: a shop and inventory system.

I've tried the method from the wiki, didn't get really far because I got confused on what I was supposed to do. Then I tried this one: http://lemmasoft.renai.us/forums/viewto ... 51&t=23071 and I ended up getting countless errors when I tried utilizing it.

The problem is I get lost after a certain point of the coding and how exactly I'm suppose to integrate it into the script.

I would like an inventory/shop code, but I also need it to be broken down to me to explain what exactly it is I have to do, hence: explain to me like I'm 5. Please and thanks.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#2 Post by trooper6 »

I am in the middle of class prep right now so I can't really answer this very complicated question, but I also want to procrastinate a bit. So I'll do a few bits at a time and maybe other people will step up and add other bits.

I think you need to understand:
1) Data Structures and doing things with them (Minimum)
2) Classes & Functions
3) Ways to create an interface between the user and the data

In this post I'll talk about Data Structures. Maybe others will take up the other parts while I'm doing class prep.
Here are two web pages talking about these structures in Python:
http://www.sthurlow.com/python/lesson06/
http://stackoverflow.com/questions/3489 ... ist-or-set

Doing things with Data Structures.
At its most basic, an inventory is just a data structure. A data structure is a collection of information. All of this is in Python so remember you must put it in a Python block, or start the line with a $.

There are 4(?) data structures to ponder.
Tuple, Dictionary, List, Set.
Tuple is a list of items that you define in your program and that will never change. Like a list of months:

Code: Select all

default months = ('January','February','March','April','May','June','July','August','September','October','November',' December')
A Dictionary is list of paired pieces of info: key and value. This list can be changed. Classic would be a telephone number list where the key is the person's name and the value is the phone number.

Code: Select all

 default phonebook = {Andrew Parson':8806336, 'Emily Everett':6784346, 'Peter Power':7658344, 'Lewis Lame':1122345}
Lists and Sets are similar but Lists can have duplicates and are in order, Sets have no duplicates and are in so particular order.
A List, which has duplicates:

Code: Select all

 default dollars_spent = [10, 12, 12, 15, 11, 10]
A Set, has no duplicates:

Code: Select all

 default friends = {'Tom', 'Jane', 'Brian', 'Jessica', 'Aiden'}
I think your inventory is best represented by a set.
So we make a set which we will call backpack:

Code: Select all

default backpack = set()
Then we add some inventory to the backpack:

Code: Select all

$backpack.add("Rope")
$backpack.add("Sword")
$packpack.add("Health Potion")
If you want to remove something from the backpack:

Code: Select all

$backpack.remove("Health Potion")
So I'll make a little program using menus to show how you add to your inventory and show you what is in it.

Code: Select all

screen inventory_screen():
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
        for item in backpack:
            text ("[item]")

default backpack = set()            
# The game starts here.
label start:
    show screen inventory_screen()

    "You are walking through the forest when you see a sword!"
    menu:
        "Do you take the sword?"
        "Yes":
            $backpack.add("Sword")
        "No":
            pass
    
    "You walk further into the forest, when you see a strange potion!"
    
    menu:
        "Do you take the potion?"
        "Yes":
            $backpack.add("Potion")
        "No":
            pass    
            
    "You walk further into the forest, when you see a rope!"
    
    menu:
        "Do you take the rope?"
        "Yes":
            $backpack.add("Rope")
        "No":
            pass    
        
    "You walk further into the forest and step on a poisoned spike! You you are dying!"
    menu:
        "What do you do?!"
        "Drink the strange potion!" if "Potion" in backpack:
            "You drink the potion and are healed! It was a health potion!"
            $backpack.remove("Potion")
        "Refuse to drink the strange potion!" if "Potion" in backpack:
            "You don't trust the potion, so you don't drink it...and die!"
        "There is nothing you can do!" if "Potion" not in backpack:
            "And so you die."
            
    "You adventure is over"
                                                                   
    return
If you wanted to do this with a list rather than a set you would use append and remove, so:

Code: Select all

screen inventory_screen():
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
        for item in backpack:
            text ("[item]")

default backpack = []           
# The game starts here.
label start:
    show screen inventory_screen()

    "You are walking through the forest when you see a sword!"
    menu:
        "Do you take the sword?"
        "Yes":
            $backpack.append("Sword")
        "No":
            pass
    
    "You walk further into the forest, when you see a strange potion!"
    
    menu:
        "Do you take the potion?"
        "Yes":
            $backpack.append("Potion")
        "No":
            pass    
            
    "You walk further into the forest, when you see a rope!"
    
    menu:
        "Do you take the rope?"
        "Yes":
            $backpack.append("Rope")
        "No":
            pass    
        
    "You walk further into the forest and step on a poisoned spike! You you are dying!"
    menu:
        "What do you do?!"
        "Drink the strange potion!" if "Potion" in backpack:
            "You drink the potion and are healed! It was a health potion!"
            $backpack.remove("Potion")
        "Refuse to drink the strange potion!" if "Potion" in backpack:
            "You don't trust the potion, so you don't drink it...and die!"
        "There is nothing you can do!" if "Potion" not in backpack:
            "And so you die."
            
    "You adventure is over"
                                                                   
    return
That is how you very basically use a list or a set, to add to it or remove from it, to check to see if things are in it.

A Shop can similarly be made very simply with menus and a money variable.
So you declare default money = 50
During the course of the adventure, you find 25 more gold pieces:
$money += 25

And you can make visit a shop keeper who will sell you things if you have enough money:

Code: Select all

screen inventory_screen():
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
        text "Gold Pieces: [money]"
        for item in backpack:
            text ("[item]")

default backpack = set()
default money = 50
            
# The game starts here.
label start:    
    show screen inventory_screen()

    "You are walking through the forest when you see a sword!"
    menu:
        "Do you take the sword?"
        "Yes":
            $backpack.add("Sword")
        "No":
            pass
    
    "You walk further into the forest, when you see a strange potion!"
    
    menu:
        "Do you take the potion?"
        "Yes":
            $backpack.add("Potion")
        "No":
            pass    
            
    "You walk further into the forest, when you see a rope!"
    
    menu:
        "Do you take the rope?"
        "Yes":
            $backpack.add("Rope")
        "No":
            pass    
    
    $money +=25
    "You walk further into the forest, when you find 25 gold pieces!"
    
    "You walk further into the forest and step on a poisoned spike! You you are dying!"
    menu:
        "What do you do?!"
        "Drink the strange potion!" if "Potion" in backpack:
            "You drink the potion and are healed! It was a health potion!"
            $backpack.remove("Potion")
            jump store
        "Refuse to drink the strange potion!" if "Potion" in backpack:
            "You don't trust the potion, so you don't drink it...and die!"
            jump end
        "There is nothing you can do!" if "Potion" not in backpack:
            "And so you die."
            jump end
            
label store:
    "You enter the store, the old man offers to sell you one item!"
    
    menu:
        "What do you buy?"
        "A rose for 10 gold pieces" if money >= 10:
            $backpack.add("Rose")
            $money -= 10
        "A dagger for 25 gold pieces" if money >= 25:
            $backpack.add("Dagger")
            $money -= 25
        "A horse for 500 gold pieces" if money >= 100:
            $backpack.add("Horse")
            $money -=500
        "I won't buy anything":
            pass
            
     
label end:
    "You adventure is over"
                                                                   
    return
That is the very most basic using only basic data structures.
People often spice things up by using classes (which gather data together) and functions (which automates commands).
However, I can't make the classes/function example until after I do my class prep for tomorrow's class! Perhaps someone else will take what I started and make an example while I'm working!
Last edited by trooper6 on Fri Jun 30, 2017 4:27 pm, edited 1 time in total.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#3 Post by trooper6 »

It is 2 am and I have to go to bed, but I'll start a little bit with classes and functions.

So a class can be a collection of data and sometimes functions. In object oriented programming it is good to think about these are something a bit larger in scope that individual instances are examples of.

So it would make sense to have a class called Item. What properties would our item have? I could see, Name, Cost, maybe weight if you wanted to deal with encumbrance. How and how many of those items you have. Now our items are going to also be things like weapons and healing potions. So maybe we could also have a damage variable, which for weapons tells how much damage it does, for potions how much damage it heals, and for other items it would just be 0.

This would look like this:

Code: Select all

init -1 python:
    class Item(object):
        def __init__(self, name, cost, wt, damage, amount=0, **kwargs):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damage = damage
            self.amount = amount
You would integrate this item into our previous code like so:

Code: Select all

screen inventory_screen():
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
        text "Gold Pieces: [money]"
        for item in backpack:
            text ("[item.name]")
    
init -1 python:
    class Item(object):
        def __init__(self, name, cost, wt, damage, amount=0, **kwargs):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damage = damage
            self.amount = amount
     
#Variable declarations
default backpack = set()
default money = 50
default sword = Item("Sword", 150, 5, 10)
default dagger = Item("Dagger", 50, 2, 5)
default potion = Item("Mysterious Potion", 50, 2, 10)
default rope = Item("Rope", 20, 10, 0)
default rose = Item("Rose", 10, 0, 0)
default horse = Item("Horse", 500, 0, 0)
       
# The game starts here.
label start:    
    show screen inventory_screen()

    "You are walking through the forest when you see a sword!"
    menu:
        "Do you take the sword?"
        "Yes":
            $backpack.add(sword)
        "No":
            pass
    
    "You walk further into the forest, when you see a strange potion!"
    
    menu:
        "Do you take the potion?"
        "Yes":
            $backpack.add(potion)
        "No":
            pass  
            
    "You walk further into the forest, when you see a rope!"
    
    menu:
        "Do you take the rope?"
        "Yes":
            $backpack.add(rope)
        "No":
            pass    
    
    $money +=25
    "You walk further into the forest, when you find 25 gold pieces!"
    
    "You walk further into the forest and step on a poisoned spike! You you are dying!"
    menu:
        "What do you do?!"
        "Drink the strange potion!" if potion in backpack:
            "You drink the potion and are healed! It was a health potion!"
            $backpack.remove(potion)
            jump store
        "Refuse to drink the strange potion!" if potion in backpack:
            "You don't trust the potion, so you don't drink it...and die!"
            jump end
        "There is nothing you can do!" if potion not in backpack:
            "And so you die."
            jump end
            
label store:
    "You enter the store, the old man offers to sell you one item!"
    
    menu:
        "What do you buy?"
        "A rose for [rose.cost] gold pieces" if money >= rose.cost:
            $backpack.add(rose)
            $money -= rose.cost
        "A dagger for [dagger.cost] gold pieces" if money >= dagger.cost:
            $backpack.add(dagger)
            $money -= dagger.cost
        "A horse for [horse.cost] gold pieces" if money >= horse.cost:
            $backpack.add(horse)
            $money -= horse.cost
        "I won't buy anything":
            pass
            
label end:
    "You adventure is over"
                                                                   
    return
Last edited by trooper6 on Fri Jun 30, 2017 4:28 pm, edited 2 times in total.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#4 Post by trooper6 »

All of that is fine...but we can get a bit more complicated right? Why don't we create a backpack class and we can give it functions!
So what variables should our backpack class have? I think it should have a Max Weight and Total Weight variable. It should also have a set to add to.
And as for functions...well, I think it should have an add_item function that...does what?
1) Check to see if there is room in the backpack. If there isn't, it should return an error.
2) If there is room in the backpack, it should add the item and increase the amount of the item, and reduce the money we have.
3) There should also be a function to remove an item, the increases the space left and the amount of items.

But now I just realized...that if we find the item, it shouldn't reduce our amount of gold...so I want to at a variable to our add_Item function called found that defaults to False. But we can pass True...and if we do...it won't reduce the gold we have.

So what might this class look like?

Code: Select all

    class Backpack(object):
        def __init__(self, maxwt, gold, **kwargs):
            self.maxwt = maxwt
            self.space_left = maxwt
            self.mySet = set()
            self.gold = gold
            
        def add_Item(self, item, found=False):
            if item.wt <= self.space_left and (item.cost <= self.gold or found):
                self.mySet.add(item)
                self.space_left -= item.wt
                item.amount += 1
                if not found:
                    self.gold -= item.cost
                return "{0} added to inventory.".format(item.name)
            else:
                if item.wt > self.space_left:
                    return "There is not enough space in your inventory!"
                elif item.cost > self.gold:
                    return "You don't have enough gold!"
                else:
                    return "There is some problem here."
Also note I want to return some text when we add or remove letting me know if I did it or not!

So with out Backpack Class done, here is the code again, integrating that:

Code: Select all

screen inventory_screen():
    vbox:
        align (0.9, 0.0)
        text "Inventory:"
        text "_____"
        text "Gold Pieces: [myBackpack.gold]"
        for item in myBackpack.mySet:
            text ("[item.name], [item.amount]")
#            text ("[item]")
        text "_____"
        text "Space Left: [myBackpack.space_left]"
    
init -1 python:
    class Item(object):
        def __init__(self, name, cost, wt, damage, amount=0, **kwargs):
            self.name = name
            self.cost = cost
            self.wt = wt
            self.damage = damage
            self.amount = amount
            
    class Backpack(object):
        def __init__(self, maxwt, gold, **kwargs):
            self.maxwt = maxwt
            self.space_left = maxwt
            self.mySet = set()
            self.gold = gold
            
        def add_Item(self, item, found=False):
            if item.wt <= self.space_left and (item.cost <= self.gold or found):
                self.mySet.add(item)
                self.space_left -= item.wt
                item.amount += 1
                if not found:
                    self.gold -= item.cost
                return "{0} added to inventory.".format(item.name)
            else:
                if item.wt > self.space_left:
                    return "There is not enough space in your inventory!"
                elif item.cost > self.gold:
                    return "You don't have enough gold!"
                else:
                    return "There is some problem here."
        
        def remove_Item(self, item):
            if item in self.mySet:
                item.amount -= 1
                self.space_left += item.wt
                if item.amount == 0:
                    self.mySet.remove(item)
                return "{0} removed.".format(item.name)
            else:
                return "{0} not in inventory!".format(item.name)
      
#Variable declarations
default backpack = set()
default myBackpack = Backpack(50, 50)
default money = 50
default sword = Item("Sword", 150, 5, 10)
default dagger = Item("Dagger", 50, 2, 5)
default potion = Item("Mysterious Potion", 50, 2, 10)
default rope = Item("Rope", 20, 10, 0)
default rose = Item("Rose", 10, 0, 0)
default horse = Item("Horse", 500, 0, 0)
      
# The game starts here.
label start:    
    show screen inventory_screen()

    "You are walking through the forest when you see a sword!"
    menu:
        "Do you take the sword?"
        "Yes":
            $result = myBackpack.add_Item(sword, True)
            "[result]"
        "No":
            pass
    
    "You walk further into the forest, when you see a strange potion!"
    
    menu:
        "Do you take the potion?"
        "Yes":
            $result = myBackpack.add_Item(potion, True)
            "[result]"
        "No":
            pass  
            
    "You walk further into the forest, when you see a rope!"
    
    menu:
        "Do you take the rope?"
        "Yes":
            $result = myBackpack.add_Item(rope, True)
            "[result]"
        "No":
            pass    
    
    $myBackpack.gold +=25
    "You walk further into the forest, when you find 25 gold pieces!"
    
    "You walk further into the forest and step on a poisoned spike! You you are dying!"
    menu:
        "What do you do?!"
        "Drink the strange potion!" if potion in myBackpack.mySet:
            "You drink the potion and are healed! It was a health potion!"
            $result = myBackpack.remove_Item(potion)
            "[result]"
            jump store
        "Refuse to drink the strange potion!" if potion in myBackpack.mySet:
            "You don't trust the potion, so you don't drink it...and die!"
            jump end
        "There is nothing you can do!" if potion not in myBackpack.mySet:
            "And so you die."
            jump end
            
label store:
    "You enter the store, the old man offers to sell you one item!"
    
    menu:
        "What do you buy?"
        "A rose for [rose.cost] gold pieces":
            $result = myBackpack.add_Item(rose)
            "[result]"
        "A dagger for [dagger.cost] gold pieces":
            $result = myBackpack.add_Item(dagger)
            "[result]"
        "A rope for [rope.cost] gold pieces":
            $result = myBackpack.add_Item(rope)
            "[result]"    
        "A horse for [horse.cost] gold pieces":
            $result = myBackpack.add_Item(horse)
            "[result]"
        "I won't buy anything":
            pass
            
     
label end:
    "You adventure is over"
                                                                   
    return
So that is some very simple, bare bones inventory stuff. I'm not the person to go into images...because I haven't experimented with that sort of stuff yet. But hopefully this gives you an understanding of the basics and you can work and experiment on your own from there. Let me know if there is anything that is unclear.

Generally speaking, you have to have an idea of what you want your inventory system to include. Do you care about encumbrance? Do you care about amounts? Do you care about gold? Are your items complex? Can people find things or only buy them? Can the player sell stuff or only buy them? How often do you plan on allowing the player to go to the store? You could make your inventory system different based on how you imagine your game playing.

For example, in Elder Scrolls, you can steal items! But if you do, you can't sell those items in regular stores, only in sketchy stores that deal in stolen goods. How is that coded? I imagine that each item would have a stolen variable. If you steal the item, that variable is True, otherwise, it is False. I imagine that in Elder Scrolls there is a store class. And one of the variables of the Store class would be a do you buy stolen items variable. Then in the buying/selling screen they compare the two variables.

Maybe you care about tracking if your items are Holy or Damned.
Maybe you care about tracking how much weight you as a player can carry based on your strength.
Maybe you care about tracking where on your body each item is.
Maybe you want to track how long food is good for before it goes bad.

You can do all of that easily. You just have to imagine how you want it to work.
Last edited by trooper6 on Fri Jun 30, 2017 4:29 pm, edited 4 times in total.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Samu-kun
King of Moé
Posts: 2262
Joined: Mon Sep 03, 2007 3:49 pm
Organization: Love in Space Inc
Location: United States
Contact:

Re: Explain to me like I'm 5: Inventory System

#5 Post by Samu-kun »

Wow, you're today's hero of the Internet. I learned a lot and I didn't even ask. Also I bookmarked this thread.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#6 Post by trooper6 »

Thanks! I don't know if it was explained enough...but I did my best in a tight time frame.
I can expand if there are any questions...and others can jump in as well.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

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

Re: Explain to me like I'm 5: Inventory System

#7 Post by JaxxyLupei »

I agree with Samu; this helped a lot more than any other shop or inventory codes/threads I've found. Thank you very much! I appreciate that you took the time to explain this to me :D

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#8 Post by trooper6 »

Do you all think I should consolidate this and put it in the cookbook?
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

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

Re: Explain to me like I'm 5: Inventory System

#9 Post by JaxxyLupei »

yes! This should be spread.

Pippin123
Regular
Posts: 52
Joined: Sat Jan 31, 2015 7:33 pm
Contact:

Re: Explain to me like I'm 5: Inventory System

#10 Post by Pippin123 »

I'll try to add my two cents on the graphical UI for inventory system.

For this you need two things:
- associate art assets (.jpg or.png) for each of your items
- code a screen that will display the content of the inventory using said assets and image buttons.

1/ Art assets
At minimum, an imagebutton will require 2 images : item_idle.png and item_hover.png
Depending of your game resolution, settle on a reasonable size for your items (e.g. 100x100)

How do we tie each image to each item ?
We use the classes described above, by adding a self.image property that will contain the path to the image.

exemple :

Code: Select all

    $sword = Item("Sword", 150, 5, 10, "items_assets/sword_%s.png")
where item_assets is a subfolder of game that contains both sword_idle.png and sword_hover.png
(the _%s.png is the syntax for the auto image button function)

To code an image_button appearing at (50,50) for this sword, we'll do something like:

Code: Select all

   imagebutton auto sword.image xpos 50 ypos 50 action <some function>
<some function> depends of what you intend to do with the inventory screen: it can be the add or sell function you defined for your backpack

2 / Code a screen.
Here I'm really only repeating what's in http://lemmasoft.renai.us/forums/viewto ... 51&t=23071
To break it down:
- you first create a grid image that will hold the image buttons you 'll create
- You need a bit of python code that picks every item in your backpack, and run the imagebutton code above, except the position is not fixed.
Exemple for a 4x4 screen with 100x100 pictures

Code: Select all

screen inventory_screen:    
    add "gui/inventory_layout.png"
    modal True 
    textbutton "Close" action [ Hide("inventory_screen"), Show("inventory_button")] align (.95,.04)
    $ x0=150
    $ y0=100
    $ i=0
    $ sorted_items = sorted(inventory.items, key=attrgetter('item_type'), reverse=True)
    $ next_inv_page=inv_page+1

    if next_inv_page*16 > len(sorted_items):
        $ next_inv_page=0

    for item in sorted_items:
        if i+1 <= (inv_page+1)*16 and i+1>inv_page*16:
            $ a =i%4
            $ b =(i%16-a)/4

            $ x = a*100+x0
            $ y = b*100+y0
            imagebutton auto item.image xpos x ypos y action [Show("inventory_button"),SetVariable("item", item), Hide("inventory_screen"), item_use] 
        $ i += 1
    if len(sorted_items)>16:
        textbutton _("Next Page") action [SetVariable("inv_page", next_inv_page),Show("inventory_screen")] xpos 500 ypos 500

User avatar
OokamiKasumi
Eileen-Class Veteran
Posts: 1779
Joined: Thu Oct 14, 2010 3:53 am
Completed: 14 games released -- and Counting.
Organization: DarkErotica Games
Deviantart: OokamiKasumi
Location: NC, USA
Contact:

Re: Explain to me like I'm 5: Inventory System

#11 Post by OokamiKasumi »

trooper6 wrote:Do you all think I should consolidate this and put it in the cookbook?
YES, please.
Ookami Kasumi ~ Purveyor of fine Smut.
Most recent Games Completed: For ALL my completed games visit: DarkErotica Games

"No amount of great animation will save a bad story." -- John Lasseter of Pixar

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

Re: Explain to me like I'm 5: Inventory System

#12 Post by JaxxyLupei »

Okay, I've run into a frustrating problem.

trooper6, I've made a few shops in my game with the code you've presented...however, there's an issue whenever I try to go to a menu of purchasable items.

I get this error here:
Image

Here's what I was doing:

Code: Select all

label giftShop:
    
    "What would you like to buy?"
    
    menu giftShopmenu:
        "Look at candy":
            jump candy
        "Look at jewelry":
            jump jewelry
        "Look at toys":
            jump toys
        "Look at flowers":
            jump flowers
        "Look at perfumes":
            jump perfumes
        "Return to the mall":
            call mallMap
            
    return

    menu candy:
        "Blue Raspberry lollipop for [bluelolli.cost] dollars.":
            $ result = myBackpack.add_Item(bluelolli)
            "[result]"
            call giftShopmenu
        "Cherry lollipop for [cherrylolli.cost] dollars.":
            $ result = myBackpack.add_Item(cherrylolli)
            "[result]"
            call giftShopmenu
        "Green Apple lollipop for [applolli.cost] dollars.":
            $ result = myBackpack.add_Item(applolli)
            "[result]"
            call giftShopmenu
        "Orange lollipop for [oranlolli.cost] dollars.":
            $ result = myBackpack.add_Item(oranlolli)
            "[result]"
            call giftShopmenu
        "Strawberry lollipop for [strawlolli.cost] dollars.":
            $ result = myBackpack.add_Item(strawlolli)
            "[result]"
            call giftShopmenu
        "Watermelon lollipop for [waterlolli.cost] dollars.":
            $ result = myBackpack.add_Item(waterlolli)
            "[result]"
            call giftShopmenu
        "Chocolate Bar for [chocobar.cost] dollars.":
            $ result = myBackpack.add_Item(chocobar)
            "[result]"
            call giftShopmenu
        "Look at something else":
            call giftShopmenu
            
    return
Basically the same exact thing you did, except the player goes back to the list of items instead of leaving the store. Even before I put in all the "call giftShopmenu"s, I was still getting this error. If I tried picking one of the other options, I still get it as well.

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#13 Post by trooper6 »

My first thought is...what are those returns doing there? That seems like it would be a problem. Also, I don't think you want to use "call" from that secondary menu, but jump. But let me translate your code into my practice project and see what I come up with.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Explain to me like I'm 5: Inventory System

#14 Post by trooper6 »

Okay so let's talk.

Those returns are used for when you "call" to a place--or when you want to end the game. You call to a label, and then return from whence you came with the return. But you are jumping rather than calling...so you don't need those returns. Second, you start in the first menu then you jump to the second menu...there is no reason for you to Call back to the first menu from the second...because it would result in you jumping back to the second menu from the first and that will cause some problems.

Your issue here is not knowing how to flow from one able to another. You may want to re-read the tutorial on labels and flow, here:
http://www.renpy.org/doc/html/quickstar ... -and-jumps
http://www.renpy.org/doc/html/label.html

The only way I might see a return being useful to this code is at the end of the store if you call to the store label rather than jump to it. This could actually be useful if you call to the store multiple times in the game from different places. That is still only one return at the end of the entire store label.

Anyhow, this is my version of your code. This version of the code works for me:

Code: Select all

label store:
    "You enter the store, the old man offers to sell you one item!"
 
    menu giftShopmenu:
        "Look at candy":
            jump candy
        "Finish Looking":
            jump end
            
    menu candy:
        "What do you buy?"
        "A rose for [rose.cost] gold pieces":
            $result = myBackpack.add_Item(rose)
            "[result]"
            jump giftShopmenu
        "A dagger for [dagger.cost] gold pieces":
            $result = myBackpack.add_Item(dagger)
            "[result]"
            jump giftShopmenu
        "A rope for [rope.cost] gold pieces":
            $result = myBackpack.add_Item(rope)
            "[result]"  
            jump giftShopmenu
        "A horse for [horse.cost] gold pieces":
            $result = myBackpack.add_Item(horse)
            "[result]"
            jump giftShopmenu
        "I won't buy anything":
            jump giftShopmenu
            
     
label end:
    "You adventure is over"
                                                                   
    return
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

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

Re: Explain to me like I'm 5: Inventory System

#15 Post by JaxxyLupei »

I've tried deleting the returns and fixing the calls to jumps...I'm still getting the same error unfortunately.

Post Reply

Who is online

Users browsing this forum: Google [Bot]