a very simple screen-based inventory

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
neowired
Regular
Posts: 199
Joined: Mon Dec 01, 2008 2:33 pm
Contact:

a very simple screen-based inventory

#1 Post by neowired »

Here's a simple screen language inventory for people like me ;p. All this code requires to work is some lists and for loops. If your inventory is fairly simple and you only have s few items total, making it this way seems much simpler and faster to me. Maybe it will help someone else.

All you need to do to manipulate this is to define two lists, one will keep the item names, the other will keep the amount. This way you won't have to append any lists with new items, just define the items beforehand. You can define a list like any variable anywhere in the code, but these have to be defined before you open the inventory menu.

Code: Select all

    $ item_name = ["cat","dog","unkle steve"]
    $ item_amount = [0,0,0]
You could combine the lists into one list but I find it easier to control and read this way.

you can now modify the amount of item like this (note, all lists start with a 0 item, not 1):

Code: Select all

    $ item_amount[0] += 1 # <- adds one to the first item
    $ item_amount[1] = 5 # <- sets the amount of the second item to 5
to open the screen write:

Code: Select all

    show screen simple_example_inventory
to hide:

Code: Select all

    hide screen simple_example_inventory
You may also need to define the a,b variables

Code: Select all

    $ a = 0
    $ b = 0
in screens.rpy add the code which defines the inventory screen (just put it at the end or something)

Code: Select all

screen simple_example_inventory: # <- this is how you define the beginning of a screen
    frame xalign 0.5 ypos 0.1:
        vbox:
            for a in range (0,len(item_name)): # <- for every item on the list between 0 and list length
                if item_amount[a] > 0: # <- checks if the amount of that item is more than 0
                    $ b = item_name[a]
                    text "[b]" # <-display the item name
you can now modify the inventory screen in various ways

for example you can show the amount of the item

Code: Select all

screen simple_example_inventory:    
    frame xalign 0.5 ypos 0.1:
        vbox:
            for a in range (0,len(item_name)):
                if item_amount[a] > 0:
                    $ b = variable_name[a]
                    $ c = variable_amount[a] # <-checks the amount of the item
                    text "[b]: [c]" # <-displays both values


you can turn the items into buttons, you would probably have to define a new list for actions

Code: Select all

    $ item_action = ["petthecat", "lickthedog", "biteunkle"]
if you define it this way, you can use it as a jump or call to a label:

Code: Select all

screen simple_example_inventory:
    frame xalign 0.5 ypos 0.1:
        vbox:
            for a in range (0,len(item_name)):
                if item_amount[a] > 0:
                    $ b = item_name[a]
                    $ c = item_amount[a]
                    $ d = item_action[a] # <- checks the name of the corresponding label on the list
                    textbutton "[b]: [c]" action Jump(d)# <-turns the items into buttons which jump to the corresponding label but you need to have a label with the corresponding name somewhere or it will error
If will want to have image buttons you can define another list with the image names and just repeat what I did above.
This is only a starter, you can expand or modify it in a variety of ways. For example you could add a variable based always on-screen inventory button which opens or closes the inventory. You could change the inventory buttons into pictures and so on.
Last edited by neowired on Wed Feb 26, 2014 10:14 am, edited 2 times in total.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: a very simple screen-based inventory

#2 Post by xela »

What is return() bring used for? You should have used dict for this as well.

There are other examples of inventories available in the cookbook already btw... I wonder if there even is a demand for more inventory code, may it be simple or easy to use advanced?
Like what we're doing? Support us at:
Image

neowired
Regular
Posts: 199
Joined: Mon Dec 01, 2008 2:33 pm
Contact:

Re: a very simple screen-based inventory

#3 Post by neowired »

Would it work without the return()?

Lists, because from my perspective dictionaries make this more complicated to understand and to edit.
There are other examples of inventories available in the cookbook already btw...
I know there are. Most of them seem to be based on classes, which I find difficult to understand even years after playing with renpy. Modifying or expanding them also seems more difficult to me.
I wonder if there even is a demand for more inventory code, may it be simple or easy to use advanced?
I'm not selling anything, what do I care about demand?
If someone finds this useful, great, I helped someone.

I'm sure you can make a much more robust one based on classes and functions, you are probably a programmer of some kind. This example isn't directed at you.

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: a very simple screen-based inventory

#4 Post by xela »

neowired wrote: Lists, because from my perspective dictionaries make this more complicated to understand and to edit.

Code: Select all

$ items = {"cat": 1, "dog": 1}

$ items["cat"] += 1

Code: Select all

screen simple_example_inventory:   
    frame xalign 0.5 ypos 0.1:
        vbox:
            for item in items:
                if items[item] > 0:
                    $ amount = items[item]
                    text "[item]: [amount]"
Matter of opinion I guess.


neowired wrote:Would it work without the return()?
Yes.


neowired wrote: If someone finds this useful, great, I helped someone.
You're right.


neowired wrote: you can turn the items into buttons, you would probably have to define a new list for actions

Code: Select all

    $ item_action = ["petthecat", "lickthedog", "biteunkle"]
if you define it this way, you can use it as a jump or call to a label:

Code: Select all

screen simple_example_inventory:
    frame xalign 0.5 ypos 0.1:
        vbox:
            for a in range (0,len(item_name)):
                if item_amount[a] > 0:
                    $ b = item_name[a]
                    $ c = item_amount[a]
                    $ d = item_action[a] # <- checks the name of the corresponding label on the list
                    textbutton "[b]: [c]" action _intra_jumps(d, "main_game_transition")# <-turns the items into buttons which jump to the corresponding label but you need to have a label with the corresponding name somewhere or it will error
return()
If will want to have image buttons you can define another list with the image names and just repeat what I did above.
This is only a starter, you can expand or modify it in a variety of ways. For example you could add a variable based always on-screen inventory button which opens or closes the inventory. You could change the inventory buttons into pictures and so on.
I am sorry but you're plainly suggesting two poor practices here, one using _intra_jumps instead of a normal Jump() what would confuse about 80% of people even amongst experienced users, second creating yet another list that has to be maintained and matched the first two because it will cause index errors if someone appends something to an item list and not any of the other two.

The second one is basically why I suggested using a dict. You can simply use something like item name + label as label names and you'll never have to think about it again just making sure labels for all items are there.

With a dict and the above, this would be all that's required:

Code: Select all

screen simple_example_inventory:   
    frame xalign 0.5 ypos 0.1:
        vbox:
            for item in items:
                if items[item] > 0:
                    $ amount = items[item]
                    textbutton "[item]: [amount]" action Jump(item + "_label") # This would jump to a label called cat_label or dog_label
Like what we're doing? Support us at:
Image

neowired
Regular
Posts: 199
Joined: Mon Dec 01, 2008 2:33 pm
Contact:

Re: a very simple screen-based inventory

#5 Post by neowired »

I am sorry but you're plainly suggesting two poor practices here, one using _intra_jumps instead of a normal Jump() what would confuse about 80% of people even amongst experienced users,
You may be right with jump. I don't remember why I was using intra jumps, I'm fairly sure I needed it at some point to do something in my own code and it stayed. I don't even remember what was the difference between the two. I have been using renpy for years so something may have changed over the years, or maybe I'm just stupid.
I assume you are right with the return and jump so I edited it accordingly. I will have to check it myself later.
second creating yet another list that has to be maintained and matched the first two because it will cause index errors if someone appends something to an item list and not any of the other two. The second one is basically why I suggested using a dict.
It is more confusing for me when I use a dictionary. If I would want to add more options then I would have to make two or more dictionaries instead.
When I have to define all the key names I feel it's easier to make errors.

If you have 10 or even 20 items in a game, controlling two or 3 lists with those items is not a problem (neither should dictionary be a problem, but for someone like me lists feel easier to control), especially if you keep them near each other. This being lists makes the code shorter because you don't have to write the key names. Shorter code is easier to control for me. I find this to be easier to read.
I know you can use a tuple(?), lists nested in lists or in dictionaries, but I feel that also makes the code less readable. It's easier for me to look through the items when it's just item names in one list, and item amounts in a second list.

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: a very simple screen-based inventory

#6 Post by verysunshine »

Is it possible to use an items name instead of its numeric value when changing how many you have?

This would change

Code: Select all

    $ item_amount[0] += 1 # <- adds one to the first item
to

Code: Select all

    $ item_amount[cat] += 1 # <- adds one to the first item
I'm not always going to remember what item 0 is in the list, but I will know what it is if I can call it by its name.

I've already tabbed the lists to make them easier to read. I have 7 items right now, and that list is probably going to grow. That makes counting by hand tricky for me. Solution: Add a new line every 5 items.

Edit: I've just realised that adding new items anywhere other than the end will cause incorrect values to be assigned, which is fine if your items don't need to be in an order, but if you want to have anything like "apple, banana, pear, cat, dog, bird" and add "strawberry" to the list with the rest of the fruit, it would move items over by one. Unfortunately, this makes the inventory management system useless for me.

Looks like it might work for day tracking, though.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: a very simple screen-based inventory

#7 Post by Alex »

verysunshine wrote: Fri Feb 02, 2018 12:08 am Is it possible to use an items name instead of its numeric value when changing how many you have?
You should use dictionaries to store the inventory items, like xela suggested above.
https://docs.python.org/2.7/tutorial/da ... ctionaries

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: a very simple screen-based inventory

#8 Post by verysunshine »

Alex wrote: Fri Feb 02, 2018 5:06 pm
verysunshine wrote: Fri Feb 02, 2018 12:08 am Is it possible to use an items name instead of its numeric value when changing how many you have?
You should use dictionaries to store the inventory items, like xela suggested above.
https://docs.python.org/2.7/tutorial/da ... ctionaries
Yes, I did eventually figure that out. Thank you, Alex.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

Post Reply

Who is online

Users browsing this forum: No registered users