For loops, list, and screens

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
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

For loops, list, and screens

#1 Post 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.

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: For loops, list, and screens

#2 Post 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

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: For loops, list, and screens

#3 Post 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

User avatar
RicharDann
Veteran
Posts: 286
Joined: Thu Aug 31, 2017 11:47 am
Contact:

Re: For loops, list, and screens

#4 Post 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")
The most important step is always the next one.

User avatar
wyverngem
Miko-Class Veteran
Posts: 615
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: For loops, list, and screens

#5 Post by wyverngem »

Thank you, makes more sense now. :D

Post Reply

Who is online

Users browsing this forum: Ocelot