Ren'Py [SOLVED]Conditional ToggleDict ?!

GoldenD

Member
Sep 30, 2018
102
70
Ok, it follows a first thread about ToggleDict.
The first was simple but now, the reality is I have some conditions or some controls before using ToggleDict correctly.
I tried to use action instead of Toggle, but when I do it, I have no more the effects of textbutton style.
Ok, the following code works fine, except the conditions of "updateCounter" function are not applied.
I hope someone will understand what I want to do, maybe Toggle isn't the answer, I don't know.

Python:
init python:
    class Item:
        def __init__(self, id, name=""):
            self.id             = id
            self.name           = name

    def updateCounter(idToSelect):
        if store.counterSelected != MAX_SELECT:
            store.listSelected[idToSelect] = not(store.listSelected[idToSelect])
            if store.listSelected[idToSelect] == True:
                store.counterSelected += 1
                return [True, False]
            elif store.counterSelected != 0:
                store.counterSelected -= 1
                return [False, True]
        elif store.listSelected[idToSelect] == True:
            store.listSelected[idToSelect] = False
            if store.counterSelected != 0:
                store.counterSelected -= 1
            return [False, True]
        return [False, True]

define listItems       = [(Item(0, name="Item01")), (Item(1, name="Item02")), (Item(2, name="Item03")), (Item(3, name="Item04")), (Item(4, name="Item05"))]
default listSelected    = dict()
default counterSelected = 0
define MAX_SELECT       = 3

label start():
    python:
        for _var in listItems:
            store.listSelected[_var.id] = False

    call screen selectItems(listItems)

screen selectItems(listOf):
    modal True
    zorder 200
    add "gui/overlay/confirm.png"
    add "gui/setup.png" pos (800, 100)  size (950,600)

    use displayItems(listOf)

    textbutton ("Ok")      action [Return(True), With( dissolve)]   xpos 950 ypos 610
    textbutton ("Cancel")  action [Rollback(), With( dissolve)]    xpos 1500 ypos 610

    key "game_menu" action [Rollback(),With( dissolve)]

screen displayItems(listOf):
    $ _xpos = 860
    $ _ypos = 220
    for eltItems in range(len(listOf)):
        textbutton listOf[eltItems].name xpos _xpos ypos _ypos:
            action [ToggleDict( listSelected, eltItems, Function(updateCounter,eltItems))]
            style "displayItems_button"

        $ _ypos += 35

    # For debug
    text "{color=#ffffff}counterSelected = [counterSelected] {/color}"

style displayItems_button is button
style displayItems_button_text is button_text:
    idle_color              "#000000"       # BLACK NORMAL
    hover_color             "#ffffff"       # WHITE HOVER
    selected_idle_color     "#550001"       # RED   SELECTED
    selected_hover_color    "#550001"       # RED   SELECTED
In updateCounter, I tried "return [False, True]" thinking it can take the place of originals True, False arguments of Toggle, but in fact, I think the call of my function does nothing and ToggleDict uses its default values, True and False
 

GoldenD

Member
Sep 30, 2018
102
70
Ok,
I think I found a solution.
Sure it's not the best but I have no more ideas.
In fact, I invert my conditions before Toggle switch True/False values.

Python:
    def updateCounter(idToSelect):
        #Invert before toggle
        if store.counterSelected != MAX_SELECT:
# Here I do what i want to do for the next True value by Toggle
            if store.listSelected[idToSelect] == False:
                store.counterSelected += 1

            elif store.counterSelected != 0:
                store.counterSelected -= 1

        elif store.listSelected[idToSelect] == True:
            if store.counterSelected != 0:
                store.counterSelected -= 1

        elif store.listSelected[idToSelect] == False:
            store.listSelected[idToSelect] = True
And for the Call, I invert Function and Toggle
Python:
.../...
        textbutton listOf[eltItems].name xpos _xpos ypos _ypos:
            action [Function(updateCounter, eltItems), ToggleDict( listSelected, eltItems, True, False )]
Not sure if it serves or interests someone, I leave it open in case someone has better and more conventional to propose to me
 

scrumbles

Well-Known Member
Jan 12, 2019
1,069
1,096
Does counterSelected track how many entries from listSelected are True?
I so, what's wrong with just sum(store.listSelected.values())?

Sorry if my post is retarded, most likely I didn't understand what you're trying to achieve.
 
Last edited:
  • Like
Reactions: VentureZapatist

GoldenD

Member
Sep 30, 2018
102
70
Does counterSelected track how many entries from listSelected are True?
I so, what's wrong with just sum(store.listSelected.values())?

Sorry if my post is retarded, most likely I didn't understand what you're trying to achieve.
Like I said, my solution is certainly not the best because didn't know all abilities of renpy or python language. And your motion seems interesting.
And I understand, you don't understand my goal.
I will explain with screens on the next thread, maybe it'll be clearer.
 

GoldenD

Member
Sep 30, 2018
102
70
Ok, now I'll try to explain all my questions about Toggle... and actions

I join 3 screenshots to understand the use of ToggleField and multiple "checkbox" (using textbutton)

Have a screen to choose 5 spells in a list of 10.
The list is on the left of the 1st screen (textbuttons) and you can have a description on button_hover
If you click the button, the font becomes red and the spell is flag.
If you want to cancel your choice, you click again and the button is free.

Code controls you don't choose more than 5 spells and Ok button can't validate if you don't choose 5....

The same screen and code is used for others list of Items, I just change the name of list when I call the screen.

When you choosed 5 spells, the Ok button validates your choices and the list appears in BrotherHood Spells on the UI.
01.png 02.png 03.png