Inventory creation?

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
casualbird
Newbie
Posts: 1
Joined: Sun Jan 14, 2018 9:06 pm
Contact:

Inventory creation?

#1 Post by casualbird »

Basically, the idea is to create some kind of separate screen where the player can go at any time to review things they've collected. It doesn't need to be complicated--the inventory items don't need to do anything, but the game is a mystery and the screen would function as a place to review what evidence they've collected. It could be entirely text-based, if need be.

I looked through the page on screens, but I couldn't figure out how to make my own--could someone explain, please?

User avatar
jesusalva
Regular
Posts: 88
Joined: Mon Jul 22, 2013 5:05 pm
Organization: Software in Public Interest, Inc.
IRC Nick: jesusalva
Github: pazkero
itch: tmw2
Location: Brazil
Discord: Jesusalva#4449
Contact:

Re: Inventory creation?

#2 Post by jesusalva »

Roughly, you need to create the inventory, create a screen to display the items, and then append it to the overlay.
Well, I only code for hobby, but if I understood your question, you do not need a ready inventory, but you want to learn how to do it.

...Otherwise, you should be looking at the cookbook: viewtopic.php?f=51&t=34131
(I guess there are other links to cookbooks too, but that one should do it.)

Anyway, here is an example of how to do it by yourself (code may not work):

1- You create an inventory, eg.

Code: Select all

items=[]
It may be an array or a dictionary. That will change how things work. I am using an array because it is easier to grasp the concepts.

2- You create a screen, with a viewport, to display all items.
It may look like this, if you're storing only strings at items array:

Code: Select all

screen inv():
    # Create a frame widget. You could do this with side, fixed, or whatever you prefer.
    frame:
        align (.5,.5)   # align the screen at middle
        maximum (.8,.8) # set maximum width and height.
        minimum (.8,.8) # set minimum too, so it is a square.
        vbox:
            viewport id "vp":
                draggable True

                ## To list all items, one at side of the other, I use an hbox.
                ## You can use anything which can nest multiple widgets, like vbox.
                ## A grid can also do the work, but you may need to calculate how
                ## many items are missing to close it, and how many columns you need.
                hbox:
                    ## Create a textbutton with the value of items, but makes button un-clickable.
                    for i in items:
                        textbutton i action None
            ## Now we're done, we need to add a scrollbar, and to add a close button.
            bar value XScrollValue("vp") # Replace with a vbar and YScrollValue if you used an vbox.
            textbutton "Return" action Return() # Closes the screen.
3- You probably want an overlay button to open and close the screen.
This is legacy code, and must be replaced. It may not even work.
If you examin a little the cookbook you'll find a proper replacement for it:

Code: Select all

    def show_inv_button():
        ## The can_use_inv variable is important if at some moments you're not
        ## supposed to be able to use it.
        if can_use_inv:
            ## Using ui functions is not advised.
            ui.add(inv_ctrl)
    config.overlay_functions.append(show_inv_button)

    def inv_ctrl():
        ui.frame(xalign = 0.05, yalign=0.95, xmaximum=0.3, ymaximum=0.1)
        ui.textbutton(_("Inventory"), clicked=ui.callsinnewcontext('show_inv'), size=12)
        #renpy.call_screen("inv")

label show_inv:
    call screen inv
    return
The above code can be optimized and improved.
Mind the can_use_inv variable: This allows you to control when you can and when you can't use the inventory.

4- By last, an example of script using it.

Code: Select all

label test_inv:
    $ can_use_inv=True
    "I can now check my backpack."
    $ items.append("Renpy Manual")
    "Lucky me, I've found a manual!"
    $ can_use_inv=False
    "I'm now too scared to use the inventory!"
    return
You can then customize as you want, to display images, to group multiple of the same item, improve display performance, and even use renpy.notify() to warn the player when you add an item...
Jesusaves/Jesusalva

Post Reply

Who is online

Users browsing this forum: Google [Bot]