- Dec 8, 2017
- 739
- 2,469
Hello guys, i have a little problem with my inventory system.
I recently started using classes rather than a dictionary so it's easier for updating the game. Everything works great, saving works, except for one thing: rollback.
When i remove an item from the inventory, then rollback to before the item was removed, the item is still missing.
Example: The mc needs flowers for his first date with his new GF. The mc has one flower bouquet in his inventory. After the choice to go on the date is selected, the flowers are correctly removed from the inventory. But when i rollback before the choice is made, the number of flowers is still at 0, they are not "put back" in the inventory. Note that the money, wich is a simple variable is working and the amount used on the date is "refunded".
This is my inventory system (lifted from the cookbook and slightly modified.):
And this is the menu choice removing the item from the class
I tried to use to "default" the classes before the start label but it doesn't work. And my google-fu is not strong enough to find a solution. Would any of you ge**could help me? It would be much appreciated.
I recently started using classes rather than a dictionary so it's easier for updating the game. Everything works great, saving works, except for one thing: rollback.
When i remove an item from the inventory, then rollback to before the item was removed, the item is still missing.
Example: The mc needs flowers for his first date with his new GF. The mc has one flower bouquet in his inventory. After the choice to go on the date is selected, the flowers are correctly removed from the inventory. But when i rollback before the choice is made, the number of flowers is still at 0, they are not "put back" in the inventory. Note that the money, wich is a simple variable is working and the amount used on the date is "refunded".
This is my inventory system (lifted from the cookbook and slightly modified.):
Code:
init python:
class Item(object):
def __init__(self, name):
self.name = name
class InvItem(object):
def __init__(self, item, amount):
self.item = item
self.amount = amount
class Container(object):
def __init__(self):
self.inventory = []
def add_item(self, item, amount=1):
if item in [i.item for i in self.inventory]: # I can't believe I got this line to work with my first try
self.finditem(item).amount += amount # oh god why
else:
self.inventory.append(InvItem(item, amount))
return('success')
def has_item(self, item, amount=1):
if item in [i.item for i in self.inventory]:
if self.finditem(item).amount >= amount:
return(self.finditem(item).amount)
else:
return(False)
else:
return(False)
def finditem(self, item):
return(self.inventory[[i.item for i in self.inventory].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.inventory.pop(self.inventory.index(self.finditem(item)))
return('gone')
else:
return('more left')
else:
return('not found')
$ backpack = Container()
And this is the menu choice removing the item from the class
Code:
menu:
"{color=FFFF00}Ask her out on a date (-100$){/color}" if backpack.has_item (flowers) >= 1 and elisa_var == 1:
if money_var <= 99:
"I don't have enough money to take her out."
jump elisaroom_night_loop
else:
$backpack.remove_item (flowers)
$money_var -= 100
jump elisalvl1_label
I tried to use to "default" the classes before the start label but it doesn't work. And my google-fu is not strong enough to find a solution. Would any of you ge**could help me? It would be much appreciated.