No attribute "name" error in inventory[SOLVED]

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
kedta35
Regular
Posts: 45
Joined: Wed Aug 30, 2023 1:31 pm
Contact:

No attribute "name" error in inventory[SOLVED]

#1 Post by kedta35 »

I'm trying to make it so that I have one inventory for each character, here's my code:

Code: Select all

init python:

    # Inventory 1

    class InventoryFio():
        def __init__(self, items, no_of_items):
            self.items = items
            self.no_of_items = no_of_items

        def add_item(self, item):
            self.items.append(item)
            self.no_of_items += 1

        def remove_item(self, item):
            self.items.remove(item)
            self.no_of_items -= 1

        def add_amount_bought(self, item, amount):
            self.items.append(item)
            self.no_of_items += amount

        def list_items(self):
            if len(self.items) < 1:
                ("0 Items")
            else:
                for item in self.items:
                    (f"{item.name} - {item.description}")

    # Inventory 2

    class InventoryBren():
        def __init__(self, items, no_of_items):
            self.items = items
            self.no_of_items = no_of_items

        def add_item(self, item):
            self.items.append(item)
            self.no_of_items += 1

        def remove_item(self, item):
            self.items.remove(item)
            self.no_of_items -= 1    

        def add_amount_bought(self, item, amount):
            self.items.append(item)
            self.no_of_items += amount    
    
        def list_items(self):
            if len(self.items) < 1:
                ("0 Items")
            else:
                for item in self.items:
                    (f"{item.name} - {item.description}")


    class InventoryItem():
        def __init__(self, name, description):
            self.name = name
            self.description = description
Here's the items and how I add items to the inventories:

Code: Select all

default inventoryfio = InventoryFio([], 0)
default inventorybren = InventoryBren([], 0)

define goldc = InventoryItem("Gold Coins", "Currency")
define stick = InventoryItem("Stick", "A stick")

Code: Select all

label inv_check:

    "Adding 783 Gold Coins..."
    $ inventoryfio.add_amount_bought("goldc", 783)
    "Now I have this much..."
    $ inventoryfio.list_items()
But then I get an error saying "AttributeError: 'str' object has no attribute 'name'"

Can anyone help please, thanks in advance.
Last edited by kedta35 on Sun Feb 18, 2024 3:01 pm, edited 1 time in total.

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: No attribute "name" error in inventory

#2 Post by philat »

You're adding "goldc", a string, not goldc, the item.

User avatar
m_from_space
Miko-Class Veteran
Posts: 975
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: No attribute "name" error in inventory

#3 Post by m_from_space »

kedta35 wrote: Sun Feb 11, 2024 11:48 pm I'm trying to make it so that I have one inventory for each character, here's my code:
On a important sidenote, it's a bit weird to create two similar classes for that. The point of classes is that you derive objects from them, so you only need one Inventory class and then create two characters-related inventory objects from them. I also would suggest using something like an "InventoryStack" class if you want to count items like goldcoins; or you make the count part of the item class. Otherwise if your character has 100 gold coins and 10 sticks in their inventory, how many items do they have? 2? 110? (I think nobody cares about 110 in this case, but your class counts exactly that.)

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]