How to make "show/hide stats" button in the pref menu?

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
User avatar
AERenoir
Veteran
Posts: 320
Joined: Fri May 27, 2011 8:23 pm
Contact:

How to make "show/hide stats" button in the pref menu?

#1 Post by AERenoir »

I have a "love meter" bar showing in a corner of the in-game screen. Is there a way to have a button to toggle it on and off in the preferences screen? Like, can you use "show/hide screen" as an action command for menus?

User avatar
SypherZent
Veteran
Posts: 362
Joined: Fri Sep 02, 2016 3:14 am
Completed: Multiverse Heroes, Space Hamster in Turmoil
Location: Puerto Rico
Contact:

Re: How to make "show/hide stats" button in the pref menu?

#2 Post by SypherZent »

Hello,

You would have to first define a parameter for the preferences menu.
So for example, inside of your scripts.rpy look for label start: and add something like this:

Code: Select all

label start:

    # Love Meter Toggle
    $ persistent.love_meter = True
The line with # is simply a commented line (you probably know this but I'm a bit anal retentive in my explanations lol).
The $ sign causes this line to be read as python code instead of the renpy scripting language.

The word persistent is sort of a class, which allows you to attach a variable of your own creation.
These persistent variables, to my understanding, persist even when the game is closed and reopened.
(Still confirming whether they pass onto Part 2 of the same game, have to do some more research but that's another topic. xD)

Now, basically the above code you have created the variable called love_meter that is a persistent variable.


You can then go to your preferences screen in the screens.rpy file.
Search for the following portion:

Code: Select all

##############################################################################
# Preferences
#
# Screen that allows the user to change the preferences.
# http://www.renpy.org/doc/html/screen_special.html#prefereces

screen preferences():
And inside of the preferences you will see that it has these frames with text buttons (if default style).
You can configure the preferences screen how you want, but basically you're going to just add this new button.

So search for a frame that's already there, I'll include the Auto-Forward time frame in the example:

Code: Select all

            frame:
                style_group "pref"
                has vbox

                label _("Auto-Forward Time")
                bar value Preference("auto-forward time")

                if config.has_voice:
                    textbutton _("Wait for Voice") action Preference("wait for voice", "toggle")

            frame:
                style_group "pref"
                has vbox
                
                label _("Love Meter")
                textbutton _("Enabled") action SetField(persistent, "love_meter", True)
                textbutton _("Disabled") action SetField(persistent, "love_meter", False)
The code above basically created the textbutton that allows the user to set the persistent field called love_meter to True or False.


Finally, inside your scripts.rpy where you are writing your story, you can do the ifcheck before displaying the love meter:

Code: Select all

if persistent.love_meter == True
# show the love meter here

Optionally, for further optimization you can create your own screen which performs the ifcheck and either shows or does not show the love meter. This way you only have to write one line in the script.rpy to call the love meter as well as include the ifcheck.

Code: Select all

#---IN SCREENS.RPY---

screen l_meter:
    if persistent.love_meter == True:
        #show the love meter here

#---IN SCRIPT.RPY---
show screen l_meter

Hope this helps!
Creator of Multiverse Heroes & Space Hamster in Turmoil

Want me to code your game?
Check my services thread!

Post Reply

Who is online

Users browsing this forum: Google [Bot]