Ren'Py How to add a custom switch to the Preferences Screen? [SOLVED]

moose_puck

Active Member
Sep 6, 2021
739
1,656
I'm trying to add a switch that players can toggle on the Preferences screen, which will turn NTR on and off.

I found some examples on the Ren'py Patreon page where they show how to first define a persistent variable and then add a toggle in the preferences screen and it seems to work, but the way it is written, the preference screen only shows one textbutton and the status of the variable is shown by if the textbutton is highlighted or not. I'd much rather prefer to have two texbuttons ... one saying "NTR On" and the other saying, of course, "NTR Off", similar to how the option for Fullscreen and Window are displayed.

Here is the code as I am testing it right now....

First, defining the persistent variable ntr_switch ...

Python:
default persistent.ntr_switch = False
Then adding the code in your screens file ...

Python:
                vbox:
                    style_prefix "check"
                    label _("NTR")
                    textbutton _("NTR On") action ToggleField(persistent, "ntr_switch")

So how could I change this to have two texbuttons, as I described above?

Another issue I am thinking about is what happens if I change the variable "ntr_switch" in the script somewhere? Does a script change of the variable cause the textbutton on the preferences screen to change? Because, right now, I am planing on asking the player during the opening introduction whether they want to turn NTR on or off, but if that is going to cause problems with the way it is presented on the Preferences Screen, then I might re-think that and leave the only option on the Preferences Screen.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,130
14,809
I'd much rather prefer to have two texbuttons ...

Not exactly your "NTR off"/"NTR on" idea, but I found this one better, and it's easy to switch back to your own view if you want:
Python:
screen whatever():

    hbox:
        text "NTR"
        null width 5  # Left some space to aerate.
        textbutton "Off":
            # Decide when the button is to be shown as /selected/.
            selected not persistent.ntr_switch
            action SetField( persistent, "ntr_switch", False )
        null width 5  # Left some space to aerate.
        textbutton "On":
            # Decide when the button is to be shown as /selected/.
            selected persistent.ntr_switch
            action SetField( persistent, "ntr_switch", True )

Another issue I am thinking about is what happens if I change the variable "ntr_switch" in the script somewhere? Does a script change of the variable cause the textbutton on the preferences screen to change?
It's the same variable, therefore what you do in the script will apply to the screen, and vice-versa.
It works even with the ToggleField screen action. It will decide what should be the new value accordingly to the one the moment the action is triggered (so the button clicked).
 

moose_puck

Active Member
Sep 6, 2021
739
1,656
Not exactly your "NTR off"/"NTR on" idea, but I found this one better, and it's easy to switch back to your own view if you want:
Python:
screen whatever():

    hbox:
        text "NTR"
        null width 5  # Left some space to aerate.
        textbutton "Off":
            # Decide when the button is to be shown as /selected/.
            selected not persistent.ntr_switch
            action SetField( persistent, "ntr_switch", False )
        null width 5  # Left some space to aerate.
        textbutton "On":
            # Decide when the button is to be shown as /selected/.
            selected persistent.ntr_switch
            action SetField( persistent, "ntr_switch", True )



It's the same variable, therefore what you do in the script will apply to the screen, and vice-versa.
It works even with the ToggleField screen action. It will decide what should be the new value accordingly to the one the moment the action is triggered (so the button clicked).
Thanks anne O'nymous

I'm done for the day, so I'll try this out in the morning.

One question though - I see you got this code in an hbox block. Can I nest this under a vbox, in the area where Ren'py suggests you adding an optional preference? I'd like to display the option on the top row of the Preferences, as it fits in there nicely (So my preference screen would have "Display" - "Rollback Slide" - "Skip" and now this "NTR")

Here's the relevant section from the default screens.rpy...

Python:
screen preferences():

    tag menu

    use game_menu(_("Preferences"), scroll="viewport"):

        vbox:

            hbox:
                box_wrap True

                if renpy.variant("pc") or renpy.variant("web"):

                    vbox:
                        style_prefix "radio"
                        label _("Display")
                        textbutton _("Window") action Preference("display", "window")
                        textbutton _("Fullscreen") action Preference("display", "fullscreen")

                vbox:
                    style_prefix "radio"
                    label _("Rollback Side")
                    textbutton _("Disable") action Preference("rollback side", "disable")
                    textbutton _("Left") action Preference("rollback side", "left")
                    textbutton _("Right") action Preference("rollback side", "right")

                vbox:
                    style_prefix "check"
                    label _("Skip")
                    textbutton _("Unseen Text") action Preference("skip", "toggle")
                    textbutton _("After Choices") action Preference("after choices", "toggle")
                    textbutton _("Transitions") action InvertSelected(Preference("transitions", "toggle"))


                ## Additional vboxes of type "radio_pref" or "check_pref" can be
                ## added here, to add additional creator-defined preferences.
                
                *** New option here ***
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Respected User
Donor
Jun 10, 2017
10,130
14,809
Can I nest this under a vbox,
Of course. I used a hbox because a single line is generally easier to fit in an already made design, it's the only reason. But you can perfectly change this to a vbox without problem. Just remember to remove the null statement, that become useless, it's all that have to be changed.
 
  • Like
Reactions: moose_puck

moose_puck

Active Member
Sep 6, 2021
739
1,656
Of course. I used a hbox because a single line is generally easier to fit in an already made design, it's the only reason. But you can perfectly change this to a vbox without problem. Just remember to remove the null statement, that become useless, it's all that have to be changed.
Tried your code this morning. I removed those nulls as suggested and I added some font styling and a ypos tag since the Label font was off and slightly higher then the other items on the top row. Works great! Here's how the basic Preferences Page looks like with the new NTR switch...

You don't have permission to view the spoiler content. Log in or register now.

Just made one additional change. I added the style_prefix "check" to make it use the same radio button on the left side...

You don't have permission to view the spoiler content. Log in or register now.
 
Last edited: