[SOLVED] TypeError: 'type' object has no attribute '_getitem_'

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
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

[SOLVED] TypeError: 'type' object has no attribute '_getitem_'

#1 Post by HotMango »

Hi
I am trying to add a function that changes certain booleans value when you click on imagebutton with the right "selected_item"
But I get the error above.
Here are the relevant codes:

Code: Select all


default invbg = True
default invbgopen = False


# testitem = the item we are selecting, rightitem = the right item that will trigger event
# inputvariable1 = the variable that will change if hit with the rightitem, outputvariable1 = inputvariable1 new value
    def testItem(testitem, rightitem, inputvariable1, outputvariable1):

        if testitem == rightitem:
            inputvariable1 = outputvariable1





screen escapesc:
    if invbgopen:
        imagebutton:
            idle "outside.jpg"


    if invbg:
        imagebutton:
            idle "kitchen_match.jpg"
            action Function[testItem(selected_item, iHair, invbgopen, True)]
*Some of these codes are in different buffers ( .rpy files), if that is important to mention.*
This is the traceback:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 67, in script
    $ renpy.pause(hard=True)
  File "game/script.rpy", line 67, in <module>
    $ renpy.pause(hard=True)
  File "game/script.rpy", line 38, in execute
    screen escapesc:
  File "game/script.rpy", line 38, in execute
    screen escapesc:
  File "game/script.rpy", line 44, in execute
    if invbg:
  File "game/script.rpy", line 45, in execute
    imagebutton:
  File "game/script.rpy", line 45, in keywords
    imagebutton:
TypeError: 'type' object has no attribute '__getitem__'

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

Full traceback:
  File "game/script.rpy", line 67, in script
    $ renpy.pause(hard=True)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\ast.py", line 914, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\python.py", line 2028, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 67, in <module>
    $ renpy.pause(hard=True)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\exports.py", line 1480, in pause
    rv = renpy.ui.interact(mouse='pause', type='pause', roll_forward=roll_forward)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\ui.py", line 297, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 2702, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 3094, in interact_core
    root_widget.visit_all(lambda i : i.per_interact())
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 541, in visit_all
    d.visit_all(callback, seen)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\screen.py", line 430, in visit_all
    callback(self)
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\core.py", line 3094, in <lambda>
    root_widget.visit_all(lambda i : i.per_interact())
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\screen.py", line 440, in per_interact
    self.update()
  File "C:\Users\Business laptop\Downloads\Programs\renpy-7.3.5-sdk\renpy\display\screen.py", line 625, in update
    self.screen.function(**self.scope)
  File "game/script.rpy", line 38, in execute
    screen escapesc:
  File "game/script.rpy", line 38, in execute
    screen escapesc:
  File "game/script.rpy", line 44, in execute
    if invbg:
  File "game/script.rpy", line 45, in execute
    imagebutton:
  File "game/script.rpy", line 45, in keywords
    imagebutton:
  File "<screen language>", line 47, in <module>
TypeError: 'type' object has no attribute '__getitem__'

Windows-8-6.2.9200
Ren'Py 7.3.5.606
inventory tst 5 1.0
Thu Jan 23 20:39:10 2020
Thanks in advance.
Last edited by HotMango on Fri Jan 24, 2020 11:53 pm, edited 1 time in total.

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: TypeError: 'type' object has no attribute '_getitem_'

#2 Post by rayminator »

it telling where the error is coming from

Code: Select all

screen escapesc:
    if invbgopen:
        imagebutton:
            idle "outside.jpg"


    if invbg:
        imagebutton:
            idle "kitchen_match.jpg"
            action Function[testItem(selected_item, iHair, invbgopen, True)]  <----
the imagebutton looks wrong you have to use something like this

this only example

Code: Select all

imagebutton insensitive "shop/got_pizza1.png" idle "shop/got_pizza1.png" hover "shop/got_pizza1_h.png" xpos 200 ypos 580 action [(SetVariable("got_pizza", 999), Notify( "Activated" ))]
or

this only example

Code: Select all

imagebutton insensitive "shop/got_pizza1.png" idle "shop/got_pizza1.png" hover "shop/got_pizza1_h.png" xpos 200 ypos 580 action VariableValue("ritor", range = 1000, offset = 0)
or

this only example

Code: Select all

imagebutton insensitive "shop/got_pizza1.png" idle "shop/got_pizza1.png" hover "shop/got_pizza1_h.png" xpos 200 ypos 580 action SetField(cousin, "heart", cousin.heart+10)
my guessing it should look like this

Code: Select all

 if invbg:
        imagebutton:
            idle "kitchen_match.jpg"
            action Function("testItem", selected_item, iHair, invbgopen, True)
I might be wrong though I only learning how to do this I do understand it up 45% of it

User avatar
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: TypeError: 'type' object has no attribute '_getitem_'

#3 Post by HotMango »

I tried this but it says "unicode" object is not callable.
Thank you anyway :(

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: TypeError: 'type' object has no attribute '_getitem_'

#4 Post by Remix »

Code: Select all

    action Function( testItem, selected_item, iHair, invbgopen, True )
    ## Function( name_of_function, *any_arguments, **any_keyword_arguments ) # no () inside it, no [], just a class Action instance setup
Frameworks & Scriptlets:

User avatar
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: TypeError: 'type' object has no attribute '_getitem_'

#5 Post by HotMango »

I've changed it to this:

Code: Select all

class testItem(Action):
        def __init__(testitem, rightitem, inputvariable1, outputvariable1):
            if testitem == rightitem:
                inputvariable1 = outputvariable1
Did what you told me to do in here:

Code: Select all

if invbg:
        imagebutton:
            idle "kitchen_match.jpg"
            action Function( testItem,selected_item, iHair, invbgopen, True)
But it still doesn't work
now I get this error :

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 67, in script
    $ renpy.pause(hard=True)
  File "game/script.rpy", line 67, in <module>
    $ renpy.pause(hard=True)
  File "renpy/common/00action_other.rpy", line 517, in __call__
    rv = self.callable(*self.args, **self.kwargs)
TypeError: __init__() takes exactly 4 arguments (5 given)

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

Full traceback:
  File "game/script.rpy", line 67, in script
    $ renpy.pause(hard=True)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\ast.py", line 912, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\python.py", line 2004, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 67, in <module>
    $ renpy.pause(hard=True)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\exports.py", line 1471, in pause
    rv = renpy.ui.interact(mouse='pause', type='pause', roll_forward=roll_forward)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\ui.py", line 295, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\core.py", line 2699, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\core.py", line 3510, in interact_core
    rv = root_widget.event(ev, x, y, 0)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\screen.py", line 707, in event
    rv = self.child.event(ev, x, y, st)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\behavior.py", line 946, in event
    return handle_click(self.clicked)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\behavior.py", line 881, in handle_click
    rv = run(action)
  File "C:\Users\pc\Downloads\renpy-7.3.2-sdk\renpy\display\behavior.py", line 320, in run
    return action(*args, **kwargs)
  File "renpy/common/00action_other.rpy", line 517, in __call__
    rv = self.callable(*self.args, **self.kwargs)
TypeError: __init__() takes exactly 4 arguments (5 given)

Windows-7-6.1.7601-SP1
Ren'Py 7.3.2.320
inventory tst 5 1.0
Fri Jan 24 03:17:01 2020
PLEASE HELP!

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: TypeError: 'type' object has no attribute '_getitem_'

#6 Post by Remix »

Now you've made testItem into a class rather than a function... So when you do the button action it invokes the call action of the class

I would advise against extending the Action class unless you understand what you are doing... just use a function instead
It's nice that you try and have learned that much, just maybe start simpler

Addendum: Python has naming protocols (which makes it easy to skip read code)... ClassesAre leading capital camel case (and factory methods that return class instances), functions_and_methods are lower case with underscores... testItem is neither one or the other so it means anyone reading your code has to backtrack and find out what it is rather than just knowing ( TestItem = class or function yielding class, test_item = function or method )
Frameworks & Scriptlets:

User avatar
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: TypeError: 'type' object has no attribute '_getitem_'

#7 Post by HotMango »

If you want to cut straight to the point please look for the "partition" of this mark: "#" down in this comment.



I still don't understand what I should do to fix this problem
I have been coding in Python for 5 weeks at maximum, I really don't get what you're trying to say
I see in somecodes things like:

Code: Select all

def __call__(attr)
I don't know what that does
I'd really appreciate it if you could explain why the normal functions (the ones defined with "def" rather than "class bla (Action)" don't work
Is there anything I am doing wrong? In python I just define functions like

Code: Select all

def bla(1, 2, 3)
    1+2 = b
    b+3 = c
So when I call the function in the "action of the imagebutton" I just put some attributes to adjust what gets changed and what doesn't
If you still don't get what I am trying to do then please bear with me in the following:
############################################################################################################################################
I want to make an imagebutton that changes a defined variable (boolean) from False to True when that button is clicked only with the right item
So I made a function like:

Code: Select all

def testItem(testitem, rightitem, inputvariable1, outputvariable2):
    # I want the button to check if the selected_item (in this case it is the testitem) is the right one by comparing it with the "rightitem" which I will put as an attribute                 #    when programming the button and then performs this:
    if testitem == rigthitem: #this checks if the selected_item is the right one
        inputvariable1 = outputvariable1 #this changes the valuable of inputvariable1 (a defined variable(boolean)) to outputvariable2(True or False)
Here's the variable I want to change its value:

Code: Select all

default invbgopen = False
I want to change it to True when I click on a button while selecting the rightitem(selected_item = rightitem)

Code: Select all

screen bla bla:
    imagebutton:
        idle bla bla
        action testItem(selected_item, iHair, invbgopen, True)
Is the last code even right? or should I precede the function with "Function"?
I have no idea why this function doesn't work like my MixingItems function.

User avatar
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: TypeError: 'type' object has no attribute '_getitem_'

#8 Post by HotMango »

Okay, so after losing my mind I tried using something so primitive:
The imagebutton jumps to a label which checks if the selected_item matches the right item and then changes the variables manually like this:

Code: Select all

label tmplb:

    if selected_item == iHair:

        $ inventory.remove(iHair)
        $ invbg = False
        $ invbgopen = True
        show screen escapesc

        $ renpy.pause(hard=True)
    else:
        show screen escapesc
        $ renpy.pause(hard=True)
I tried putting the function I created in this label so it has more versatility to it but it just doesn't work.
This works perfectly but I know it is not feasible with the long-time projects.
*I CONSIDER THIS SOLVED* but if you have any better solution especially for "why functions don't work" in both screen language(action) and in label ($ function_name) please do help me.
Thanks for everyone, I'll be waiting for your solutions.

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: TypeError: 'type' object has no attribute '_getitem_'

#9 Post by Remix »

I would look at a few more screen Actions, including the If() action. You might find Ren'Py supports what you are trying to do:

https://renpy.org/doc/html/screen_actions.html

Code: Select all

    action If(
        selected_item == test_item, # <-- the condition (this is evaluated when the screen loads btw)
        ToggleVariable( "variable_name" ), # <-- the action to do If the condition yields a True
        NullAction() # <-- the action to do if condition yields False 
        ## (None would make the button insensitive, NullAction just does nothing while still allowing hover etc)
   )
Your use of labels to do it is perfectly valid too. You could even pass in the arguments to the label if wanted.
Frameworks & Scriptlets:

User avatar
HotMango
Regular
Posts: 28
Joined: Wed Jan 15, 2020 10:36 pm
Skype: aboodal3deny
Contact:

Re: TypeError: 'type' object has no attribute '_getitem_'

#10 Post by HotMango »

I will look into the action if() statement as I was looking for a way to use if statements exactly after "action" in an image map/button
Thank you very much and sorry for the inconvenience.
I will mark this as [SOLVED]

Post Reply

Who is online

Users browsing this forum: Google [Bot]