[SOLVED] Help with Inventory items and money

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
leoxhurt
Regular
Posts: 34
Joined: Fri Aug 04, 2017 6:25 pm
Contact:

[SOLVED] Help with Inventory items and money

#1 Post by leoxhurt »

i was trying to set a inventory and money system to my novel, but when i try to put in a store actual items and actual money like dropped from the choices or battles not appear, to show the actual current money and inventory has a specific code?
Last edited by leoxhurt on Sat Aug 19, 2017 7:41 pm, edited 1 time in total.

TheChatotMaestro
Regular
Posts: 91
Joined: Mon Jul 31, 2017 8:33 am
Deviantart: LedianWithACamera
Contact:

Re: Help with Inventory items and money

#2 Post by TheChatotMaestro »

What is your problem? I can't tell what you're asking.

leoxhurt
Regular
Posts: 34
Joined: Fri Aug 04, 2017 6:25 pm
Contact:

Re: Help with Inventory items and money

#3 Post by leoxhurt »

TheChatotMaestro wrote: Thu Aug 17, 2017 9:18 pm What is your problem? I can't tell what you're asking.
i want to know about inventory and money systems, i have one of them on my novel, but when i try to add a shop when i access the shop, my inventory and money appear empty, it was a specific code to show the current money and itens in inventory?

PetrolJunk
Newbie
Posts: 4
Joined: Fri Aug 18, 2017 10:03 am
Contact:

Re: Help with Inventory items and money

#4 Post by PetrolJunk »

I'm sorry Leoxhurt, I too am having trouble understanding what it is that you are asking. I though maybe you needed a link to tutorials on coding a money and inventory system, but then i read that you already have a system in you novel. (at least that's what i think I'm reading.). Then i thought maybe you are having some problems with your code, but you didn't provide any for us to review so i don't think that is it either. Could you rephrase the question? Maybe that might help.

leoxhurt
Regular
Posts: 34
Joined: Fri Aug 04, 2017 6:25 pm
Contact:

Re: Help with Inventory items and money

#5 Post by leoxhurt »

PetrolJunk wrote: Fri Aug 18, 2017 2:19 pm I'm sorry Leoxhurt, I too am having trouble understanding what it is that you are asking. I though maybe you needed a link to tutorials on coding a money and inventory system, but then i read that you already have a system in you novel. (at least that's what i think I'm reading.). Then i thought maybe you are having some problems with your code, but you didn't provide any for us to review so i don't think that is it either. Could you rephrase the question? Maybe that might help.

Code: Select all


##Ren'Py Inventory System v. 1.5 provided under public domain by saguaro

init python: 
    import renpy.store as store    
    
    class Item(store.object):
        def __init__(self, name, desc, icon=False, value=0, act=Show("inventory_popup", message="Nothing happened!"), type="item", recipe=False):
            global cookbook
            self.name = name
            self.desc = desc
            self.icon = icon
            self.value = value               
            self.act = act # screen action
            self.type = type # type of item
            self.recipe = recipe # nested list of [ingredient, qty]   
            
            if recipe:
                cookbook.append(self)
                cookbook.sort(key=lambda i: i.name) #alpha order

        def change(self, name, desc=False, icon=False, value=False, act=False, recipe=False): 
            self.name = name
            if desc:
                self.desc = desc
            if icon:
                self.icon = icon
            if value:
                self.value = value   
            if act:
                self.act = act
            if recipe:
                self.recipe = recipe                
            
    class Inventory(store.object):
        def __init__(self, name, money=0, barter=100):
            self.name = name
            self.money = money
            self.barter = barter #percentage of value paid for items            
            self.inv = []  # items stored in nested list [item object, qty]
            self.sort_by = self.sort_name
            self.sort_order = True #ascending, descending
            self.grid_view = True
            
        def buy(self, item, price):            
            self.deposit(price)            
            self.take(item[0])                

        def check(self, item):
            if self.qty(item):
                for i in self.inv:
                    if i[0] == item:        
                        return self.inv.index(i) # returns item index (location)            
            
        def check_recipe(self, item): # verify all ingredients are in inv
            checklist = list()
            for i in item.recipe:
                if self.qty(i[0]) >= i[1]:
                    checklist.append(True)
            if len(checklist) == len(item.recipe):
                return True
            else:
                return False        
                
        def craft(self, item):
            for line in item.recipe:
                self.drop(line[0], line[1])
            self.take(item)  
            message = "Crafted a %s!" % (item.name)
            renpy.show_screen("inventory_popup", message=message)   
                            
        def deposit(self, amount):
            self.money -= amount   
                            
        def drop(self, item, qty=1):
            if self.qty(item):
                item_location = self.check(item)
                if self.inv[item_location][1] > qty:
                    self.inv[item_location][1] -= qty
                else:
                    del self.inv[item_location]                      
                            
        def qty(self, item):
            for i in self.inv:
                if i[0] == item:   
                    return i[1] # returns quantity   
                    
        def sell(self, item, price):
            self.withdraw(price)
            self.drop(item[0])
            
        def sort_name(self):
            self.inv.sort(key=lambda i: i[0].name, reverse=self.sort_order)
            
        def sort_qty(self):
            self.inv.sort(key=lambda i: i[1], reverse=self.sort_order)
                      
        def sort_value(self):
            self.inv.sort(key=lambda i: i[0].value, reverse=self.sort_order)
           
        def take(self, item, qty=1):
            if self.qty(item):
                item_location = self.check(item)            
                self.inv[item_location][1] += qty                  
            else:
                self.inv.append([item,qty])  

        def withdraw(self, amount):
            self.money += amount
            
    def calculate_price(item, buyer):
        if buyer:
            price = item[0].value * (buyer.barter * 0.01)
            return int(price)
        
    def money_transfer(depositor, withdrawer, amount):
        if depositor.money >= amount:
            depositor.deposit(amount)
            withdrawer.withdraw(amount) 
        else:
            message = "Sorry, %s doesn't have %d!" % (buyer.name, amount) 
            renpy.show_screen("inventory_popup", message=message) 

    def trade(seller, buyer, item):
        seller.drop(item[0])
        buyer.take(item[0])              
        
    def transaction(seller, buyer, item):
        price = calculate_price(item, buyer)
        if buyer.money >= price:   
            seller.sell(item, price)
            buyer.buy(item, price)
        else:
            message = "Sorry, %s doesn't have enough money!" % (buyer.name)
            renpy.show_screen("inventory_popup", message=message)

    transfer_amount = 0
          
screen tooltip(item=False,seller=false):      
    if item:
        hbox:
            xalign 0.5 yalign 1.0
            if seller:
                text ("[item[0].name]: [item[0].desc] (Sell Value: " + str(calculate_price(item, seller)) + ")")
            else:
                text "[item[0].name]: [item[0].desc] (Value: [item[0].value])" 
                
  
now the problem is, i setted the money in the start label at :

Code: Select all

$ e_inv.money = 100
and for the inventory i setted in the start label at:

Code: Select all

  $ cookbook = list()
    $ e_inv = Inventory("Kaishi")
    $ eye = Item(name="Eyeball", desc="A human eyeball, how creepy!", icon="images/eye.png", value=250)
    $ but = Item("Button", "A shiny button", "images/button.png", 100, act=Show("inventory_popup", message="This item is only used in crafting"))
    $ e_inv.take(eye)
    $ e_inv.take(but,2)

when i try to create a label with a shop the money and items desapear

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Help with Inventory items and money

#6 Post by Remix »

I would suggest just adding a debug screen to check the python object you are playing with and visually see if each line is doing what you expect...

Code: Select all

screen track_data( obj ):
    vbox:
        # all attributes of the object that are public and not methods...
        $ d = ["{0}={1}\n".format(k, getattr(obj, k)).replace("[","[[").replace("{","{{") for k in dir(obj) if k[0] != "_" and not callable( getattr(obj, k) ) ]
        text "Obj: {size=-5}[d]{/size}"
    timer 0.02 action (renpy.restart_interaction) repeat True

label start:
    "Before anything"  
    $ cookbook = list()
    $ e_inv = Inventory("Kaishi")
    show screen track_data( e_inv )
    $ eye = Item(name="Eyeball", desc="A human eyeball, how creepy!", icon="images/eye.png", value=250)
    "eye"
    $ but = Item("Button", "A shiny button", "images/button.png", 100, act=Show("inventory_popup", message="This item is only used in crafting"))
    "button"
    $ e_inv.take(eye)
    "take eye"
    $ e_inv.take(but,2)
    "take button"
Check whether money changes, whether items are added etc
Frameworks & Scriptlets:

leoxhurt
Regular
Posts: 34
Joined: Fri Aug 04, 2017 6:25 pm
Contact:

Re: Help with Inventory items and money

#7 Post by leoxhurt »

Remix wrote: Fri Aug 18, 2017 8:11 pm I would suggest just adding a debug screen to check the python object you are playing with and visually see if each line is doing what you expect...

Code: Select all

screen track_data( obj ):
    vbox:
        # all attributes of the object that are public and not methods...
        $ d = ["{0}={1}\n".format(k, getattr(obj, k)).replace("[","[[").replace("{","{{") for k in dir(obj) if k[0] != "_" and not callable( getattr(obj, k) ) ]
        text "Obj: {size=-5}[d]{/size}"
    timer 0.02 action (renpy.restart_interaction) repeat True

label start:
    "Before anything"  
    $ cookbook = list()
    $ e_inv = Inventory("Kaishi")
    show screen track_data( e_inv )
    $ eye = Item(name="Eyeball", desc="A human eyeball, how creepy!", icon="images/eye.png", value=250)
    "eye"
    $ but = Item("Button", "A shiny button", "images/button.png", 100, act=Show("inventory_popup", message="This item is only used in crafting"))
    "button"
    $ e_inv.take(eye)
    "take eye"
    $ e_inv.take(but,2)
    "take button"
Check whether money changes, whether items are added etc
I discovered a interesting thing, the money and the items appear at the tracker, but not in the inventory so can i assume that is a error in the code?

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Help with Inventory items and money

#8 Post by Remix »

Presumably so yes.
If the object holds the correct values as you step through each line, the error is likely in your screen to display the inventory.

If you cannot track it down yourself, we'd need to see that screen code to offer any help
Frameworks & Scriptlets:

leoxhurt
Regular
Posts: 34
Joined: Fri Aug 04, 2017 6:25 pm
Contact:

Re: Help with Inventory items and money

#9 Post by leoxhurt »

Remix wrote: Sat Aug 19, 2017 5:00 pm Presumably so yes.
If the object holds the correct values as you step through each line, the error is likely in your screen to display the inventory.

If you cannot track it down yourself, we'd need to see that screen code to offer any help
i finnaly found the error the inventory was listed 2 times, and i removed the second time and problem solved really thanks for your help

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]