How to add an option to preferences?

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.
Message
Author
luminarious
Veteran
Posts: 353
Joined: Thu May 01, 2008 1:12 pm
Projects: Winter, winter
Location: Estonia
Contact:

How to add an option to preferences?

#1 Post by luminarious »

Un question, gentlemen. It has often been discussed as to wether sex scenes could somehow be avoided, for example if a 15-year-old wants to play a game with a story otherwise suitable for her/him. I do not know wether I will actually need it with my game, but nevertheless I'd like to know how to add an option "Adult scenes on/ off" to the preferences screen?

It would simply set a variable to true or false. And at the start of any possibly adult scene I would simply check for it. If adult_scenes is false, a milder alternative could be shown instead, or even just skipped. It's probably better if the preference was set to False by default, probably.. :)

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: How to add an option to preferences?

#2 Post by PyTom »

luminarious wrote:It would simply set a variable to true or false. And at the start of any possibly adult scene I would simply check for it. If adult_scenes is false, a milder alternative could be shown instead, or even just skipped. It's probably better if the preference was set to False by default, probably.. :)

Code: Select all

init python:

    # Set the default value.
    if persistent.hentai is None:
        persistent.hentai = False

    # Add the pref.
    config.preferences['prefs_left'].append(
        _Preference(
            "Hentai",
            "hentai",
            [ ("Enabled", True, "True"),
              ("Disabled", False, "True") ],
            base=persistent))
Then when it comes time for a hentai scene:

Code: Select all

if persistent.hentai:
    # Let's get it on.
else:
    # Holding hands is more than enough.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

luminarious
Veteran
Posts: 353
Joined: Thu May 01, 2008 1:12 pm
Projects: Winter, winter
Location: Estonia
Contact:

Re: How to add an option to preferences?

#3 Post by luminarious »

Thank you very much. I'll add it to the cookbook as well.. :)

EvilDragon
Veteran
Posts: 284
Joined: Fri Dec 28, 2007 5:47 am
Location: Where the Dragons rule!
Contact:

Re: How to add an option to preferences?

#4 Post by EvilDragon »

How to define the same thing with a toggle button? In fact, how is a toggle button done anyways?
Angels of paradise, angels of sacrifice
Please let me be under your wings...

luminarious
Veteran
Posts: 353
Joined: Thu May 01, 2008 1:12 pm
Projects: Winter, winter
Location: Estonia
Contact:

Re: How to add an option to preferences?

#5 Post by luminarious »

What do you mean by a toggle button?

EvilDragon
Veteran
Posts: 284
Joined: Fri Dec 28, 2007 5:47 am
Location: Where the Dragons rule!
Contact:

Re: How to add an option to preferences?

#6 Post by EvilDragon »

Meaning a single button that changes state when pressed.

This preference (as PyTom defined it) is a multi-button - meaning there are as many buttons as there are options for a preference (here, that's 2). A toggle button would be only a single button that changes it's text (or image, if it's an imagebutton) depending on the state of the preference: like in "Preference ON"/"Preference OFF".
Angels of paradise, angels of sacrifice
Please let me be under your wings...

delta
Epitome of Generic
Posts: 525
Joined: Sat Dec 22, 2007 12:59 pm
Projects: yes
Contact:

Re: How to add an option to preferences?

#7 Post by delta »

Ren'Py does not have this functionality. I wanted it, so I wrote my own (well, more like duplicated and slightly adjusted the existing preferences class). This won't work for your game as is, because it uses my own presentation shortcut functions and ties into other engine hacks of mine, but maybe you can adapt it. Also, I won't walk you through the steps to get it working, sorry.

Code: Select all

    class TogglePreference(object):
        """
        This is a class that's used to represent a 2-choice preference.
        For the original documentation, see the class _Preference in 00preferences.rpy
        """
        def __init__(self, name, field, boolmap=(False,True), base=_preferences):
            # boolmap maps properties that actually have more values internally to "False" and "True"

            self.name = name
            self.field = field
            self.values = []
            self.boolmap = boolmap
            self.base = base

            config.all_preferences[name] = self

        def render_preference(self, thisxpos=0, thisypos=0):
            cur = getattr(self.base, self.field)

            # this uses a hardcoded widget. Leave out the definition of checkboximage if you want to do it differently
            if cur == self.boolmap[0]:
                boolCur = False
                checkboximage = "ui/bt-cf-unchecked.png"
            elif cur == self.boolmap[1]:
                boolCur = True
                checkboximage = "ui/bt-cf-checked.png"
            else:
                boolCur = cur
                checkboximage = "ui/bt-cf-unchecked.png"

            def clicked(value=boolCur):
                if value == True:
                    writevalue = self.boolmap[0]
                elif value == False:
                    writevalue = self.boolmap[1]
                else:
                    writevalue = value # just making safe we don't mess up TOO much

                setattr(self.base, self.field, writevalue)
                return True

            # This is a custom display function that incorporates the checkboximage into a button. Use some ui code of your own here.
            widget_button(self.name, checkboximage, clicked, xsize=325, widgetyoffset=0)
And then, later, for example...

Code: Select all

# displayStrings is an object that contains all my UI text. You can replace it with string literals
        fullscreen_p = TogglePreference(displayStrings.config_fullscreen_label, 'fullscreen')
        transition_p = TogglePreference(displayStrings.config_transitions_label, 'transitions', (2,0)) # "1" would actually be the "some" state we don't use
        unreadskip_p = TogglePreference(displayStrings.config_skip_unseen_label, 'skip_unseen')
        choiceskip_p = TogglePreference(displayStrings.config_skip_after_choice_label, 'skip_after_choices')
and then, in the right spot on your preferences page...

Code: Select all

        fullscreen_p.render_preference()
        transition_p.render_preference()
        unreadskip_p.render_preference()
        choiceskip_p.render_preference()
The rest is left as an exercise for the reader.

EvilDragon
Veteran
Posts: 284
Joined: Fri Dec 28, 2007 5:47 am
Location: Where the Dragons rule!
Contact:

Re: How to add an option to preferences?

#8 Post by EvilDragon »

How come there is no toggle button functionality? Why is then the "check button" in Theme test (Developer mode) exhibiting the exact toggle action that I'm asking for?
Angels of paradise, angels of sacrifice
Please let me be under your wings...

luminarious
Veteran
Posts: 353
Joined: Thu May 01, 2008 1:12 pm
Projects: Winter, winter
Location: Estonia
Contact:

Re: How to add an option to preferences?

#9 Post by luminarious »

I had never actually used it, hence my ignorance.. :P
Code for the check button is apparently:

Code: Select all

ui.textbutton("Check Button", size_group=sg, clicked=ui.returns("toggle"), role=role(toggle_var), style='check_button')
Appropriate Wiki page - http://www.renpy.org/wiki/ui.textbutton - doesn't, however, mention this kind of role..

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: How to add an option to preferences?

#10 Post by PyTom »

The check button mode is a work in progress. It exists in the style system, but hasn't been set up in preferences yet.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

EvilDragon
Veteran
Posts: 284
Joined: Fri Dec 28, 2007 5:47 am
Location: Where the Dragons rule!
Contact:

Re: How to add an option to preferences?

#11 Post by EvilDragon »

luminarious wrote:I had never actually used it, hence my ignorance.. :P
Code for the check button is apparently:

Code: Select all

ui.textbutton("Check Button", size_group=sg, clicked=ui.returns("toggle"), role=role(toggle_var), style='check_button')
Appropriate Wiki page - http://www.renpy.org/wiki/ui.textbutton - doesn't, however, mention this kind of role..
That's because toggle_var is later below tested to determine the right course of action after ui.interact().
Angels of paradise, angels of sacrifice
Please let me be under your wings...

User avatar
Gear
Miko-Class Veteran
Posts: 764
Joined: Tue Apr 05, 2011 10:15 pm
Projects: Tempestus Sum
Organization: Xenokos Interactive
IRC Nick: Gear
Skype: Skye.Gear
Location: Grand Prairie, TX
Contact:

Re: How to add an option to preferences?

#12 Post by Gear »

PyTom wrote:
luminarious wrote:It would simply set a variable to true or false. And at the start of any possibly adult scene I would simply check for it. If adult_scenes is false, a milder alternative could be shown instead, or even just skipped. It's probably better if the preference was set to False by default, probably.. :)

Code: Select all

init python:

    # Set the default value.
    if persistent.hentai is None:
        persistent.hentai = False

    # Add the pref.
    config.preferences['prefs_left'].append(
        _Preference(
            "Hentai",
            "hentai",
            [ ("Enabled", True, "True"),
              ("Disabled", False, "True") ],
            base=persistent))
Then when it comes time for a hentai scene:

Code: Select all

if persistent.hentai:
    # Let's get it on.
else:
    # Holding hands is more than enough.
This might be a dumb question, but WHERE do you post the code (former, not latter). I keep getting errors it seems, no matter where I try and put it.
The best reason to get up in the morning is to outdo yourself: to do it better than you've ever done it before. But if you haven't done it better by nightfall... look at your globe and pick a spot: it's always morning somewhere.

User avatar
Aleema
Lemma-Class Veteran
Posts: 2677
Joined: Fri May 23, 2008 2:11 pm
Organization: happyB
Tumblr: happybackwards
Contact:

Re: How to add an option to preferences?

#13 Post by Aleema »

^ That's because if you're using the newer Ren'Py, this is outdated. To add things to the preference screen, you manually add the button in screens.rpy.

017Bluefield
Regular
Posts: 93
Joined: Mon Dec 30, 2013 1:55 am
Projects: Project Bluefield - Origins
Organization: Autumn Rain
Deviantart: playerzero17
itch: autumn-rain
Contact:

Re: How to add an option to preferences?

#14 Post by 017Bluefield »

So, is there a way to make custom Preference()s with Ren'Py now that it's in version 7? Or has this gone by the wayside for now?

martingerdes
Regular
Posts: 26
Joined: Sun Oct 07, 2018 5:14 am
Contact:

Re: How to add an option to preferences?

#15 Post by martingerdes »

I can answer this one! :D

In screens.rpy, look for "screen preferences():"
That is the screen which builds up the options you can configure as a player.

In there you find the comment

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

So directly below that, you can add something like:

Code: Select all

vbox:
  style_prefix "check"
  label _("Stuff to avoid")
  textbutton _("Sex") action ToggleField(persistent,"avoid_sex")
Then just initialize the choices in your own code:

Code: Select all

#Avoid stuff?
define persistent.avoid_sex = False
You should be able to adapt this as needed. 8)

PS: To answer the question more literally: since the screen is defined normally instead of being auto generated from an array, you won't be able to just append a new entry to an array and have that show up in the preferences. Unless you first code that into the screen (looping over the array and generating textbuttons in a vbox).

Post Reply

Who is online

Users browsing this forum: Google [Bot]