Page 1 of 1

For loops, list, and screens

Posted: Thu Jan 16, 2020 2:52 am
by wyverngem
Can someone please explain to me why sometimes a for loop works and sometimes it doesn't in screen language?

I can easily display a list of strings by using a for loop, such as party members. However, if I try to change the list while showing the screen it doesn't update. What is the work around to making something simple like the following code work as intended?

Code: Select all

define party_list = []
init python:
    global party_list
    def add_member(name):
        if name not in party_list:
            party_list.append(name)
    def remove_member(name):
        if name in party_list:
            party_list.remove(name)

screen party_status():
    vbox:
        label "Current Party"
        for i in party_list:
            textbutton i action Function("remove_member(i)")

        label "Out of party, click to add them."
        textbutton "Terra" action Function(add_member('Terra'))
        textbutton "Celest" action Function("add_member", 'Celest')
        textbutton "Locke" action Function("add_member", 'Locke')

        null height 100
        textbutton "Confirm" action Return()

screen my_party:
    hbox:
        xalign 0.9 spacing 25
        for name in party_list:
            label name text_size 50

label start:
    $ party_list = []
    call screen party_status
    show screen my_party
    vt "Eir appears from the left."
    "Eir" "Looks like we're making some progress."
    return
The intent is that you can add the names to the pre-defined list and remove them as many times as you want. You hit confirm when you're happy with the list. However, it doesn't work. The screen doesn't update and sometimes when you start the game it already has a name inside of it. I'm not sure what's going on with the code, but any help would be appreciated.

Re: For loops, list, and screens

Posted: Thu Jan 16, 2020 3:58 am
by philat
You're not "refreshing" the screen when you use your Function(). You can manually restart the interaction, but AddToSet() and RemoveFromSet() are honestly probably easier... https://www.renpy.org/doc/html/screen_a ... ta-actions

Re: For loops, list, and screens

Posted: Thu Jan 16, 2020 11:17 am
by wyverngem
EDIT: OKay silly me forgot to capitalize on the SetToAdd, however, I'm still getting an error.

My code:

Code: Select all

default my_list = []

screen change_my_list:
    vbox:
        xalign 0.1 yalign 0.1
        label my_list text_size 50
        null height 20
        label "Add to Set."
        textbutton "Apple" action AddToSet("my_list", "Apple")
        null height 25

        label "Remove From Set."
        textbutton "Apple" action RemoveFromSet("my_list", "Apple")
        null height 24
        textbutton "Confirm" action Return()
label main_menu:
    return
# The game starts here.

label start:
    scene black
    call screen change_my_list
    "games continues"
The error:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 27, in script
    call screen change_my_list
  File "renpy/common/000statements.rpy", line 531, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "renpy/common/00action_data.rpy", line 417, in __call__
    self.set.add(self.value)
AttributeError: 'unicode' object has no attribute 'add'

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

Full traceback:
  File "game/script.rpy", line 27, in script
    call screen change_my_list
  File "C:\Program Files\Renpy 7.3.5\renpy\ast.py", line 1949, in execute
    self.call("execute")
  File "C:\Program Files\Renpy 7.3.5\renpy\ast.py", line 1937, in call
    return renpy.statements.call(method, parsed, *args, **kwargs)
  File "C:\Program Files\Renpy 7.3.5\renpy\statements.py", line 277, in call
    return method(parsed, *args, **kwargs)
  File "renpy/common/000statements.rpy", line 531, in execute_call_screen
    store._return = renpy.call_screen(name, *args, **kwargs)
  File "C:\Program Files\Renpy 7.3.5\renpy\exports.py", line 2905, in call_screen
    rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)
  File "C:\Program Files\Renpy 7.3.5\renpy\ui.py", line 297, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\core.py", line 2702, in interact
    repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, **kwargs)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\core.py", line 3518, in interact_core
    rv = root_widget.event(ev, x, y, 0)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\screen.py", line 714, in event
    rv = self.child.event(ev, x, y, st)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\layout.py", line 998, in event
    rv = i.event(ev, x - xo, y - yo, cst)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\behavior.py", line 962, in event
    return handle_click(self.clicked)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\behavior.py", line 897, in handle_click
    rv = run(action)
  File "C:\Program Files\Renpy 7.3.5\renpy\display\behavior.py", line 320, in run
    return action(*args, **kwargs)
  File "renpy/common/00action_data.rpy", line 417, in __call__
    self.set.add(self.value)
AttributeError: 'unicode' object has no attribute 'add'

Windows-8-6.2.9200
Ren'Py 7.3.5.606
Sandbox 1.0
Thu Jan 16 09:20:55 2020

Re: For loops, list, and screens

Posted: Thu Jan 16, 2020 12:09 pm
by RicharDann
In your code, you're passing your list name as a string object, hence the error. Instead, you need to pass the list or set name directly:

Code: Select all

textbutton "Apple" action AddToSet(my_list, "Apple")

Re: For loops, list, and screens

Posted: Fri Jan 17, 2020 1:35 am
by wyverngem
Thank you, makes more sense now. :D