Naming the Player Character
There are a number of good reasons to allow the player to name the character they will be playing. The main one being that they will find it easier to play that character. However it also comes with challenges:
- The player must give themselves a name. They can't be a blank empty string.
- The player must not be able to enter a stupidly long name that will break your say box formatting. Someone will enter "Percival Fredrickstein von Musel Klossowski de Rolo III" just to see what happens.
- The player shouldn't be able to choose the name shared with an NPC in the game (even one they haven't met yet). This gets confusing if they talk to each other.
- The player shouldn't be put on the spot to choose a name, or forced to use a keyboard, so there should be a sensible default.
This will need a character definition for the PC, a list of names they can't pick, and a variable to hold their current name. Feel free to split these declarations into your various files:
define pc = Character("[pcName]") # The player character define npcNames = ["Diana", "Eileen", "Susan"] # Names the PC can't choose default pcName = "You" # What to call the PC
By using Ren'Py's text substitution in the who part of the character it can change once the player picks a name for their character.
Next, add this (or similar) to get the player's input and change pcName
:
# Allow the player to pick their character's name. # label pickName: $ renpy.dynamic('done', 'name', 'prompt') $ done = False while not done: $ prompt = "What is your given name?" $ prompt += " Type in your name, or just press <Enter> to be Erin." $ name = renpy.input(prompt, length=16, exclude=' ').strip().capitalize() or "Erin" if name in npcNames: " There's another person in this game called [name]. Please choose a different name for yourself. " else: $ done = True $ pcName = name return
Local variables are used for done
, name
, and
prompt
so as not to perturb the calling code.
A while
loop is used so the question can be repeated until
the player makes a good choice.
On the renpy.input
the name length is limited, and no
space characters are allowed. Whatever is entered is given an initial
capital.
If nothing is entered it is replaced with a default "Erin"
.
The name is then checked against a list of other NPC names. If it is
a match it is rejected with a suitable message.
Finally the chosen "good" name is assigned to pcName
.
Example
Here's an example use. A none to subtle hint is given that the player is naming a character who appears female in this example game. It can be disconcerting to have the player name a character before they know what gender they appear as.
"Barkeep" "And who might you be girl?" call pickName pc "I'm [pcName] and I don't like being called a girl thank you."