[Solved] How to know what the currently selected button is in a screen from python code?

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
henvu50
Veteran
Posts: 337
Joined: Wed Aug 22, 2018 1:22 am
Contact:

[Solved] How to know what the currently selected button is in a screen from python code?

#1 Post by henvu50 »

How can you check to see which button in a screen has their selected state set to true, from python code?

Code: Select all

init python:
    def SomeFunc_ReturnCurrentSelectedButton(Screen)
        getCurrentlySelectedButton(Screen)

screen 50():
    ....
    textbutton80
    textbutton81 (CURRENTLY SELECTED)
    textbutton82    
.....
Last edited by henvu50 on Tue Jun 22, 2021 11:43 am, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2402
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: How to know what the currently selected button is in a screen from python code?

#2 Post by Ocelot »

Screen widgets are Views. They are not intended to have data extracted from it. While there are multiple design patterns in existence, Model almost universally does not request data from Views. Ultimately, Views should decide their state based on data contained in the Model. I.e. your code should control state of the buttons, not other way around.
https://en.wikipedia.org/wiki/Model%E2% ... controller

If you want to know View state in Model that means something is terrible wrong with your architecture. I would suggest to reconsider your approach. As for your question, this code works, but it is fragile, because it accesses screen internals.

Code: Select all

screen TST_SCR():
    vbox:
        id "bvox"
        textbutton "BTN1":
            action SetVariable("iss", 1)
        textbutton "BTN2":
            action SetVariable("iss", 2)

default iss = 0

label start:
    show screen TST_SCR
    '...'
    '..'
    python:
        is_button_selected = ["selected" in button.role for button in renpy.get_widget(id="bvox", screen="TST_SCR", layer="screens").children]
    '[iss], [is_button_selected]'
    return
You will have to check list for selected buttons, because several buttons can be selected. Or no one.
< < insert Rick Cook quote here > >

Post Reply

Who is online

Users browsing this forum: No registered users