Page 1 of 1

Inventories - I need a walkthrough

Posted: Wed Jan 18, 2012 1:57 am
by mishila
Hi.
I need help making an inventory for my game.
It requires you to pick up objects and you will be able to save them, and possibly use them, later on in the game.
Now before you redirect me to this and this, I've seen them. Frankly, I don't understand them, and I would be eternally grateful if someone could break it down for me - namely the first link.
How would I be able to replicate that same concept using screens?
How do I add multiple items to the inventory?
And... where do I define item_name? How would I show a picture of the item, also? Is there a way to show every item in the inventory at once, and once clicked on, they can be enlarged [think CG gallery].
I'm so ignorant. Even if you can just answer one of my questions, you will have helped me.
Thank you!

Re: Inventories - I need a walkthrough

Posted: Wed Jan 18, 2012 1:15 pm
by leon
I'm pretty new to Ren'py, but here's my interpretation of the code:

Code: Select all

init python:   
    showitems = True # variable showitems is set here. This line must be before "if showitems:" line or there will be an error
#missing init of items! add the next line:
    items = [] # this variable stores the items. Here we give it an empty value - a list with no elements. We need this or there will be an error, the first time we try to use this variable in our function

    def display_items_overlay(): # we define a new function - the indented part 
        if showitems: # if showitems is False, nothing happens
            inventory_show = "Inventory: " # inventory_show is a string variable. here we give it the initial value - the text "Inventory: ".
            for i in range(0, len(items)): # this for loop repeats indented parts. variable "i" has a value 0 in the first repetition; "i" increases by 1 with every repetition, until "i" is equal to the number of items. Function len(items) returns the number of items.
                item_name = items[i].title() # items[i] represents individual inventory items, where "i" is the items index - from 0 to the number of items. 
"Items" is a variable/object of type list. Calling the title() function, returns the name of a list item, as a string - in our case - this is the name of inventory item. We store the name of the item (string) into variable "item_name".
                if i > 0:
                    inventory_show += ", " # we add a comma to the string "inventory_show" for every repetition, except the first time
                inventory_show += item_name # for every repetition, we add a string variable "item_name" (we stored it a few lines back), to the (also string) variable "inventory_show".

# this is outside the for loop. "inventory_show" should now contain the text we want to display - all inventory items listed with commas.
            ui.frame() # draws a frame around the text
            ui.text(inventory_show) # displays the text - inventory_show is the variable we prepared earlier.

    config.overlay_functions.append(display_items_overlay) # this line ensures, we don't need to call the function we defined. Instead, we control whether inventory is shown or not by setting the value of showitems to False or True. Magic...

# end of python code; you use these lines to control the inventory in Ren'py code:
$ items.append("stone") #when you want to add items
$ items.remove("stone")#when you want to remove items
$ showitems = False #when you don't want to show the inventory onscreen (cutscenes and the like)
$ showitems = True #when you want to reshow the inventory after the cutscene is over
How would I be able to replicate that same concept using screens?
This code shows an information screen: http://www.renpy.org/wiki/renpy/doc/coo ... ion_Screen
How do I add multiple items to the inventory?
You use the same code to add the item multiple times. To add 3 stones:
$ items.append("stone")
$ items.append("stone")
$ items.append("stone")
If you want to do it with a single line of code ($ items.my_append("stone", 3)), you need do define inventory as an object (second inventory example) and define a method/function for adding multiple items.
If you want the display grouped items ("Inventory: 3 stones, 2 chocolates"), you need to modify the code, that prepares the variable "inventory_show" in the for loop.
And... where do I define item_name?
Item name is a variable, internal to the function for inventory display. You don't need to define it, or use it, outside of this function. When you add the items with "append", the string you are appending will be used as item_name.
How would I show a picture of the item, also?
Replace the code in the for loop where "inventory_show" is being prepared.
Instead of adding the item name to the text that will be display (inventory_show += item_name), just display the picture instead and delete all parts that prepare and display the text. You may need to handle x,y coordinates for the item images (something like x=i*100), based on item index(i).
Is there a way to show every item in the inventory at once, and once clicked on, they can be enlarged [think CG gallery].
Check out imagebuttons in the Information_Screen example.

Re: Inventories - I need a walkthrough

Posted: Sat Feb 11, 2012 10:47 pm
by mishila
Thank you for being so thorough! I will put this to good use - you have helped me immensely.

Re: Inventories - I need a walkthrough

Posted: Thu Mar 01, 2012 7:39 am
by kenobihiro
leon wrote:I'm pretty new to Ren'py, but here's my interpretation of the code:

Code: Select all

init python:   
    showitems = True # variable showitems is set here. This line must be before "if showitems:" line or there will be an error
#missing init of items! add the next line:
    items = [] # this variable stores the items. Here we give it an empty value - a list with no elements. We need this or there will be an error, the first time we try to use this variable in our function

    def display_items_overlay(): # we define a new function - the indented part 
        if showitems: # if showitems is False, nothing happens
            inventory_show = "Inventory: " # inventory_show is a string variable. here we give it the initial value - the text "Inventory: ".
            for i in range(0, len(items)): # this for loop repeats indented parts. variable "i" has a value 0 in the first repetition; "i" increases by 1 with every repetition, until "i" is equal to the number of items. Function len(items) returns the number of items.
                item_name = items[i].title() # items[i] represents individual inventory items, where "i" is the items index - from 0 to the number of items. 
"Items" is a variable/object of type list. Calling the title() function, returns the name of a list item, as a string - in our case - this is the name of inventory item. We store the name of the item (string) into variable "item_name".
                if i > 0:
                    inventory_show += ", " # we add a comma to the string "inventory_show" for every repetition, except the first time
                inventory_show += item_name # for every repetition, we add a string variable "item_name" (we stored it a few lines back), to the (also string) variable "inventory_show".

# this is outside the for loop. "inventory_show" should now contain the text we want to display - all inventory items listed with commas.
            ui.frame() # draws a frame around the text
            ui.text(inventory_show) # displays the text - inventory_show is the variable we prepared earlier.

    config.overlay_functions.append(display_items_overlay) # this line ensures, we don't need to call the function we defined. Instead, we control whether inventory is shown or not by setting the value of showitems to False or True. Magic...

# end of python code; you use these lines to control the inventory in Ren'py code:
$ items.append("stone") #when you want to add items
$ items.remove("stone")#when you want to remove items
$ showitems = False #when you don't want to show the inventory onscreen (cutscenes and the like)
$ showitems = True #when you want to reshow the inventory after the cutscene is over
How would I be able to replicate that same concept using screens?
This code shows an information screen: http://www.renpy.org/wiki/renpy/doc/coo ... ion_Screen
How do I add multiple items to the inventory?
You use the same code to add the item multiple times. To add 3 stones:
$ items.append("stone")
$ items.append("stone")
$ items.append("stone")
If you want to do it with a single line of code ($ items.my_append("stone", 3)), you need do define inventory as an object (second inventory example) and define a method/function for adding multiple items.
If you want the display grouped items ("Inventory: 3 stones, 2 chocolates"), you need to modify the code, that prepares the variable "inventory_show" in the for loop.
And... where do I define item_name?
Item name is a variable, internal to the function for inventory display. You don't need to define it, or use it, outside of this function. When you add the items with "append", the string you are appending will be used as item_name.
How would I show a picture of the item, also?
Replace the code in the for loop where "inventory_show" is being prepared.
Instead of adding the item name to the text that will be display (inventory_show += item_name), just display the picture instead and delete all parts that prepare and display the text. You may need to handle x,y coordinates for the item images (something like x=i*100), based on item index(i).
Is there a way to show every item in the inventory at once, and once clicked on, they can be enlarged [think CG gallery].
Check out imagebuttons in the Information_Screen example.

Thank very much for this post. Can i ask if there a way to interact with those item being displayed on-screen ( like a button to trigger an event)?

Re: Inventories - I need a walkthrough

Posted: Fri Mar 02, 2012 9:12 pm
by leon
@kenobihiro: If you need to trigger events, you use imagebuttons to display the items. http://www.renpy.org/doc/html/screens.html#imagebutton