[solved] multiple of the same item in inventory giving problems

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
zant19
Newbie
Posts: 6
Joined: Sun Dec 21, 2014 6:33 am
Contact:

[solved] multiple of the same item in inventory giving problems

#1 Post by zant19 »

So the problem is that I can equip the items just fine as long as their are not multiple of the same item in the inventory. but as soon as I equip an item that is the same as in the inventory I become unable to unequip it or change it out. I have tried multiple things (which has helped me make the code much cleaner) but I keep having the same problem with the button being unelectable.

I made a simple test game and the same thing is still happening.
just drop it images into the image folder and it should do the same for others.

to see what is wrong you can click on the saber and it will work properly, but as soon as sword is selected the problem happens.

Code: Select all


init python:
    class InvItem:
        def __init__(self, name, cost, img):
            self.name = name
            self.cost = cost
            self.img = img


    class Equipable(InvItem):
        def __init__(self, name, cost, img):
            InvItem.__init__(self, name, cost, img)

        def equip(self, target):
            self.is_equipped = True
            self.equipped_to = target

        def unequip(self, target):
            self.is_equipper = False
            self.equipped_to = None

    class Weapon(Equipable):
        def __init__(self, name, cost, img, atk, wpn_type):
            Equipable.__init__(self, name, cost, img)
            self.atk = atk
            self.wpn_type = wpn_type

        def equip(self, target):
            Equipable.equip(self, target)
            target.equip_weapon(self)

        def unequip(self, target):
            self.equipped_to.unequip_weapon(self, target)
            Equipable.unequip(self)



    class Player:
        def __init__(self, name, hp, mp, max_hp, max_mp, atk):
            self.name = name
            self.hp = hp
            self.mp = mp
            self.max_hp = hp
            self.max_mp = mp
            self.atk = atk
            self.weapon = None


        def equip_weapon(self, weapon):
            if self.weapon != None:
                self.unequip_weapon()
            self.weapon = weapon
            self.atk += weapon.atk

        def unequip_weapon(self):
            if self.weapon != None:
                self.atk -= self.weapon.atk
                self.weapon = None


default inventory = []
default heartinventory = []
default player = Player("Player", 25, 25, 25, 25, 5)


# The game starts here.
label start:


    default sword = Weapon("Sword", 1, "weapon_sword.png", 1, "sword")                 #
    default saber = Weapon("Saber", 1, "weapon_saber.png", 2, "sword")                 #



    $ inventory.append(saber)
    $ heartinventory.append(sword)
    $ heartinventory.append(sword)

    label mainbody_build:
        scene bg getinmybelly
        show screen mainbody_build_screen_inventory
        show screen mainbody_build_screen_heartinventory
        show screen mainbody_build_screen_equipment
        call screen mainbody_build_screen
        pause



    screen mainbody_build_screen:

        hbox:
            spacing 150
            vbox:
                xsize 200
                spacing 10
                label "Character Details" xalign 0.5
                label "HP: [player.hp]/[player.max_hp]"
                label "atk: [player.atk]"
                textbutton "Return":
                    action Hide("mainbody_build_screen_heartinventory"), Hide("mainbody_build_screen_inventory"), Jump("Heartworld")


    screen mainbody_build_screen_equipment:
        
        frame:
            xsize 300
            ysize 300
            xpos .2 ypos .1
            imagebutton:
                idle [If(player.weapon == None, "equipment_weapon.png", "[player.weapon.img]")]
                xalign .3 yalign .3
                action If(player.weapon != None, true = AddToSet(heartinventory, player.weapon)), Function(player.unequip_weapon), Jump("mainbody_build")



    screen mainbody_build_screen_inventory:

        vpgrid:
            cols 1
            spacing 5
            draggable True
            mousewheel True
            scrollbars "vertical"
            xpos .6 ypos .1
            xsize 300 ysize 300
            for i in inventory:
                if isinstance(i, Weapon):
                    imagebutton:
                        idle i.img
                        action If(player.weapon != None, true = AddToSet(heartinventory, player.weapon)), Function(i.equip, player), RemoveFromSet(inventory, i)


    screen mainbody_build_screen_heartinventory:

        vpgrid:
            cols 4
            spacing 5
            draggable True
            mousewheel True
            scrollbars "vertical"
            xpos .7 ypos .1
            xsize 300 ysize 300
            for i in heartinventory:
                if isinstance(i, Weapon):
                    imagebutton:
                        idle i.img
                        action If(player.weapon != None, true = AddToSet(heartinventory, player.weapon)), Function(i.equip, player), RemoveFromSet(heartinventory, i)




label ending:

    "The end."


Attachments
weapon_sword.png
weapon_sword.png (1.38 KiB) Viewed 435 times
weapon_saber.png
weapon_saber.png (1.39 KiB) Viewed 435 times
equipment_weapon.png
equipment_weapon.png (507 Bytes) Viewed 435 times
Last edited by zant19 on Fri Aug 26, 2022 3:15 pm, edited 1 time in total.

User avatar
m_from_space
Miko-Class Veteran
Posts: 978
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: multiple of the same item in inventory giving problems

#2 Post by m_from_space »

I didn't test anything, but just found a typo in your code:

Code: Select all

 class Equipable(InvItem):
        def __init__(self, name, cost, img):
            InvItem.__init__(self, name, cost, img)

        def equip(self, target):
            self.is_equipped = True
            self.equipped_to = target

        def unequip(self, target):
            self.is_equipper = False <------ TYPO "is_equipper" instead of "is_equipped"
            self.equipped_to = None

zant19
Newbie
Posts: 6
Joined: Sun Dec 21, 2014 6:33 am
Contact:

Re: multiple of the same item in inventory giving problems

#3 Post by zant19 »

thanks for finding that :) I updated it but still the same thing.

laure44
Regular
Posts: 84
Joined: Mon Mar 08, 2021 10:55 pm
Projects: Arkan'sTower, Gemshine Lorelei!
Location: France
Contact:

Re: multiple of the same item in inventory giving problems

#4 Post by laure44 »

It seems the issue comes from the fact that the two swords are basically the same object. Making a copy of sword would do the trick because it would be a whole other object.

Code: Select all

$ heartinventory.append(copy.deepcopy(sword))
# OR
$ heartinventory.append(Weapon("Sword", 1, "weapon_sword.png", 1, "sword"))
Didn't test this with your game but I remember having a similar issue with mine quite some time ago, and this is the way I resolved it. I'm no python expert though so maybe there's a better way.

zant19
Newbie
Posts: 6
Joined: Sun Dec 21, 2014 6:33 am
Contact:

Re: multiple of the same item in inventory giving problems

#5 Post by zant19 »

thanks for the help laure44 !

Code: Select all

$ heartinventory.append(Weapon("Sword", 1, "weapon_sword.png", 1, "sword"))
worked right away

I had to mess around to get

Code: Select all

$ heartinventory.append(copy.deepcopy(sword))
to work (got to import copy at the beginning of python block) but it definitely better looking then the other way.
(after finally learning classes and making them so pretty I can leave them where I put them)

laure44
Regular
Posts: 84
Joined: Mon Mar 08, 2021 10:55 pm
Projects: Arkan'sTower, Gemshine Lorelei!
Location: France
Contact:

Re: multiple of the same item in inventory giving problems

#6 Post by laure44 »

zant19 wrote: Fri Aug 26, 2022 2:41 pm I had to mess around to get

Code: Select all

$ heartinventory.append(copy.deepcopy(sword))
to work (got to import copy at the beginning of python block) but it definitely better looking then the other way.
Right, I forgot about the import copy part, sorry about that :oops:

Glad it helped!

zant19
Newbie
Posts: 6
Joined: Sun Dec 21, 2014 6:33 am
Contact:

Re: multiple of the same item in inventory giving problems

#7 Post by zant19 »

it wasn't anything a little help google sensei couldn't solve.

Post Reply

Who is online

Users browsing this forum: Houseofmine