Page 1 of 1

Input MC name that filters NPC names! + extra nifty features (Ren'Py 7.4.2)

Posted: Tue Feb 16, 2021 9:19 pm
by cheonbyeol
Since I spent quite some time (and a couple stupid questions in the Discord) on figuring this out, might as well share the resulting code.

What this does is handling MC's name with a couple handy features:
  • can be given a list of names (NPCs) to avoid!
  • name is persistent (game remembers it even if you start a new game, so it will also be remembered in replays!)
  • asked once on first playthrough, but changable from Preferences anytime (could even be set before first playthrough)
This is how it looks for me: https://imgur.com/a/CSMzLsZ

Code: Select all

## in script.rpy
default npcs = ["name1","name2","name3"] ## list of names
define mc = Character("[persistent.mcname]", color="#faa")
## ...
label start:
    if not persistent.mcname: ## only asks for a name if none is defined. you can remove the condition if you prefer
        call screen name_mc
    mc "Hi! My name is [persistent.mcname]."
The screen that pops up

Code: Select all

## screens.rpy
screen name_mc():
    modal True
    zorder 100
    default name = persistent.mcname or "Mary Sue" ## put your own default name here
    add "overlay.png"
    frame:
        xalign 0.5
        yalign 0.5
        hbox:
            spacing 20
            add "mc sprite": ## optional, show the character they are supposed to name
                zoom 0.5
                xalign 0.0
                yalign 1.0
            vbox:
                ypos 100
                xalign 0.5
                spacing 10
                xsize 200
                label "Her name is..."
                frame:
                    xalign 0.5
                    xfill True
                    padding (10,10)
                    input value ScreenVariableInputValue("name") pixel_width 200 length 30
                $ name = name.strip()
                if name in npcs:
                    text "The name is taken, please choose something else.":
                        color "#f00"
                else:
                    textbutton _("Done!"):
                        action [SetVariable("persistent.mcname",If(name,name,"Mary Sue")),Return()]
                        ## if name is left empty, we default back to Mary Sue
Changing name from Preferences later on:

Code: Select all

## in screens.rpy, in screen preferences(), where it says something like "additional vboxes can be added for user-defined preferences"
hbox:
    spacing 10
    image "mc icon.png": ## again optional to show the MC design
        size (100,100)
    vbox:
        label _("Main character")
        default name_changing = False ## by default, the input is hidden, we display the name and a link to change it
        if not name_changing:
            text "Current name: [persistent.mcname]"
            textbutton _("Change name") action SetScreenVariable("name_changing",True)
        else:
            default name = persistent.mcname or "Mary Sue" ## this setting is available before starting a game, so we add a default again
            frame:
                xsize 220
                padding (10,10)
                input value ScreenVariableInputValue("name") pixel_width 200 length 30
                $ name = name.strip()
                if name in npcs:
                    text "The name is taken, please choose something else.":
                        color "#f00"
                else:
                    textbutton _("Done!"):
                        action [SetVariable("persistent.mcname",If(name,name,"Mary Sue")),SetScreenVariable("name_changing",False)]
                        ## again, if left empty, we default to Mary Sue. we also hide the input again.
Hope it is "cookbook-worthy" and someone will find it useful!
Let me know if you have suggestions, comments, questions, etc.

Re: Input MC name that filters NPC names! + extra nifty features (Ren'Py 7.4.2)

Posted: Mon Mar 08, 2021 5:25 pm
by Moshibit
This is a cool script, thanks for sharing.

Re: Input MC name that filters NPC names! + extra nifty features (Ren'Py 7.4.2)

Posted: Tue Oct 17, 2023 7:06 pm
by Two Dollars
Thank you for the code. I had a couple of questions.

Code: Select all

default npcs = ["name1","name2","name3"] ## list of names
So if the default npcs were Moe, Larry, and Curly then they player couldn't input those names, correct?

Code: Select all

define mc = Character("[persistent.mcname]", color="#faa")
## ...
label start:
    if not persistent.mcname: ## only asks for a name if none is defined. you can remove the condition if you prefer
        call screen name_mc
    mc "Hi! My name is [persistent.mcname]."

Does anyone know what code you would need to add for first name and last name? Would it be:

Code: Select all

define mc = Character("[persistent.firstname], [persistent.lastname]", color="#faa")