Let's say we want to allow the player to enter any name he likes for his character. Moreover, we want to reject very long names (which are likely to be mistakes anyway), and we want to extract the player's chosen first name from the rest.
"Identity Theft"
The player's forename is a text that varies. The player's full name is a text that varies.
When play begins:
now the command prompt is "What is your name? > ".
To decide whether collecting names:
if the command prompt is "What is your name? > ", yes;
no.
After reading a command when collecting names:
if the number of words in the player's command is greater than 5:
say "[paragraph break]Who are you, a member of the British royal family? No one has that many names. Let's try this again.";
reject the player's command;
now the player's full name is the player's command;
now the player's forename is word number 1 in the player's command;
now the command prompt is ">";
say "Hi, [player's forename]![paragraph break]";
say "[banner text]";
move the player to the location;
reject the player's command.
We also want to postpone the proper beginning of the game until we've gotten the name:
Instead of looking when collecting names: do nothing.
Rule for printing the banner text when collecting names: do nothing.
Rule for constructing the status line when collecting names: do nothing.
Your Bedroom is a room. The printed name of Your Bedroom is "[player's forename]'s Bedroom".
The player carries a letter. The description of the letter is "Dear [player's full name], [paragraph break]You have won the Norwegian Daily Lottery! ...".
If we are compiling for Glulx, this is enough to capture not only the player's name but also the capitalization he uses.
If we are compiling for the Z-machine, the player's input will unfortunately be reduced to lower case before we can inspect it. If we would like by default to capitalize the first letter of each word of the name, we might substitute the following after reading a command rule:
After reading a command when collecting names:
if the number of words in the player's command is greater than 5:
say "[paragraph break]Who are you, a member of the British royal family? No one has that many names. Let's try this again.";
reject the player's command;
now the player's full name is the substituted form of "[the player's command in title case]";
now the player's forename is word number 1 in the player's full name;
now the command prompt is ">";
say "Hi, [player's forename]![paragraph break]";
say "[banner text]";
move the player to the location;
reject the player's command.
The "reading a command" activity is rather advanced; for the moment, what we need to understand is that we're intervening in commands at the start of play and insisting that the player's first instruction to the game consist of a choice of gender. After that point, the gender will be set and play will proceed as normal.
In order to do the parsing, we define gender as a kind of value, and give several alternate names to each gender.
"Baritone, Bass"
Getting Started is a room.
Gender is a kind of value. The genders are masculine, feminine, and unknown. Understand "male" or "man" or "M" as masculine. Understand "female" or "woman" or "F" as feminine.
A person has a gender. The gender of the player is unknown.
When play begins:
now the command prompt is "Please choose a gender for your character. >".
After reading a command when the gender of the player is unknown:
if the player's command includes "[gender]":
now the gender of the player is the gender understood;
if the gender of the player is unknown:
say "This story requires a selection of male or female. [run paragraph on]";
reject the player's command;
if the gender of the player is masculine, now the player is male;
if the gender of the player is feminine, now the player is female;
say "[line break]Thank you. We now begin...";
now the command prompt is ">";
move the player to Sandy Beach;
reject the player's command;
otherwise:
say "Sorry, we're not ready to go on yet. [run paragraph on]";
reject the player's command.
Sandy Beach is a room.
Instead of examining the player when the player is female:
say "Congratulations, you are a girl!"
Instead of examining the player when the player is male:
say "Congratulations, you are a boy!"
If we had a whole series of things to ask the player about, we might define a whole series of kinds of value
The vocal ranges are soprano, mezzosoprano, contralto...
and use a "construction stage" variable to keep track of the current stage of character-construction, as in
After reading a command when the current construction stage is choosing a vocal range:
...
Suppose we would like to allow the player to choose a gender for the main character. We'd also like this to happen at the beginning of the game and outside the main parsing sequence. "When play begins" seems like a good place to put this.
"Pink or Blue"
When play begins:
say "Should your character be male or female? >";
if men win, now the player is male;
otherwise now the player is female;
say paragraph break.
Now a piece of Inform 6 code handles the unusual input. It's not necessary to understand this to use it, and the code should work for any question you'd like to ask the player. The first three words in quotation marks ('male', 'M', 'man'...) correspond to positive feedback; the later three words correspond to negative feedback. So "to decide whether men win" will be true if the player types one of the first three, and false if he types one of the last three.
To decide whether men win:
(- Question('male','M//','man','female','F//','woman') -)
Include (-
[ Question pos1 pos2 pos3 neg1 neg2 neg3 first_word_typed;
while (true) {
VM_ReadKeyboard(buffer, parse);
wn = 1; first_word_typed = NextWordStopped();
if (first_word_typed == pos1 or pos2 or pos3) rtrue;
if (first_word_typed == neg1 or neg2 or neg3) rfalse;
print "Please choose ", (address) pos1, " or ", (address) neg1, ". > ";
}
];
-)
Instead of examining the player when the player is female:
say "Congratulations, you are a girl!"
Instead of examining the player when the player is male:
say "Congratulations, you are a boy!"
The Room of Self-Knowledge is a room. "Mirrors cover every available wall-surface of this hexagonal chamber, allowing you to examine yourself from all angles."