Page 1 of 1

Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 9:11 am
by Wertous
Hello!
I am a new person in this matter
I am trying to integrate two inventory systems into one - since in each of them there is not what is in the other

So my goal is a displayed inventory list with the ability to put on and take off things from the character, such as weapons, armor and other things with a change in the character’s characteristics

Here is my code

Code: Select all

сharacter.rpy
init python: 

    class Player:
        def __init__(self, hp, mp, atk, defense, mdef,level=1):
            self.hp = hp
            self.mp = mp
            self.max_hp = hp
            self.max_mp = mp
            self.atk = atk
            self.defense =defense
            self.mdef =mdef
            self.level = level
            self.weapon = None
            self.armor = {"head": None, "chest": None, "acc" : None,"shield":None}


        def addHP(self,amount):
            self.hp += amount
            if self.hp > self.max_hp:
                self.hp = self.max_hp



        def addMP(self,amount):
            self.mp += amount
            if self.mp > self.max_mp:
                self.mp = self.max_mp

        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


        def equip_armor(self,armor, slot):
            if self.armor[slot] != None:
                self.unequip_armor(slot)
            self.armor[slot] = armor
            self.defense += armor.defense
            self.mdef += armor.mdef


        def unequip_armor(self,slot):
            if self.armor[slot] != None:
                self.defense -= self.armor[slot].defense
                self.mdef -= self.armor[slot].mdef
                self.armor[slot] = None
                

Re: Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 9:13 am
by Wertous

Code: Select all


inventory.rpy

init python: 

    class InventoryItem:
        def __init__(self, img, value):
            self.img = img
            self.value = value


    class Consumable(InventoryItem):
        def __init__(self,img,value,hp_gain,mp_gain):
            InventoryItem.__init__(self,img,value)
            self.hp_gain = hp_gain
            self.mp_gain = mp_gain

        def use(self, target):
            inventory.remove(self)
            target.addHP(self.hp_gain)
            target.addMP(self.mp_gain)
            global selected_item
            selected_item = None


    class Equipable(InventoryItem):
        def __init__(self, img, value):
            InventoryItem.__init__(self, img, value)
            self.is_equipped = False
            self.equipped_to = None


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

        def unequip(self):
            self.is_equipped = False
            self.equipped_to = None



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


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

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


    class Armor(Equipable):
        def __init__(self, img, value, defense, mdef,slot):
            Equipable.__init__(self, img, value)
            self.defense = defense
            self.mdef =mdef
            self.slot = slot


        def equip(self,target):
            Equipable.equip(self, target)
            target.equip_armor(self, self.slot)

        def unequip(self):
            self.equipped_to.unequip_armor(self.slot)
            Equipable.unequip(self)


    class KeyItem(InventoryItem):
        def __init__(self, img):
            InventoryItem.__init__(self, img, 0)
            
 

Re: Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 9:14 am
by Wertous

Code: Select all



inventory_screen.rpy


style slot:
        background Frame("square",0,0)
        minimum(80,80)
        maximum(80,80)
        xalign 0.5
        
screen view_nav(inventory):
    hbox:
        text "View: "
        textbutton "Grid" action SetField(inventory, "grid_view", True)        
        textbutton "list" action SetField(inventory, "grid_view", False)     
                
screen sort_nav(inventory):
    hbox:
        text "Sort by: "         
        textbutton "Name" action [ToggleField(inventory, "sort_by", inventory.sort_name), inventory.sort_name]
        textbutton "Quntity" action [ToggleField(inventory, "sort_by", inventory.sort_qty), inventory.sort_qty]
        textbutton "Price" action [ToggleField(inventory, "sort_by", inventory.sort_value), inventory.sort_value]
        if inventory.sort_order:
            textbutton "asc." action [ToggleField(inventory, "sort_order"), inventory.sort_by]
        else:
            textbutton "des." action [ToggleField(inventory, "sort_order"), inventory.sort_by]            

screen inventory_popup(message):
    zorder 100
    frame:
        style_group "invstyle"
        hbox:
            text message
    timer 1.5 action Hide("inventory_popup")







screen inventory_screen_1(first_inventory):
    
    add "images/invent1.jpg"
    
 


    frame:
        style "slot"
        xpos 175 ypos 400
        if pc.weapon != None:
            add pc.weapon.img
        else:
            label "weapon" xalign 0.5 yalign 0.5 text_size 15








    
    frame:
        style_group "invstyle"          
        hbox:
            spacing 25
            vbox:
                      
               
                use inventory_view_1(first_inventory,)                          
                use view_nav(first_inventory)
                use sort_nav(first_inventory)
               
                textbutton "Закрыть" action Return()
           
                

screen inventory_view_1(inventory):     
    side "c r":
        style_group "invstyle"
        area (0, 0, 500, 400) xpos 50
        vpgrid id ("vp"+inventory.name):
            draggable True   
            mousewheel True
            xsize 500 ysize 400
            if inventory.grid_view:
                cols 4 spacing 5
            else:
                cols 1 spacing 25
            for item in inventory.inv:
                
               
                $ value = item[0].value
                $ qty = str(item[1])
                hbox:
                    if item[0].img:
                        $ img = item[0].img
                        $ hover_img = im.Sepia(img)
                        imagebutton:
                            idle LiveComposite((80,80), (0,0), img, (0,0), Text(qty))
                          
                            action [SetVariable("selected_item", item),Show("question_screen")]
                          
                    
                    
        
            if len(inventory.inv) == 0:
                add Null(height=100,width=100)
                                    
        vbar value YScrollValue("vp"+inventory.name)
        
        
        
screen question_screen :
    frame:
         minimum(300,300)
         maximum(300,300)
         background "#100b"
         xalign 0.5 yalign 0.5
         text "[selected_item]"
        
         textbutton "Equip"  action Function( Player.equip_weapon,selected_item)
        
  
        
init -2: 

 
    style invstyle_frame:
        xpos 610
        ypos 30
        background "#0018"
        
    style invstyle_label_text:
        size 40
        
    style invstyle_label:
        xalign 0.5
        
 

Re: Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 9:15 am
by Wertous

Code: Select all



screept.rpy

define player = Character("Rufus",color="#c8c8ff")
default pc = Player(15,10,5,3,2,1)
default sword = Weapon("sword", 10, 5, "sword")
default selected_item= None
default Player_inv = Inventory("")




      
label start:  
    
    
    
#$ Player_inv = Inventory("")
$ Player_inv.take(sword) 
$ Player_inv.take(sword)


Re: Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 9:21 am
by Wertous
In general and in general, the system works fine but there is one problem - namely, equip does not work through the button
received error
While running game code:
unbound method equip_weapon () must be called with Player instance as first argument (got revertable list instance instead

Re: Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 9:29 am
by Wertous
Or if on list inventory screen. rpy on line 122 I change to

Code: Select all

textbutton "Equip"  action Function( selected_item.equip,pc)
which is typical for the first system

I get an error

RevertableList has no attribute equip


In general, I tried different options to declare this function - but they all lead to an error, but the code
in screept.rpy

Code: Select all

$ pc. equip_weapon (sword)
puts a sword on the player

As far as I can judge, the problem is that somehow the image of the items and the inventory is displayed differently and does not have the form for which the teams from the first inventory option work
Although maybe I'm wrong


I would be grateful for a hint on how to avoid this.

Re: Attempt to integrate two inventory systems

Posted: Fri Aug 09, 2019 10:47 am
by Wertous
If I change the cod of the button to

Code: Select all

 textbutton "Equip"  action Function( Weapon.equip,selected_item,pc)
I get an error
unbound method equip() must be called with Weapon instance as first argument ( got revertable List instace instead)

as far as I understand the problem is that the click handler does not have the correct information about the item in the inventory
How can this be fixed?