Screen with input box for variables that get stored

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
Houlerner
Newbie
Posts: 1
Joined: Sat Sep 14, 2019 6:29 pm
Projects: Project Ascension
Location: Ireland
Contact:

Screen with input box for variables that get stored

#1 Post by Houlerner » Sat Sep 14, 2019 8:43 pm

Hello, I'm pretty new to Ren'py and wanted to do something kind of specific that let's me call for an variable input box with a message/question to define variables and store them. It took me way longer than I'd hoped for something that was in the end just knowing the correct commands and syntax. So I'm gonna share this in the hopes someone will find it useful one day.

Here's how it looks:
input_box.jpg
input_box.jpg (17.79 KiB) Viewed 128 times

And here's the code:

Code: Select all

screen input_box(message, variable_name, done=Return(True)):
    modal True

    frame:
        left_padding 20 top_padding 20 right_padding 20 bottom_padding 20
        xalign 0.5 yalign 0.5
        vbox:
            spacing 10
            text message
            frame:
                xalign 0
                xsize 320
                input value VariableInputValue(variable_name,returnable=True)
            textbutton _("Confirm"):
                xalign 0.5
                action done
^ The custom screen. You can put this in the screens.rpy file. Obviously it could be made a lot prettier but I'll leave that to you. The "returnable" parameter means that you can also press enter instead of using the confirm button.

Code: Select all

default mc_name = "Hans"
define mc = Character("mc_name", who_color="#EB5", dynamic=True)
^ Defining a default value for the variable that will later display as the default / pre-entered input. Not defining the variable will result in an error. Second line is so you can later use mc to have a speaker for text.

Code: Select all

label naming:
    call screen input_box("Name your character:", "mc_name") # call function stops the rest of the game until the function is returned
    
    $ mc_name = mc_name.strip() # strip whitespace at start and end of input
    
    mc "I'm alive!"
^ The code to call up the screen in the first line after the label. Afterwards we use strip() to remove the white space around the name in case the user accidentally pressed space before submitting the name.


And that's it. I hope you found this useful :)
Last edited by Houlerner on Sun Sep 22, 2019 4:49 am, edited 1 time in total.

User avatar
namastaii
Eileen-Class Veteran
Posts: 1299
Joined: Mon Feb 02, 2015 8:35 pm
Projects: Template Maker for Ren'Py, What Life, Coffea Beans
Github: namastaii
Skype: Discord: lunalucid#7684
Soundcloud: deadseed
itch: lunalucid
Location: USA
Contact:

Re: Screen with input box for variables that get stored

#2 Post by namastaii » Thu Sep 19, 2019 12:04 am

I remember this from the Discord conversation. Thank you for sharing. This is a good solution for calling an input screen to store any variable instead of something specific so it's good to know about.

Post Reply

Who is online

Users browsing this forum: No registered users