I'm using Python. I've read a bit about this and can't seem to wrap my mind around it. What I want to do is have a class called Potions with various potion objects in it. For now there's one potion, a simple HealthPotion. I want potions to be stackable in inventories and shop stocks. So I need an instance of the potion amount for an inventory and an instance for each shop stock that carries potions. The potion amount would be dynamic, for buying/selling and looting of potions. If someone could provide a basic explanation or examples that would be great.
Here's a snippet of what I have:
class Potion(Item):
def __init__(self, name, desc, val, amt, type, effect, bound):
Item.__init__(self, name, desc, val, amt, type, effect, bound)
self.name = name
self.desc = desc
self.val = val
self.amt = amt
self.type = 0 #Restorative
self.effect = effect
def use(self, you):
#Use health potion
you.hp_current += self.effect
you.displayStats()
#Format: Name, Description, Value, Amount, Type, Effect, Bound
HealthPotion = Potion('Health Potion', 'Restores 10 hit points when consumed', 10, 0,
0, 10, 0)
Ideally the default amount would be set to 0 and I'd be able to declare how much a certain shop would start with in their stock. The inventory and shop stock is set up as an array that items are appended and removed to/from. I think I've got the logic down for how this would work, I just am having trouble with instancing the amounts.
EDIT: This is part of what I have in a buy method to see what would happen without using instances. It's pretty ugly and I discerned that you.inventory.y.amt will not work. y being the selected item from the list of items that is displayed in a "shop."
x = selection - 1 #Item menu starts at 1. But arrays start at 0. So it takes user input and subtracts 1 to match the array.
y = self.stock[x]
if y.val <= you.coin:
if y.amt == 0:
you.inventory.append(y)
you.inventory.y.amt += 1
else:
you.inventory.y.amt += 1;
you.coin -= y.val
self.funds += y.val
if self.stock.y.amt > 1:
self.stock.y.amt -= 1
else:
self.stock.y.amt -= 1
self.stock.pop(x)
I've looked at examples like this:
class foo:
a = 1
i = foo()
foo.a => 1
i.a => 1
i.a = "inst"
foo.a => 1
i.a => "inst"
I'm wondering if I don't just create a second HealthPotion object, but that doesn't sound right to me. This example leads me to think otherwise. Maybe I just don't understand instancing.
"This would create first object of Employee class"
emp1 = Employee("Zara", 2000)
"This would create second object of Employee class"
emp2 = Employee("Manni", 5000)
Thanks!