screen buttons not updating underlying dict

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
godel23
Newbie
Posts: 3
Joined: Sun Jan 06, 2019 11:56 am
Contact:

screen buttons not updating underlying dict

#1 Post by godel23 »

Hi,
I'm trying to set up a screen where the player can choose one of a number of challenges/minigames to be the next one to attempt. To make expanding the set of challenges easier, I have them stored as dicts in a list with keys to show if it is available / prioritised. I'm trying to use a textbutton to toggle the prepared key. The screen is called using a call screen statement.

The screen displays ok and clicking on the textbutton does indeed seem to toggle to the selection on my screen, but it's not changing the underlying dict. Going out of the screen and in again just resets everything (even opening the console and closing it). I've tried a number of ways of doing this (calling a function, using setDict, using enumerate in the for loop to get an index etc), but there is clearly something fundamental I'm missing. It seems clear from my testing that while a local copy of the variables are being updated the global variables are not, and that's why it keeps reverting.

Have searched through the online documentation and various topics on here, but can't work out what is happening. Any ideas what I'm doing wrong? Thanks!

Code: Select all

default challenges = [
    {   'Available' : True,
        'Prepared'  : False,
        'Name'      : 'horse riding challenge'},
    {   'Available' : True,
        'Prepared'  : False,
        'Name'      : 'swimming challenge'}	#etc
]

screen Laptop_Challenges():
    use Laptop_screen(_("Challenge Planner"), scroll = "viewport"):
            hbox:
                grid 1 2:
                    frame:
                        xsize 100
                        ysize 200
                        text "Skill games" size 22
                    frame:
                        xsize 100
                        ysize 200                    
                        text "Puzzles" size 22
                grid 4 2:
                    for c in challenges:
                        frame:
                            xsize 200
                            ysize 200
                            textbutton c['Name'] action [ToggleDict(c,'Prepared')] sensitive c['Available']

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

Re: screen buttons not updating underlying dict

#2 Post by Alex »

godel23 wrote: Sat Jun 12, 2021 7:41 am ...
How and where do you use this screen?
'Cause this sample code works for me.

Code: Select all

default my_list = [
    {"txt":"Text 1", "active":True},
    {"txt":"Text 2", "active":True},
    {"txt":"Text 3", "active":True},
    {"txt":"Text 4", "active":True},
    ]
    
screen container_scr():
    vbox:
        align(0.5,0.05)
        transclude

screen my_scr():
    modal True
    use container_scr:
        for bttn in my_list:
            button:
                hbox:
                    text bttn["txt"]
                    if bttn["active"]:
                        text " 1"
                    else:
                        text " 0"
                action ToggleDict(bttn, "active")
                sensitive bttn["active"]
                
        textbutton "Close" action Hide("my_scr")
        textbutton "Return" action Return()


# The game starts here.
label start:
    "..."
    call screen my_scr
    "... ..."
    show screen my_scr
    "..."
    "... ..."
    show screen my_scr
    "?!"

godel23
Newbie
Posts: 3
Joined: Sun Jan 06, 2019 11:56 am
Contact:

Re: screen buttons not updating underlying dict

#3 Post by godel23 »

I'm calling the screen as part of a normal label flow:

Code: Select all

label doWork:
    "You sit down in front of your laptop and pull up the challenges that [s.name] has prepared. Which one will we do next?"
    call screen Laptop_Challenges()
    "You lean back from the laptop. Hopefully that was time well spent - at least it wasn't too strenuous."
return
I ran your code and it works fine on my end too, so clearly it's something in my project that's creating the issue. When I moved my screen code into a standalone renpy project it worked fine, so I must have some sort of configuration issue. I'll try and experiment to see if I can see what I've done.

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: screen buttons not updating underlying dict

#4 Post by Remix »

Just remember that you will need to pass an interaction (a dialogue line, a pause or a renpy.checkpoint) after the value changes in order for it to be saved in the store.

Also note that the console is not a great place to test things like this as it performs a rollback before displaying the value.
Frameworks & Scriptlets:

godel23
Newbie
Posts: 3
Joined: Sun Jan 06, 2019 11:56 am
Contact:

Re: screen buttons not updating underlying dict

#5 Post by godel23 »

Ahhh - I think I've worked out what was going on. I (for reasons best known to my own sub-concious) had a screen nested within a screen within a screen, i.e. I was using two transclude statements. Flattening those led to the buttons working as expected. Thanks for your help @Alex, your example helped me troubleshoot what was happening.

And thanks for the heads up on the console @Remix - the rollback you describes explains perfectly the behaviour I'm seeing when I open it. Shouldn't be a problem in the final game, of course!

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Li yuanlin