I'm not very experienced, and I have a lovely working inventory that I've built in renpy/python. I have run into some struggles when trying to make my items stackable. I have been trying to get my class item object to have an editable variable/integer I'm not sure if it's a syntax I'm looking for, or if I need to arrange my classes differently, and if so, how? Previously, the inventory would just add an identical item (and icon) into my display screen. This is what I want to display in my designated text:
class item(object):
def __init__(self, name, img, himg, THIS THING):
self.name = name
self.img = img
self.himg = himg
self.THIS THING = THIS THING
The closest I have come without getting a 'name is not defined' error despite the name clearly being defined or some kind of garbled "%item.amountd" text appearing in my field instead is this:
label start:
show screen inventory
pause
"adding another item"
$ backpack.add_item(popcorn)
jump start
return
default popcorn = item("popcorn", "inventory/ragepop_idle.png", "inventory/ragepop_hover.png", 0)
default money = 50
default backpack = container()
default inv = []
######screen
screen inventory():
zorder 2
add "images/inventory/inventorybg.png"
modal True
text "Money: [money]0" xpos 310 ypos 60 color("#FFFFFF")
hbox:
xpos 298 ypos 113
spacing 7
xmaximum 695
box_wrap True
for thing in backpack.inv:
imagebutton idle thing.item.img hover thing.item.himg action Null
text "{b}[thing.item.amt]{/b}" size 20 color("#FFFFFF") xoffset -30 yoffset 5
imagebutton:
idle "inventory/invpois.png"
hover "inventory/invpois2.png"
focus_mask True
action Hide("inventory")
##amt is what I settled on for the THIS THING
init python:
class item(object):
def __init__(self, name, img, himg, amt):
self.name = name
self.img = img
self.himg = himg
self.amt = amt
class invitem(object):
def __init__(self, item, amount):
self.item = item
self.amount = amount
class container(object):
def __init__(self):
self.inv = []
## Disregard these add/remove methods as I have to change these once I figure out how to $ amt+=1
##As they stand now, they serve to put the icon in the inventory the old unstackable way.
def add_item(self, item, amount=1):
self.inv.append(invitem(item, amount))
return("success")
def has_item(self, item, amount=1):
if item in [i.item for i in self.inv]:
if self.finditem(item).amount >= amount:
return(self.finditem(item).amount)
else: return(False)
else:
return(False)
def finditem(self, item):
return(self.inv[[i.item for i in self.inv].index(item)])
def remove_item(self, item, amount=1):
if self.has_item(item):
self.finditem(item).amount -= amount
if self.finditem(item).amount <= 0:
self.inv.pop(self.inv.index(self.finditem(item)))
return ("gone")
else:
return ("more left")
else:
return ("not found")
When I run this, the inventory display screen is first empty, and then the item appears in there like this: example run
Unfortunately, that 0 would be anything I put in there and I can't figure out how to add or subtract to ''amt'' during game play either by calling the item or trying to get the item to call an easily edited variable and display that as it's ''amt''.
This is the part where I'm supposed to tell you what all I've tried. This is tough for me, because I've tried so many variations of so many different things that I can't get into specifics of exactly what was typed. Essentially I played around with all the variations of interpolating the amt I could find through the documentation and other peoples' forum results and trying to make it work with my items, and mostly got a bunch of "class item doesn't have attribute amt" and "name amt is not defined". After that failed, i tried to convince my item to get its amt from a completely separate variable. I have successfully and easily used interpolated text to display the money on that inventory screen. it just asks for "text "money: [money].0" and changes easily during gameplay with $ money 1+10, etc. I just haven't been able to convince it to work properly if the interpolated information I'm trying to get from it is in an item class.
Thanks in advance for any ideas you may have about what i'm doing wrong!