Renpy Inventory System Class Errors

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
User avatar
margaretcatter
Regular
Posts: 44
Joined: Fri May 18, 2018 7:16 pm
Projects: Kathelm Princess X Princess | Hurricane Like Me
itch: margaretcatter
Location: New York/LA
Contact:

Renpy Inventory System Class Errors

#1 Post by margaretcatter »

So I'm working off of this cookbook instructional viewtopic.php?f=51&t=44730 I'm disregarding things about weight because weight doesnt factor into my game.

Code: Select all

TypeError: __init__() takes exactly 1 argument (2 given)
which I know is usually because of the wrong number of arguments in a class, my classes look like this

Code: Select all

init python:
    class Item(object):
        def __init__(self, name):
            self.name = name
    class Inv_Item(object):
        def __init__(self, item, amount):
            self.item = item
            self.amount = amount
    class Container(object):
        def __init__(self):
            self.inventory = []
        def has_item(self, item, amount=1):
            if item in [i.item for i in self.inventory]:
                if self.inventory[[i.item for i in self.inventory].index(item)].amount >= amount:
                    return(self.inventory[[i.item for i in self.inventory].index(item)].amount)
                else:
                    return(False)
        def find_item(self, item):
            return(self.inventory[[i.item for i in self.inventory].index(item)])
        def add_item(self, item, amount=1):
            self.inventory.append(InvItem(item, amount))
        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)))
I thought it was because I wrote

Code: Select all

class Container(object):
        def __init__(self): 

and not

Code: Select all

class Container(object):
        def __init__(self, inventory):

but when I add inventory to the class I get

Code: Select all

While running game code:
  File "game/script.rpy", line 45, in script
    python:
  File "game/script.rpy", line 49, in <module>
    workbag.add_item("Phone")
  File "game/script.rpy", line 35, in add_item
    self.inventory.append(InvItem(item, amount))
NameError: global name 'InvItem' is not defined

which i dont understand or know how to fix.

Saithir
Newbie
Posts: 11
Joined: Mon Jun 25, 2018 4:27 pm
Contact:

Re: Renpy Inventory System Class Errors

#2 Post by Saithir »

NameError: global name 'InvItem' is not defined
Well, it's not. (seems code blocks can't bold things, but you're missing the underscore when you're using the InvItem in the method)

Code: Select all

class Inv_Item(object):

Code: Select all

        def add_item(self, item, amount=1):
            self.inventory.append(InvItem(item, amount))
After fixing that your code seems to work without the __init__ TypeError mentioned.

User avatar
margaretcatter
Regular
Posts: 44
Joined: Fri May 18, 2018 7:16 pm
Projects: Kathelm Princess X Princess | Hurricane Like Me
itch: margaretcatter
Location: New York/LA
Contact:

Re: Renpy Inventory System Class Errors

#3 Post by margaretcatter »

jfc this is why I shouldnt do things past midnight. Thanks

User avatar
margaretcatter
Regular
Posts: 44
Joined: Fri May 18, 2018 7:16 pm
Projects: Kathelm Princess X Princess | Hurricane Like Me
itch: margaretcatter
Location: New York/LA
Contact:

Re: Renpy Inventory System Class Errors

#4 Post by margaretcatter »

Follow up now that its the light of day and I'm not half sleep. This again is my inventory code now with correct underscores.

Code: Select all

init -1 python:
    class Item(object):
        def __init__(self, name, amount=0):
            self.name = name
            self.amount = amount
    class Inv_item(object):
        def __init__(self, item, amount=0):
            self.item = item
            self.amount = amount
    class Container(object):
        def __init__(self):
            self.inventory = ()
        def has_item(self, item, amount=1):
            if item in [i.item for i in self.inventory]:
                if self.inventory[[i.item for i in self.inventory].index(item)].amount >= amount:
                    return(self.inventory[[i.item for i in self.inventory].index(item)].amount)
                else:
                    return(False)
        def find_item(self, item):
            return(self.inventory[[i.item for i in self.inventory].index(item)])
        def add_item(self, item, amount=1):
            self.inventory.append(Inv_item(item, amount))
        def remove_item(self, item, amount=1):
            if self.has_item(item):
                self.find_item(item).amount -= amount
            if self.find_item(item).amount <= 0:
                self.inventory.pop(self.inventory.index(self.find_item(item)))

default workbag = Container()
default phone = Item("Phone", 1)
And this is my screen code

Code: Select all

screen invdisplay:
        vbox:
            align (0.0, 0.0)
            text "Inventory:"
            for Item in workbag:
                text ("[item.name]")
And when I launch my game I get

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 28, in script
    tc "So this is they story of me, Troy Colby, and the time I tried to do the right thing and still got stuck literally holding the bag."
  File "game/screens.rpy", line 1518, in execute
    screen invdisplay:
  File "game/screens.rpy", line 1518, in execute
    screen invdisplay:
  File "game/screens.rpy", line 1519, in execute
    vbox:
TypeError: 'Container' object is not iterable

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 28, in script
    tc "So this is they story of me, Troy Colby, and the time I tried to do the right thing and still got stuck literally holding the bag."
  File "/Applications/renpy-6.99.14.3-sdk/renpy/ast.py", line 652, in execute
    renpy.exports.say(who, what, interact=self.interact, *args, **kwargs)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/exports.py", line 1199, in say
    who(what, *args, **kwargs)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/character.py", line 1016, in __call__
    self.do_display(who, what, cb_args=self.cb_args, **display_args)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/character.py", line 817, in do_display
    **display_args)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/character.py", line 566, in display_say
    rv = renpy.ui.interact(mouse='say', type=type, roll_forward=roll_forward)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/ui.py", line 287, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/core.py", line 2649, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/core.py", line 3033, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/core.py", line 511, in visit_all
    d.visit_all(callback)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/core.py", line 511, in visit_all
    d.visit_all(callback)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/core.py", line 511, in visit_all
    d.visit_all(callback)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/screen.py", line 424, in visit_all
    callback(self)
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/core.py", line 3033, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/screen.py", line 434, in per_interact
    self.update()
  File "/Applications/renpy-6.99.14.3-sdk/renpy/display/screen.py", line 619, in update
    self.screen.function(**self.scope)
  File "game/screens.rpy", line 1518, in execute
    screen invdisplay:
  File "game/screens.rpy", line 1518, in execute
    screen invdisplay:
  File "game/screens.rpy", line 1519, in execute
    vbox:
  File "/Applications/renpy-6.99.14.3-sdk/renpy/sl2/slast.py", line 1428, in execute
    for index, v in enumerate(value):
TypeError: 'Container' object is not iterable

Darwin-16.6.0-x86_64-i386-64bit
Ren'Py 7.0.0.196
Hurricane Like Me 0.1
Wed Jul  4 13:27:04 2018

When I take out the screen code it works perfectly (as in no errors), I can add items to my inventory and everything with no problem. I just can't see what I'm adding which is frustrating.

Saithir
Newbie
Posts: 11
Joined: Mon Jun 25, 2018 4:27 pm
Contact:

Re: Renpy Inventory System Class Errors

#5 Post by Saithir »

And again python is right ;)

"TypeError: 'Container' object is not iterable" because it's not.

Code: Select all

for item in workbag
Won't work without making some weird meta methods in the python class (can't help with that as I'm not that good yet), but you can make it work this way:

Code: Select all

for item in workbag.inventory
However:

Code: Select all

self.inventory = ()
It's a tuple (pair of values) or whatever and you probably want to use a list instead:

Code: Select all

self.inventory = []
It was right the first time you pasted that code, so I would suggest coffee or tea ;)

User avatar
margaretcatter
Regular
Posts: 44
Joined: Fri May 18, 2018 7:16 pm
Projects: Kathelm Princess X Princess | Hurricane Like Me
itch: margaretcatter
Location: New York/LA
Contact:

Re: Renpy Inventory System Class Errors

#6 Post by margaretcatter »

Can I blame it on the excessive heat wave I'm living though?

Thanks so much!

Saithir
Newbie
Posts: 11
Joined: Mon Jun 25, 2018 4:27 pm
Contact:

Re: Renpy Inventory System Class Errors

#7 Post by Saithir »

Yes, heat waves, broken coffee machines, cats stepping on the keyboard and deleting parts of code, and other acts of God, are all perfectly fine professional programmer excuses :D

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]