[SOLVED] problems appending, "tuple object is not callable" inventory sys

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
Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

[SOLVED] problems appending, "tuple object is not callable" inventory sys

#1 Post by Dr_arell »

so keep in mind that im not looking for a specific answer but more like a "how to", anything that gets me to the goal is valid

hi, ive been trying to do the an inventory/store system, theres is a part where i set a inventory for each character so im able to give them items, but im getting hard stuck. the problem is that i cannot successfully append the item from my inventory to the characters inventory, the characters are made with objects and their inventories are contained inside a object , also have another class for my items so it looks something like this.

Code: Select all

    class Item:
        global items_avaliable
        global store_sections
        items_avaliable = []


        def __init__(self, created_for, cost, imagen, description, category):
            self.created_for = created_for
            self.cost = cost
            self.imagen = im.Scale(imagen, 152, 196)
            self.description = description
            self.category = category
                   
red_sweater_for_gwen = Item("gwen_", 500, "/images/screens/Store/gwen_sweater_red.png", "red_sweater_for_gwen", clothes)                    
       
       
       
    class Character_info:

        def __init__(self, ui_image, ui_color):
            self.ui_image = ui_image
            self.ui_color = ui_color
            self.inventory = []
            
gwen_ = Character_info("/images/screens/Store/gwen_menu.png", "#7A1800")                    
                    
ive tried creating a method inside the item class with getattr() to extract the inventory from gwen_ and then interact with it, looking like this

Code: Select all

    def give_item(self, character):
        getattr(character, "inventory").append(self)
the funny thing is that this usually works but theres one more thing that is breaking my head

im trying to trigger the give_item() as an action for a button

Code: Select all

action Function(getattr(item_being_used, "give_item"), character)
and here i get a hard error "Tuple obejct is not callable" for what ive read theres something messing with the parenthesis in python itself and this is just not an option.

keep in mind that this is for general purposes and not just for the character of gwen_, so any solutions to the main problem are accepted, it doesnt have to be this specific, ty for your time
Last edited by Dr_arell on Thu Jul 01, 2021 5:57 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2398
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: problems appending, "tuple object is not callable" inventory sys

#2 Post by Ocelot »

A full error message would be helpful.

Also, why getattr? There are zero reasons to use it there.
< < insert Rick Cook quote here > >

Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

Re: problems appending, "tuple object is not callable" inventory sys

#3 Post by Dr_arell »

respecting to the error, im not longer having it, it seems i moved things around and now its not popping off, but that doesnt mean its working, python its just ignoring it, but refuses to do what i want, that is appending to a different lists(inventories) depending on what the variable is. i will reply with the code im currently using.

replying to your question, the first getattr() is because im trying to append to different inventories and it is kind of a workaround, the one used at the end its because item being used is the container for a for loop, "from whatever item_being_used is, get its method give_item()"

i cannot append this way because, python thinks im trying to access a item called inventory, instead what i mean is using whats stored inside it
i cannot do this for example:

Code: Select all

default character_var = mike
character_var.append(whatever)
setting the lists(inventories) inside a object i can use gettatr() which allows me to use a variable as first argument on the getattr() function or in other words, .append to the inventory the variable tells it, at least thats what im trying here

Code: Select all

class Character:

        def __init__(self, character_name):
            self.character_name =  character_name
            self.inventory = []

mike = Character("mike")

getattr(character_var, "inventory").append(item_being_used)

Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

Re: problems appending, "tuple object is not callable" inventory sys

#4 Post by Dr_arell »

as i said now its not appending or raising a error, it is just skiping it and moving on with the next code.
the abomination of code im currently using:

Code: Select all

    class Item:

        def __init__(self, created_for, cost, imagen, description, category):
            self.created_for = created_for
            self.cost = cost
            self.imagen = im.Scale(imagen, 152, 196)
            self.description = description
            self.category = category
i space it because the give_item() is the one giving me problems, but this is inside the class item as well. z means any item a for loops spits out, it is just an object from the class item.

Code: Select all

        def give_item(self):
            if getattr(self, "created_for") == str(talking_to):

                inventory.remove(z)
                getattr(talking_to, "inventory").append(z)

Code: Select all

red_sweater_for_gwen = Item("gwen_", 500, "/images/screens/Store/gwen_sweater_red.png", "red_sweater_for_gwen", clothes)

    class Character_info:

        def __init__(self, ui_image, ui_color):
            self.ui_image = ui_image
            self.ui_color = ui_color
            self.inventory = []


    gwen_ = Character_info("/images/screens/Store/gwen_menu.png", "#7A1800")

screen inventory():
    button:
        xalign 0.5
        frame:

            xmaximum  200
            background  "#F3CD26"
            hbox:
                text "Give" xalign 0.5
        action Function(getattr(z, "give_item"))
                            

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2398
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: problems appending, "tuple object is not callable" inventory sys

#5 Post by Ocelot »

None of getattr are nessesary here. In fact, if you are using getattr and are not implementing specific tricky things, you are most certainly either misusing it or your class is fundamentally broken.

getattr(character_var, "inventory").append(item_being_used)character_var.inventory.append(item_being_used)
getattr(z, "give_item")z.give_item

This is not code that can work:

Code: Select all

def give_item(self):
    # First problem: what is talking_to? Does it have str() support? How does it behave? 
    # Right now it should give you an error, becasue there is no talking_to variable anywhere
    if getattr(self, "created_for") == str(talking_to): # → self.created_for == str(lalking_to)

        # same thing. invertory is not a variable in this context? Did you mean self.inventory?
        # where does z comes from? What is it type? Does it have properly implemented comparison functions or, 
        # if using default one, is the _identity_ of z and one in list the same?
        inventory.remove(z)
        getattr(talking_to, "inventory").append(z) # → talking_to.inventory.append(z)
< < insert Rick Cook quote here > >

Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

Re: problems appending, "tuple object is not callable" inventory sys

#6 Post by Dr_arell »

let me be a bit more specific

i know i can do this and just throw a bunch of if statemens and conditionals just to run one of the objects at the time

Code: Select all

gwen.inventory.append(item_being_used)
mike.inventory.append(item_being_used)
sarah.inventory.append(item_being_used)
but im trying to achieve this with the least amount of code possible, since the code its the same, i want to be able to change only the object using a variable, instead python looks for an inexistent object called character.

Code: Select all

character = gwen_ #mike_ #sarah_ #josh_ #logan_  <---------------------- these are all object names the variable can be
character.inventory.append(item_being_used)
i should be able to run any of these depending on what the variable character is, but the code above is not working.

Code: Select all

gwen.inventory.append(item_being_used)
mike.inventory.append(item_being_used)
sarah.inventory.append(item_being_used)
josh.inventory.append(item_being_used)
logan.inventory.append(item_being_used)
that is why im using getattr() because it does allow me using a variable, unlike the way above, and this is also the reason why i say getattr() is a work around.

Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

Re: problems appending, "tuple object is not callable" inventory sys

#7 Post by Dr_arell »

talking_to is a variable that has as purpose store objects from the class Character_info, one object at the time
so u could say talking_to can be equal to mike_ or gwen_ or sarah_ etc

z is the same than item_being_used, it contains a object a for loop spits out, so z is just that the name of the object im using, i use z on my original code but i put it as item_being_used here so it can be comprehend. z is just the name of the object im currently getting from the for loop, the varaible that contains the iteration from the for loop.

each object from the item class has a self.created_for, it is given the name of the character_info object they belong to, as a string.

so self.created_for lets say is == "mike_", talking_to will contain also the name of an object(character_info), but depending with who im interacting taking_to will be set to mike_ gwen_ sarah_ etc, anyways this always works as intended so its not the problem.

Code: Select all

if getattr(self, "created_for") == str(talking_to): # → self.created_for == str(lalking_to)
you are right about this, getattr() is not needed here, i changed it, but theres not much difference, the code still behaves the same.

the standalone inventory you see there is not a self.inventory, it is actually the inventory where all my objects are, or in other words, the players inventory, it is a tuple. Remember im trying to pass a object from my inventory to the characters self.inventory, sorry for the confusion.

sorry if this is a pain, but it is a full functional inventory system and its a bit big to post here, its almost complete, this is the part that is holding me back, i cannot post the whole thing here.

Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

Re: [SOLVED] problems appending, "tuple object is not callable" inventory sys

#8 Post by Dr_arell »

thank you very much, it seems the problem was that when you write the name of a object inside a variable the variable stores the object, not the name, i was trying to comparate with the name and that always came out false skipping the code i was running.

User avatar
Alex
Lemma-Class Veteran
Posts: 3093
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: [SOLVED] problems appending, "tuple object is not callable" inventory sys

#9 Post by Alex »

Dr_arell wrote: Tue Jun 29, 2021 12:11 am ...

Code: Select all

action Function(getattr(item_being_used, "give_item"), character)
and here i get a hard error "Tuple obejct is not callable" ...
Not sure, but try

Code: Select all

action Function(z.give_item, character)

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2398
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: [SOLVED] problems appending, "tuple object is not callable" inventory sys

#10 Post by Ocelot »

Dr_arell wrote: Thu Jul 01, 2021 6:00 am thank you very much, it seems the problem was that when you write the name of a object inside a variable the variable stores the object, not the name, i was trying to comparate with the name and that always came out false skipping the code i was running.
So, in the end the error was in the way talking_to was assigned/read. Which was not shown.

This is why it is always a good idea to isolate the error and create a reproducible example. For RenPy projects it usually means creating new project, copying relevant parts, removing irrelevant parts, replacing some values which come from other parts of the code with predefined values that should work (or a mock object). Ideally you will have a small 50-70 lines script that anyone can copy, paste in the new project and see the problem.

Sometimes problem disappears when you try to isolate it. That means that core issue was somewhere else and you need to broaden your serch. For example, you could notice that problem disappears when you replace talking_to with predefined string. That would mean the code is fine, and something wront with the variable you receive. So, you will shift focus to that variable. Adding code to test its value, checking where it comes from... In the end, creating self-contained example often enough to find and fix the problem.

About getattr: getattr(foo, "bar") is in most cases exact equivalent of foo.bar. The only reasons to use getattr are:
  • You want to access property (not object) which name is stored in variable
  • You want to make use of the third argument of getattr
< < insert Rick Cook quote here > >

Dr_arell
Regular
Posts: 70
Joined: Sun Feb 23, 2020 11:24 pm
Deviantart: DarellArt
Contact:

Re: [SOLVED] problems appending, "tuple object is not callable" inventory sys

#11 Post by Dr_arell »

yeah, it was a weird situation, somehow i assumed the evaluation, was coming True but it wasn't, this was an interaction i wasnt aware of,
this problem got me stuck for weeks, what i ended up doing is printing the value talking_to had after you mentioned it, just to find out it had the memory location the object was using and not the name of the object itself.

thanks for the help again man, i can finally have mental peace.

Post Reply

Who is online

Users browsing this forum: No registered users