[SOLVED] if statement for a string within list in a class

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
Naustix
Regular
Posts: 46
Joined: Mon Dec 24, 2012 8:46 pm
Completed: ACE Academy
Projects: Entangled States, GalaCollider
Organization: Studio Lockheart
Github: naustix
Location: Omaha, NE
Contact:

[SOLVED] if statement for a string within list in a class

#1 Post by Naustix »

Hey Lemmasoft,

What is best way to make an if statement checking for a string within a list within a class?

Here is a piece of the code that contains the class and the list I wish to draw from, "equipped"- so within Inventory.equipped() ?

Code: Select all

    class Inventory():
        def __init__(self, name, limit):
            """
            name     - The name of the inventory. Can be anything
            limit    - The maximum number of items the player is allowed to have
            store    - A list which contains the items player has aquired
            equipped - Current Equipped items.
            """
            self.name       = name
            self.limit      = limit
            self.store      = []
            self.equipped   = {"core"   : "None",
                               "thrust" : "None",
                               "melee"  : "None",
                               "range"  : "None",
                               "frame"  : "None",
                               "misc"   : "None"
                              }
So for example if "range" has "gun1" assigned to it, how would I make an if statement checking if "gun1" is in the list?

I ask that broadly because there is only ever one kind of item in the game (so there's only 1 obtainable "gun1") and so they'll never appear in any other slot to confuse checking the entire list.

Let me know the answer, or if I can provide any additional info- thanks~!
Last edited by Naustix on Mon Nov 09, 2015 12:34 pm, edited 3 times in total.
Lead Programmer from ACE Academy
Project Lead & Engineer for Entangled States / Founder at Studio Lockheart

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: If statement for list containing string in class

#2 Post by PyTom »

Code: Select all

if "gun1" in list(self.equipped.values()):
     ...
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Naustix
Regular
Posts: 46
Joined: Mon Dec 24, 2012 8:46 pm
Completed: ACE Academy
Projects: Entangled States, GalaCollider
Organization: Studio Lockheart
Github: naustix
Location: Omaha, NE
Contact:

Re: If statement for list containing string in class

#3 Post by Naustix »

Thanks Tom.

Is it also possible to use these in a ConditionSwitch? When I attempt it I get

Code: Select all

File "game/rpy/sprites/GEAR Sprites.rpy", line 244: invalid syntax
            if "gun1" in list(self.equipped.values()):
              ^
And the code I use just looks like

Code: Select all

image meleeLoadout = ConditionSwitch (
        if "gun1" in list(self.equipped.values()):
            "i/sprites/GEAR/MC/weapons/default/pistol.png"
        )
Lead Programmer from ACE Academy
Project Lead & Engineer for Entangled States / Founder at Studio Lockheart

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: If statement for list containing string in class

#4 Post by PyTom »

Not like this. There are two problems. The first is that the ConditionSwitch takes a string expression, and the second is that self is not defined. One could do something like:

Code: Select all

image meleeLoadout = ConditionSwitch (
   "'gun1' in list(player.equipped.values)",
   "i/sprites/GEAR/MC/weapons/default/pistol.png",
)

Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Naustix
Regular
Posts: 46
Joined: Mon Dec 24, 2012 8:46 pm
Completed: ACE Academy
Projects: Entangled States, GalaCollider
Organization: Studio Lockheart
Github: naustix
Location: Omaha, NE
Contact:

Re: If statement for list containing string in class

#5 Post by Naustix »

Hmm,

When I now use (with "inv" now defined for the class usage)

Code: Select all

image rangeLoadout = ConditionSwitch (
        "'defaultPistol' in list(inv.equipped.values)",
        "i/sprites/GEAR/MC/weapons/default/pistol.png"
        )
I get the following error when trying to show the image in the script:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/rpy/episodes/E2/D5/E2D5S2.rpy", line 354, in script
    show mc attR at bl with dissolve
TypeError: 'instancemethod' object is not iterable

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

Full traceback:
  File "game/rpy/episodes/E2/D5/E2D5S2.rpy", line 354, in script
    show mc attR at bl with dissolve
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\ast.py", line 1250, in execute
    renpy.exports.with_statement(trans, paired)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\exports.py", line 1251, in with_statement
    return renpy.game.interface.do_with(trans, paired, clear=clear)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\core.py", line 1865, in do_with
    clear=clear)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\core.py", line 2285, in interact
    repeat, rv = self.interact_core(preloads=preloads, **kwargs)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\core.py", line 2622, in interact_core
    self.draw_screen(root_widget, fullscreen_video, (not fullscreen_video) or video_frame_drawn)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\core.py", line 1725, in draw_screen
    renpy.config.screen_height,
  File "renpy/display/render.pyx", line 379, in renpy.display.render.render_screen (gen\renpy.display.render.c:5907)
    rv = render(root, width, height, 0, 0)
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 650, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\transition.py", line 362, in render
    top = render(self.new_widget, width, height, st, at)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 650, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 650, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "renpy/display/accelerator.pyx", line 104, in renpy.display.accelerator.transform_render (gen\renpy.display.accelerator.c:1888)
    cr = render(child, widtho, heighto, st - self.child_st_base, at)
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 282, in render
    surf = render(self.child, width, height, st, at)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\image.py", line 218, in render
    return wrap_render(self.target, width, height, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\image.py", line 90, in wrap_render
    rend = render(child, w, h, st, at)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 650, in render
    surf = render(child, width, height, cst, cat)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 282, in render
    surf = render(self.child, width, height, st, at)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 282, in render
    surf = render(self.child, width, height, st, at)
  File "renpy/display/render.pyx", line 103, in renpy.display.render.render (gen\renpy.display.render.c:3066)
    cpdef render(d, object widtho, object heighto, double st, double at):
  File "renpy/display/render.pyx", line 185, in renpy.display.render.render (gen\renpy.display.render.c:2683)
    rv = d.render(widtho, heighto, st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 1127, in render
    self.update(st, at)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 1114, in update
    child, redraw = self.function(st, at, *self.args, **self.kwargs)
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 1177, in condition_switch_show
    return condition_switch_pick(switch), None
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\display\layout.py", line 1171, in condition_switch_pick
    if renpy.python.py_eval_bytecode(code):
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\python.py", line 1472, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "<none>", line 1, in <module>
  File "C:\Users\Naustix\Game_Development\REN'PY\renpy-6.99.5-sdk\renpy\python.py", line 558, in __init__
    list.__init__(self, *args)
TypeError: 'instancemethod' object is not iterable

Windows-8-6.2.9200
Ren'Py 6.99.5.602

If I take out .values and use

Code: Select all

image rangeLoadout = ConditionSwitch (
        "'defaultPistol' in list(inv.equipped)",
        "i/sprites/GEAR/MC/weapons/default/pistol.png"
        )
then I can get past that error but instead the game can no longer choose a displayable, like it can't search the list.
Lead Programmer from ACE Academy
Project Lead & Engineer for Entangled States / Founder at Studio Lockheart

User avatar
mobychan
Veteran
Posts: 275
Joined: Fri Apr 24, 2015 6:31 am
Projects: The Chosen - Sakura Pink & Gentian Blue
Organization: Foresoft
Location: Germany
Contact:

Re: [Help] 'instancemethod' object is not iterable

#6 Post by mobychan »

just looked over it real quick, but I believe it has to be values() not values

User avatar
Naustix
Regular
Posts: 46
Joined: Mon Dec 24, 2012 8:46 pm
Completed: ACE Academy
Projects: Entangled States, GalaCollider
Organization: Studio Lockheart
Github: naustix
Location: Omaha, NE
Contact:

Re: [Help] 'instancemethod' object is not iterable

#7 Post by Naustix »

mobychan is right, but in addition I needed to remove 'list' as well.

The fixed code for this is

Code: Select all

image rangeLoadout = ConditionSwitch (
        "'defaultPistol' in inv.equipped.values()",
        "i/sprites/GEAR/MC/weapons/default/pistol.png"
        )
Thanks for the help everyone~!
Lead Programmer from ACE Academy
Project Lead & Engineer for Entangled States / Founder at Studio Lockheart

Post Reply

Who is online

Users browsing this forum: Bing [Bot]