Player Naming your MC with Keyboard

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
wyverngem
Miko-Class Veteran
Posts: 613
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Player Naming your MC with Keyboard

#1 Post by wyverngem » Fri Nov 04, 2016 6:49 pm

Most games will allow us to name our main characters. It's been a feature for a long time, heroic main character names such as "Asmonkey" to the every so popular "1>~" or simple " ". How can we code this to prevent it? It's not that hard. To start your name you need to define your main character's name in an init block. I call mine mc just to keep it simple.

Code: Select all

default mc = "Guy"
This can be at the top of your script file or in it's own .rpy file. As long as it's before start. The next thing you need to do is to add the variable to the name slot in the defined characters. To do that we write [mc].

Code: Select all

define p = Character("[mc]")
You can get SUPER fancy with changing the character, but for now that's all you need in your code to change the name. So you come to the point where you want to let your player name your mc. We're going to create a label called nameMC for this. So in your story when you want them to name themselves "label nameMC' will be a looped label that you can call when naming the character.

Code: Select all

label start:
    # You're game here.
    e "Hello, you're the new student! What's your name?"
label nameMC:
    # game continues
Looking at the basic code for this. You can simply write,

Code: Select all

$ mc = renpy.input("What's your name?")
By doing this we give the user free range to put anything they want into that content. With this basic code we're not limiting how many characters they can have or what they put into the code. By not limited the number of characters we can have whole dialogues of script which causes your text box to become distorted. Don't believe me look.
overload.jpg
https://lemmasoft.renai.us/forums/download/file.php?mode=view&id=41442&sid=87a224707dfd2b316351df1902f63582
overload-2.jpg
I entered all that text and I continued to enter it without being stopped. Then I let the game continue with that name and played a while with the name causing distortions not only to my name box, but some other fancy screens I had used the variable mc with. So here's how you stop the player from entering a novel.

Code: Select all

$ mc = renpy.input("What's your name?", length=10)
Your player can now only enter 10 characters. Now I said characters, look what I can do now. I'm adding special characters as well as spaces. I can even add math equations! Or python, which can screw with your game.
characters.jpg
So we're taking that option out by only allowing letters lower and uppercase. Though before I give you the code ask yourself this.

Do I want to assume that all spaces are mistypes on the keyboard and use $ mc = mc.strip() to get rid of it, or do I want players to have a space in their names? So they can "Ellie von Nirenberg" (Not that the name would fix with a 10 character limit)

Also some names have apostrophes in them like R'aihir. If you're going to only allow lower and uppercase letters you're missing out on those. So add it if you want to at the end like this.

Without a space so you don't have the .strip() the name.

Code: Select all

    $ mc = renpy.input("Please give her a name. Max 10 characters", allow="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
With a space so you can name it.

Code: Select all

    $ mc = renpy.input("Please give her a name. Max 10 characters", allow=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
You can also remove trailing whitespaces by using .rstrip() which will remove those at the end of the name if you allow the user to add spaces.

You're almost done right! Nope. Before you allow a name to go on you can do a few more things. One is to make sure they just didn't hit enter, setting the variable of mc to nothing.

Code: Select all

    if mc == "":
        "You didn't enter anything for her name."
        jump nameMc
To make sure that the character's first letter of their name is capitalized write this:

Code: Select all

$ mc = mc.title()
Just keep in mind that .title() will capitalize every character after a space. So "Ellie von Weinberg" becomes "Ellie Von Weinberg".

Another thing you can check is how many characters are in the name the player has inputted. Without it you can have people calling their characters X or worse if you let them use apostrophes '. So to change that write this:

Code: Select all

    if mc is not None:
        $ check_name = len(mc)
        if check_name == 1:
            "You must name your character a minimum of 2 letters."
            jump nameMC
Another thing to note is that a character can be a space. So if you're allowing spaces you can end up with just spaces as the name. To correct you can see if there's two spaces in a row.

Code: Select all

    if mc == "  ":
        "You didn't enter anything for her name."
        jump nameMc
Hopefully this helpful for those who want to have keyboard entered user input. :D Here's the full code for those who want to look at it. It's not required by any means to have a quick choice menu to confirm that the player wants that name, but it's another way to just confirm that the user inputted what they input intentionally.

Code: Select all

default mc = ""

define e = Character('Eileen')
define p = Character("[mc]")

label start:
    # You're game here.
    e "Hello, you're the new student! What's your name?"
label nameMC:
    $ mc = renpy.input("Please give her a name. Max 10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
    if mc == "":
        "Please give your character a name that is between 2-10's characters long."
        jump nameMc
    if mc is not None:
        $ check_name = len(mc)
        if check_name == 1:
            "You must name your character a minimum of 2 letters."
            jump nameMC
    $ mc = mc.title()
    # Yay, your name doesn't hit any conditions. You can name them that!
    e "[mc]...well you're not on the list!"
    menu:
        "You want to name them [name]?"
        "No":
            jump nameMc
        "Yes":
            pass
    player "I'm just new."
    # game continues  
    return
Last edited by wyverngem on Tue Jan 29, 2019 6:28 pm, edited 4 times in total.

User avatar
noeinan
Eileen-Class Veteran
Posts: 1126
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Player Naming your MC with Keyboard

#2 Post by noeinan » Sun Dec 11, 2016 7:20 pm

Very useful tutorial, thank you!
Image

Image
Image

User avatar
xavimat
Eileen-Class Veteran
Posts: 1445
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: Player Naming your MC with Keyboard

#3 Post by xavimat » Fri Dec 16, 2016 11:23 am

Some comments:

- Using the same name for the character (mc) and the name (mc) could have unexpected outputs.
- Better to "default" the variable, instead of "init" it.
- You don't really need DynamicCharacter. Put the variable between [] (same effect)
- You could add also a trailing spaces check with strip:

Code: Select all

default mc = ""
define e = Character("Eileen")
define m = Character("[mc]")
label start:
    while not mc:
        $ mc = renpy.input("Please give her a name. 2-10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
        $ mc = mc.strip()
        if len(mc) < 2:
            "You must name your character a minimum of 2 letters."
            $ mc = ""
    $ mc = mc.title()
    m "Hi! My name is [mc]."
    e "[mc]...well you're not on the list!"
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
wyverngem
Miko-Class Veteran
Posts: 613
Joined: Mon Oct 03, 2011 7:27 pm
Completed: Simple as Snow, Lady Luck's Due,
Projects: Aether Skies, Of the Waterfall
Tumblr: casting-dreams
itch: castingdreams
Location: USA
Contact:

Re: Player Naming your MC with Keyboard

#4 Post by wyverngem » Sun Feb 05, 2017 6:01 pm

Yep, those are very good comments xavimat.
- Using the same name for the character (mc) and the name (mc) could have unexpected outputs.
Thanks for catching this, I forgot to change it back from my original set. Usually I use mc_name for their name and mc to define the character.
- Better to "default" the variable, instead of "init" it.
I learned my coding before most of us switched to the "default" for defining variables. If anyone is interested there's a forum post here about it viewtopic.php?f=32&t=42249&hilit=default+vs
- You don't really need DynamicCharacter. Put the variable between [] (same effect)
Yes, it's a bit outdatted, but it's the same reason that I code with init. (original cookbook https://www.renpy.org/wiki/renpy/doc/re ... cCharacter entry. Don't get confused just use the new documentation please.)
- You could add also a trailing spaces check with strip:
That would be $ mc_name.rstrip() for those who are curious.

Rhythm
Newbie
Posts: 9
Joined: Wed Feb 22, 2017 10:29 am
Contact:

Re: Player Naming your MC with Keyboard

#5 Post by Rhythm » Wed Feb 22, 2017 10:31 am

Hey, this seems really useful, however when I try to follow the recipe I get this error message:

Code: Select all

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 11, in script
    $ mc = renpy.input("Please give her a name. Max 10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
  File "game/script.rpy", line 11, in <module>
    $ mc = renpy.input("Please give her a name. Max 10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
  File "game/screens.rpy", line 170, in execute
    screen input(prompt):
  File "game/screens.rpy", line 170, in execute
    screen input(prompt):
  File "game/screens.rpy", line 173, in execute
    window:
  File "game/screens.rpy", line 175, in execute
    vbox:
  File "game/screens.rpy", line 175, in keywords
    vbox:
AttributeError: 'StoreModule' object has no attribute 'dialogue_xalign'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 11, in script
    $ mc = renpy.input("Please give her a name. Max 10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\ast.py", line 814, in execute
    renpy.python.py_exec_bytecode(self.code.bytecode, self.hide, store=self.store)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\python.py", line 1715, in py_exec_bytecode
    exec bytecode in globals, locals
  File "game/script.rpy", line 11, in <module>
    $ mc = renpy.input("Please give her a name. Max 10 characters", allow=" 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", length=10)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\exports.py", line 797, in input
    rv = renpy.ui.interact(mouse='prompt', type="input", roll_forward=roll_forward)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\ui.py", line 285, in interact
    rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\display\core.py", line 2519, in interact
    scene_lists.replace_transient()
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\display\core.py", line 822, in replace_transient
    self.remove(layer, tag)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\display\core.py", line 1107, in remove
    self.hide_or_replace(layer, remove_index, "hide")
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\display\core.py", line 1031, in hide_or_replace
    d = oldsle.displayable._hide(now - st, now - at, prefix)
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\display\screen.py", line 443, in _hide
    self.update()
  File "E:\Programs\renpy-6.99.12.3-sdk\renpy\display\screen.py", line 578, in update
    self.screen.function(**self.scope)
  File "game/screens.rpy", line 170, in execute
    screen input(prompt):
  File "game/screens.rpy", line 170, in execute
    screen input(prompt):
  File "game/screens.rpy", line 173, in execute
    window:
  File "game/screens.rpy", line 175, in execute
    vbox:
  File "game/screens.rpy", line 175, in keywords
    vbox:
  File "<screen language>", line 177, in <module>
AttributeError: 'StoreModule' object has no attribute 'dialogue_xalign'

Windows-8-6.2.9200
Ren'Py 6.99.12.3.2123
Veritas 1.0
Any ideas why?

User avatar
Divona
Miko-Class Veteran
Posts: 678
Joined: Sun Jun 05, 2016 8:29 pm
Completed: The Falconers: Moonlight
Organization: Bionic Penguin
itch: bionicpenguin
Contact:

Re: Player Naming your MC with Keyboard

#6 Post by Divona » Wed Feb 22, 2017 11:01 am

Rhythm wrote:Hey, this seems really useful, however when I try to follow the recipe I get this error message:

Any ideas why?
That's a bug in Ren'Py 6.99.12.3.2123. Solution has been posted here: viewtopic.php?f=8&t=41687&start=15#p444578
Completed:
Image

User avatar
justloveme94
Newbie
Posts: 10
Joined: Sat Nov 18, 2017 11:07 pm
Contact:

Re: Player Naming your MC with Keyboard

#7 Post by justloveme94 » Sat Nov 25, 2017 11:28 pm

Hello everyone, sorry for the question but I am still very new to coding so please forgive me! I am getting an error after inputting the name. I am not sure where I am going wrong. Do I need to input a menu some place?

Code: Select all

[code]
I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 90, in script
    menu:
KeyError: u'name'

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 90, in script
    menu:
  File "C:\Users\mazur\Desktop\Video Game Maker Stuff\renpy-6.99.13-sdk\renpy\ast.py", line 1480, in execute
    renpy.exports.say(None, "\n".join(narration), interact=False)
  File "C:\Users\mazur\Desktop\Video Game Maker Stuff\renpy-6.99.13-sdk\renpy\exports.py", line 1173, in say
    who(what, *args, **kwargs)
  File "C:\Users\mazur\Desktop\Video Game Maker Stuff\renpy-6.99.13-sdk\renpy\character.py", line 902, in __call__
    what = what_pattern.replace("[what]", sub(what, translate=translate))
  File "C:\Users\mazur\Desktop\Video Game Maker Stuff\renpy-6.99.13-sdk\renpy\character.py", line 884, in sub
    return renpy.substitutions.substitute(s, scope=scope, force=force, translate=translate)[0]
  File "C:\Users\mazur\Desktop\Video Game Maker Stuff\renpy-6.99.13-sdk\renpy\substitutions.py", line 242, in substitute
    s = formatter.vformat(s, (), kwargs)
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 563, in vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 585, in _vformat
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 646, in get_field
  File "/home/tom/ab/x64lucid-deps/install/lib/python2.7/string.py", line 605, in get_value
KeyError: u'name'

Windows-8-6.2.9200
Ren'Py 6.99.13.2919
Visual Novel Testing Ground 1.0

User avatar
justloveme94
Newbie
Posts: 10
Joined: Sat Nov 18, 2017 11:07 pm
Contact:

Re: Player Naming your MC with Keyboard

#8 Post by justloveme94 » Sat Nov 25, 2017 11:35 pm

Never mind! Figured it out, I had to change [name] to [mc] and it worked. It generated the name I had typed into the box.

Post Reply

Who is online

Users browsing this forum: No registered users