Item slot placements in inventories

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
kedta35
Regular
Posts: 45
Joined: Wed Aug 30, 2023 1:31 pm
Contact:

Item slot placements in inventories

#1 Post by kedta35 »

I'm using just Ren'Py for the most part to make an inventory system and it works fine enough for what I want but the problem is that I don't know how to make it so that the inventory can be empty and items get ordered into character's inventories once bought.

For example, if your inventory is empty and you buy 3 apples, now your inventory has three apples in slot 1. But, if slot 1 already had a different item, then the apples go into slot 2 etc...

I should note that the item slot positions are very specific based on the image of the inventory so just using spaces in a hbox or vbox isn't really accurate unless there is a way of going about it that I haven't seen, which may be likely.

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Item slot placements in inventories

#2 Post by jeffster »

For every slot of the inventory, certain information should be stored:

* position (x, y) of the slot,
* what item is there in the slot.

For example:

Code: Select all

default inventory = [
    {"pos": (100, 20), "item": None},
    {"pos": (100, 120), "item": None},
    {"pos": (100, 220), "item": None},
    {"pos": (100, 320), "item": None},
    {"pos": (100, 420), "item": None},
    {"pos": (200, 20), "item": None},
    {"pos": (200, 120), "item": None},
    {"pos": (200, 220), "item": None},
    {"pos": (200, 320), "item": None},
    {"pos": (200, 420), "item": None},
    ]
Then to check which is the first empty slot:

Code: Select all

init python:
    def first_free(inv):
        for i in range(0, len(inv)):
            if inv[i]["item"] is None:
                return i       # Return the index of the first free slot in the inventory

        return -1 # No free slots in the inventory - return some special code (-1)
Therefore to put an item in the first free slot:

Code: Select all

    "You found an apple."
    $ ff = first_free(inventory)
    if ff < 0:
        "You can't put it in the bag. The bag is full."
    else:
        $ inventory[ff]["item"] = "apple"
        "You put it in the bag."
Here when I assign an item to an inventory slot, I assume that all items are stored as a dict:

Code: Select all

default items = {
    "apple": ["items/apple.png", "A fruit containing vitamins, potassium etc. Yummy!"],
    "arrow": ["items/arrow.png", "An arrow-shaped piece of plastic."],
    "avocado": ["items/avocado.png", "A mysterious green object. Is it plastic?"],
   #...
    }
Of course this is just a crude example. (E.g. it has no amount of items). But the idea should be clear: you store the information about the inventory and its items, and use it, perhaps with convenience functions.

Here's how I did it in my version of inventory:
viewtopic.php?t=66699

In the items screen you can show items at positions taken from inventory list:

Code: Select all

screen inventory():
    add "images/inventory.webp"
    for i in inventory:
        if i["item"] is not None:
            add items[i["item"]][0]:
                pos i["pos"]
                tooltip items[i["item"]][1]

kedta35
Regular
Posts: 45
Joined: Wed Aug 30, 2023 1:31 pm
Contact:

Re: Item slot placements in inventories

#3 Post by kedta35 »

I tried implementing it into my game but I got an error saying that "tooltip" is not a keyword argument or valid child of the add statement.

Code: Select all

screen inventory():
    add "images/inventory.webp"
    for i in inventory:
        if i["item"] is not None:
            add items[i["item"]][0]:
                pos i["pos"]
                tooltip items[i["item"]][1]
Also, how can I add items from a button on a screen with that code?

Code: Select all

    "You found an apple."
    $ ff = first_free(inventory)
    if ff < 0:
        "You can't put it in the bag. The bag is full."
    else:
        $ inventory[ff]["item"] = "apple"
        "You put it in the bag."

jeffster
Veteran
Posts: 409
Joined: Wed Feb 03, 2021 9:55 pm
Contact:

Re: Item slot placements in inventories

#4 Post by jeffster »

kedta35 wrote: Sun Apr 07, 2024 7:18 pm I tried implementing it into my game but I got an error saying that "tooltip" is not a keyword argument or valid child of the add statement.
There is official Ren'Py documentation here:
https://renpy.org/doc/html/
Most problems can be solved by searching that site.

About tooltip: my bad. Searching documentation helps:
https://renpy.org/doc/html/screen_actions.html#tooltips

So let's use imagebutton or some other displayable instead of "add":

Code: Select all

screen inventory():
    add "images/inventory.webp"
    for i in inventory:
        if i["item"] is not None:
            imagebutton:
                idle items[i["item"]][0]
                pos i["pos"]
                tooltip items[i["item"]][1]

    # This has to be the last thing shown in the screen.
    $ tooltip = GetTooltip()
    if tooltip:
        nearrect:
            focus "tooltip"
            prefer_top True
            frame:
                xalign 0.5
                text tooltip
Also, how can I add items from a button on a screen with that code?

Code: Select all

    "You found an apple."
    $ ff = first_free(inventory)
    if ff < 0:
        "You can't put it in the bag. The bag is full."
    else:
        $ inventory[ff]["item"] = "apple"
        "You put it in the bag."
I don't understand the question. This code is for the VN script. Like, you go somewhere, perhaps choose some path etc., and you find an apple.

If you want to click a button on screen and have items added, then that button should have action calling some function or script which would add those items. Example:

Code: Select all

screen click_apple():
    add "images/garden_background.webp"
    imagebutton:
        idle "images/garden_apple.webp"
        pos (400, 200)
        action Jump("get_apple")
    imagebutton:
        idle "images/garden_exit.webp"
        pos (900, 600)
        action Return()

label start:
    call screen click_apple
    "You don't have apples."
    return

label get_apple:
    $ ff = first_free(inventory)
    if ff >= 0:
        "You got an apple."
        $ inventory[ff]["item"] = "apple"
    return

kedta35
Regular
Posts: 45
Joined: Wed Aug 30, 2023 1:31 pm
Contact:

Re: Item slot placements in inventories

#5 Post by kedta35 »

How would you make it so that only one image of an item goes into the inventory and if more is added then it's shown in text using its variable since it currently adds more singular images of the same item if you add multiple.

And, what I meant by adding with an imagebutton was how do I do this:

Code: Select all

$ inventory[ff]["item"] = "apple"
but with the SetVariable() action in an imagebutton

Post Reply

Who is online

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