How do you use the On-Screen Inventory code?

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
User avatar
Kokoro Hane
Eileen-Class Veteran
Posts: 1219
Joined: Thu Oct 27, 2011 6:51 pm
Completed: 30 Kilowatt Hours Left, The Only One Girl { First Quarter }, An Encounter ~In The Rain~, A Piece of Sweetness, Since When Did I Have a Combat Butler?!, Piece by Piece, +many more
Projects: Fateful Encounter, Operation: Magic Hero
Organization: Tofu Sheets Visual
Deviantart: kokoro-hane
itch: tofu-sheets-visual
Contact:

How do you use the On-Screen Inventory code?

#1 Post by Kokoro Hane » Tue Sep 18, 2012 10:44 pm

http://www.renpy.org/wiki/renpy/doc/coo ... _Inventory

I am having trouble with this code (the first one, inventory). I have no idea how it works or how to implement it. It would be a REALLY USEFUL code to me, if I could get it to work. Thanks in advanced!
PROJECTS:
Operation: Magic Hero [WiP]
Piece By Piece [COMPLETE][Spooktober VN '20]
RE/COUNT RE:VERSE [COMPLETE][RPG]
Since When Did I Have a Combat Butler?! [COMPLETE][NaNoRenO2020+]
Crystal Captor: Memory Chronicle Finale [COMPLETE][RPG][#1 in So Bad It's Good jam '17]

But dear God, You're the only North Star I would follow this far
Owl City "Galaxies"

User avatar
kankan
Regular
Posts: 80
Joined: Tue Mar 06, 2012 1:47 am
Contact:

Re: How do you use the On-Screen Inventory code?

#2 Post by kankan » Tue Sep 18, 2012 11:38 pm

Ohh, inventory codes, how fun! There's a few things that got left out of the explanation though; at the moment it looks like it would just cause a few errors instead of showing anything?

Code: Select all

init python:
    showitems = True        # The inventory screen will pop up whenever this is True and hides
                                    # when it's False. 

    items = []              # This wasn't in the sample code, but it's where you keep track of items.
                               # Eventually you can fill it with items, so it would be more like ["Eggs", "Cheese"]
   
    def display_items_overlay():
        if showitems:
            inventory_show = "Inventory: "
            for i in range(0, len(items)):            # This will go through your list of items

     !!!    item_name = items[i].title()        # This is a problem line (read: ERROR); this requires that items be a class (more complex and also undefined at the moment)
     !!!    item_name = items[i]              # Replace the line above if you want it to be a simple inventory, or delete this

                if i > 0:                                   # If the item is the first item in your inventory, don't add a comma before it
                    inventory_show += ", "
                inventory_show += item_name    # This line now reads "Inventory: Item1, Item2, ..." and so on
            ui.frame()
            ui.text(inventory_show)            # The ui functions are what display everything to the screen.

    config.overlay_functions.append(display_items_overlay)        # And this makes the function display depending on showitems
You're probably going to have to make a few changes depending on what you want it to look like or what you want it to do. The two lines with !!! in them especially have to be figured out (though you probably want the bottom one, which is much simpler).
Last edited by kankan on Wed Sep 19, 2012 7:36 pm, edited 1 time in total.

User avatar
Kokoro Hane
Eileen-Class Veteran
Posts: 1219
Joined: Thu Oct 27, 2011 6:51 pm
Completed: 30 Kilowatt Hours Left, The Only One Girl { First Quarter }, An Encounter ~In The Rain~, A Piece of Sweetness, Since When Did I Have a Combat Butler?!, Piece by Piece, +many more
Projects: Fateful Encounter, Operation: Magic Hero
Organization: Tofu Sheets Visual
Deviantart: kokoro-hane
itch: tofu-sheets-visual
Contact:

Re: How do you use the On-Screen Inventory code?

#3 Post by Kokoro Hane » Wed Sep 19, 2012 3:29 pm

Ooh, thank you very much! These all go in the Init section of the script, correct?
PROJECTS:
Operation: Magic Hero [WiP]
Piece By Piece [COMPLETE][Spooktober VN '20]
RE/COUNT RE:VERSE [COMPLETE][RPG]
Since When Did I Have a Combat Butler?! [COMPLETE][NaNoRenO2020+]
Crystal Captor: Memory Chronicle Finale [COMPLETE][RPG][#1 in So Bad It's Good jam '17]

But dear God, You're the only North Star I would follow this far
Owl City "Galaxies"

User avatar
rasburn
Regular
Posts: 70
Joined: Wed Aug 01, 2012 5:53 pm
Contact:

Re: How do you use the On-Screen Inventory code?

#4 Post by rasburn » Wed Sep 19, 2012 3:38 pm

Code: Select all

if i < 0:
I instinctively feel this should be replaced by

Code: Select all

if i > 0:
but haven't tried the code. Am I wrong?

User avatar
kankan
Regular
Posts: 80
Joined: Tue Mar 06, 2012 1:47 am
Contact:

Re: How do you use the On-Screen Inventory code?

#5 Post by kankan » Wed Sep 19, 2012 7:35 pm

rasburn wrote:

Code: Select all

if i < 0:
I instinctively feel this should be replaced by

Code: Select all

if i > 0:
but haven't tried the code. Am I wrong?
Yikes, I messed it up on accident. You're right, I'll fix it. Thanks!

And yep, this goes into the init section!

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: How do you use the On-Screen Inventory code?

#6 Post by asatiir » Sat Oct 12, 2013 10:59 am

I spent the past week trying to figure out how this inventory system works and I'm really lost and my script is error-ridden, can someone please post an example how it really works?

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

Re: How do you use the On-Screen Inventory code?

#7 Post by Alex » Sat Oct 12, 2013 5:44 pm

It is better to post error message you've got and part of your code to let people find the problem.
Here it is an example of code

Code: Select all

# Declare characters used by this game.
define e = Character('Eileen', color="#c8ffc8")

init python:   
    showitems = True
   
    def display_items_overlay():
        if showitems:
            inventory_show = "Inventory: "
            for i in range(0, len(items)):
                item_name = items[i].title()   # all the things are taken from list named "items"
                if i > 0:
                    inventory_show += ", "
                inventory_show += item_name
            ui.frame()
            ui.text(inventory_show)
    config.overlay_functions.append(display_items_overlay)
    
# The game starts here.
label start:
    $ showitems = False  # will hide inventory
    $ items = []     # an empty inventory list named "items"
    e "You got nothing yet..."
    
    $ showitems = True    # will show inventory
    
    e "See it?"
    
    $ items.append("a new Ren\'Py game")   # add an item to inventory list
    e "You've created a new Ren'Py game."

    e "Once you add a story, pictures, and music, you can release it to the world!"
    
    $ items.append("an orange")
    "You've found an orange..."
    
    $ items.remove("an orange")   # remove an item from inventory list
    "... and ate it immediately."
    "Om-nom-nom."

    return
http://docs.python.org/2/tutorial/datastructures.html

Also, this might be useful for you - http://lemmasoft.renai.us/forums/viewto ... 51&t=23071

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: How do you use the On-Screen Inventory code?

#8 Post by asatiir » Sun Oct 13, 2013 12:51 pm

I was following that yesterday and couldn't figure it out, going back to what you posted, I got this error:
I'm sorry, but an uncaught exception occurred.

While running game code:
File "renpy/common/00start.rpy", line 122, in script call
File "game/script.rpy", line 34, in script
File "game/script.rpy", line 34, in python
File "game/script.rpy", line 10, in python
NameError: global name 'items' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
File "C:\renpy-6.15.7-sdk\renpy\execution.py", line 288, in run
node.execute()
File "C:\renpy-6.15.7-sdk\renpy\ast.py", line 718, in execute
renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
File "C:\renpy-6.15.7-sdk\renpy\python.py", line 1297, in py_exec_bytecode
exec bytecode in globals, locals
File "game/script.rpy", line 34, in <module>
$ renpy.pause(0)
File "C:\renpy-6.15.7-sdk\renpy\exports.py", line 848, in pause
rv = renpy.ui.interact(mouse='pause', type='pause', roll_forward=roll_forward)
File "C:\renpy-6.15.7-sdk\renpy\ui.py", line 237, in interact
rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
File "C:\renpy-6.15.7-sdk\renpy\display\core.py", line 1853, in interact
repeat, rv = self.interact_core(preloads=preloads, **kwargs)
File "C:\renpy-6.15.7-sdk\renpy\display\core.py", line 1979, in interact_core
self.compute_overlay()
File "C:\renpy-6.15.7-sdk\renpy\display\core.py", line 1650, in compute_overlay
i()
File "game/script.rpy", line 10, in display_items_overlay
for i in range(0, len(items)):
NameError: global name 'items' is not defined

Windows-7-6.1.7601-SP1
Ren'Py 6.15.7.374
A Ren'Py Game 0.1alpha

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

Re: How do you use the On-Screen Inventory code?

#9 Post by Alex » Sun Oct 13, 2013 5:23 pm

Overlay functions are always shown onscreen, you've set

Code: Select all

init python:   
    showitems = True
that means that the function will show inventory from the very beginning of the game, so at first you need to declare a list named "items" otherwise you'll get an error about "name "items" is not defined".
Check if you have

Code: Select all

$ items = []
right at the beginning of start label (before anything else).

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: How do you use the On-Screen Inventory code?

#10 Post by asatiir » Mon Oct 14, 2013 5:24 am

I understood those parts, but I can't get past the previous error screen.
Image
Image

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

Re: How do you use the On-Screen Inventory code?

#11 Post by Alex » Mon Oct 14, 2013 1:24 pm

I'm not quite understand - do you still get the error message? If so - show the actual code you have (init block and start label) to let me or somebody else find an error.

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: How do you use the On-Screen Inventory code?

#12 Post by asatiir » Mon Oct 14, 2013 4:40 pm

Sure thing, I do appreciate your patience with this.

Code: Select all

# You can place the script of your game in this file.
# Declare images below this line, using the image statement.
# eg. image eileen happy = "eileen_happy.png"

image bg dream1 = "dream1.png"

# Declare characters used by this game.
define a = Character('Voice', color="#c8ffc8")

init python:
    
    showitems = True
    def display_items_overlay():
        if showitems:
            inventory_show = "Inventory: "
            for i in range(0, len(items)):
                item_name = items[i].title()   # all the things are taken from list named "items"
                if i > 0:
                    inventory_show += ", "
                inventory_show += item_name
            ui.frame()
            ui.text(inventory_show)
    config.overlay_functions.append(display_items_overlay)
    ##
$ items.append("stick") #when you want to add items
$ items.remove("stick")#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

label splashscreen:
    $ renpy.music.play("Zombie Hoodoo.mp3")
    $ renpy.pause(0)
    scene black
    show text "Within the Walls, The Wall and Within the Walls: Extended are works of fiction. None of the events represented in this interactive fiction portray or reflect in any way the beliefs of the author or anyone involved in the making of this game." 
    with dissolve
    with Pause(10.0)

    hide text 
    with dissolve
    
    $ renpy.pause(0)
    scene black
    show text "Headphones are recommended." 
    with dissolve
    with Pause(2.0)

    hide text 
    with dissolve
    
    $ renpy.pause(0)
    scene black
    show text "Zombie Camel Games Presents..." 
    with dissolve
    with Pause(2.0)

    hide text 
    with dissolve

    return

init:
    # Use it in subtitle mode.
    $ esubtitle = Character(None,
                            what_size=50,
                            what_outlines=[(3, "#000000", 2, 2), (3, "#000000", 0, 0)],
                            what_layout="subtitle",
                            what_xalign=0.5,
                            what_text_align=0.5,
                            window_background=None,
                            window_yminimum=0,
                            window_xfill=False,
                            window_xalign=0.5)
    
# The game starts here.
label start:
    $ showitems = False  # will hide inventory
    $ items = []     # an empty inventory list named "items"
    scene black
    $ renpy.music.stop(channel="music", fadeout=3)
    esubtitle "Choose your story."
    menu:

        "Within the Walls":
        
            jump withinthewalls1
            
        "Within the Walls: Extended":
        
            jump withinthewalls2
        
        "The Wall (the original short story)":
        
            jump thewall
Image
Image

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

Re: How do you use the On-Screen Inventory code?

#13 Post by Alex » Tue Oct 15, 2013 11:32 am

Well, the thing happens 'cause your game starts not at start label, but at splashscreen label - Ren'Py is trying to show inventory at that point, but "items" list will be defined only at start label.
To fix this just set

Code: Select all

init python:   
    showitems = False
so your inventory function won't be searching for "items" to show at the very beginning of the game and you'll be able to define it at start label and only then change "showitems" value to True.

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: How do you use the On-Screen Inventory code?

#14 Post by asatiir » Wed Oct 16, 2013 3:23 am

That makes so much more sense, so I have to leave the value as false until I open it back on start?
Image
Image

User avatar
asatiir
Regular
Posts: 86
Joined: Tue Oct 01, 2013 6:04 pm
Completed: Within the Walls (Twine)
Projects: Roses Will Rise
Organization: Asatiir's Tales
Skype: asatiir
itch: asatiir
Location: Dubai, UAE
Contact:

Re: How do you use the On-Screen Inventory code?

#15 Post by asatiir » Wed Oct 16, 2013 7:28 am

It worked! Thanks a lot! :D
Image
Image

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]