u/Discord_Melody

Image 1 — Help? My Inventory System isn't removing the right amount of items.
Image 2 — Help? My Inventory System isn't removing the right amount of items.
▲ 3 r/RenPy

Help? My Inventory System isn't removing the right amount of items.

Hi. It's me again, lol. Back with another question...

So I'm having a little trouble and I can't seem to figure out what's going on. So for the past few days, I've been working on and testing a simple inventory/gift system for my visual novel project, but I've run into a problem. When I remove one item from my inventory, it seems to take out two items instead. I'm not completely sure why it's doing that, especially when I have it set to only take the amount I requested. Here's the code for the inventory system and screenshots of what I'm getting. I'm completely stumped. Maybe someone here can see what I'm doing wrong and steer me in the right difrection? Thanks in advance!

Inventory Code

init python:
    class Item:
        def __init__(self, name, image, desc=""):
            self.name = name
            self.image = image
            self.desc = desc


    class Inventory:
        def __init__(self, slots=10):
            self.items = {}
            self.slots = slots
            self.unlocked_slots = 5


        def add(self, item, amount=1):
            if item in self.items:
                self.items[item] += amount
            elif len(self.items) < self.unlocked_slots:
                self.items[item] = amount
            else:
                renpy.notify("Inventory full!")


        def remove(self, item, amount=1):
            if item in self.items:
                if self.items[item] > amount:
                    self.items[item] -= amount
                else:
                    del self.items[item]


        def unlock_slot(self, count=1):
            self.unlocked_slots = min(self.unlocked_slots + count, self.slots)


# Define Items/Inventory
default apple = Item("apple", "images/apple.png", "A sweet red apple.")
default diamond = Item("diamond", "images/diamond.png", "A shiny diamond.")


default my_inv = Inventory(slots=20)

Inventory Screen

screen inventory_screen(target_char=None):
    modal True
    frame:
        xalign 0.5 yalign 0.5
        xsize 1400 ysize 800
        background Frame ("gui/inventory_frame_bg.png", 1400, 800)
        vbox:
            text "Inventory" size 30 xalign 0.5
            
            viewport id "inv_vp":
                scrollbars "vertical"
                mousewheel True
                draggable True
                
                vpgrid cols 5 spacing 10:
                    # Items in Unlocked Slots
                    for item, qty in my_inv.items.items():
                        frame:
                            style "empty"
                            background Frame ("gui/slot_bg5.png", xsize=256, ysize=256)
                            vbox:
                                imagebutton idle item.image action NullAction()
                                text "[qty]" size 25 xalign 1.0
                                if target_char:
                                    textbutton "Gift" action [Function(my_inv.remove, item), Return(item)]
                    
                    # Unlocked Slots
                    for i in range(my_inv.unlocked_slots - len(my_inv.items)):
                        add "gui/slot_bg5.png":
                            xysize (256, 256)


                    # Locked Slots
                    for i in range(my_inv.slots - my_inv.unlocked_slots):
                        add "gui/locked_slot_bg.png":
                            xysize (256, 256)
            textbutton "Close" action [Hide("inventory_screen"), Return()] align (1.0, 1.0)

Testing Script

define e = Character("Eileen")

# The game starts here.

label start:

    scene bg room

    show eileen happy

    "What would you like to do?"

    menu:

        "Explore.":

            "You are exploring..."

            $ my_inv.add(apple, 5)

            $ my_inv.add(diamond,5)

            $ my_inv.add(diamond,1)

            "You found an apple!"

            "You found some diamonds!"

            jump meeting_npc

        "Visit Eileen.":

            jump meeting_npc

        "Unlock a slot!":

            $ my_inv.unlock_slot(1)

            "You unlocked a slot!"

            jump start

label meeting_npc:

    e "Hello!"

    menu:

        "Give a gift":

            jump gift_process

            

            pause

        "Talk":

            "Nice weather."

            jump meeting_npc

label gift_process:

    call screen inventory_screen(
target_char
="Eileen")

    $ given_item = _return

    if given_item == apple:

        e "Oh, I love donuts! Thank you!"

        $ my_inv.remove(apple, 1)

        e "Would you like to give another gift?"

        menu:

            "Yes.":

                jump meeting_npc

            "No.":

                e "Thanks for the gift!"

                return

    elif given_item == diamond:

        e "Oooh so sparkly! Thank you!"

        $ my_inv.remove(diamond, 1)

        e "Would you like to give another gift?"

        menu:

            "Yes.":

                jump meeting_npc

            "No.":

                e "Thanks for the gift!"

                return

    else:

        e "Changed your mind? That's okay!"

    return
u/Discord_Melody — 6 days ago