0 votes
by (120 points)
I want to have a character in my story ask the player for a name to use and have the player be able to type a name to be used. What code could I use to achieve this?

1 Answer

+2 votes
by (63.7k points)
edited by

In the future you need to specify which story format you're using and its full version (because, yes, that makes a difference).  For now, I'm going to assume that you're using either Harlowe or SugarCube.

In both of the following examples, the name set by the player is stored within the story variable $name.

NOTE: In addition to the basics shown below, you may wish to set up some code to check the variable's value after the player has been given a chance to enter a name, to ensure that they actually did enter something.

 

Harlowe v2.0.1+

You may use the (set:) and (prompt:) macros.  For example:

→ With a default name, that the player may change.
(set: $name to (prompt: "What is your name?", "default name"))

→ Without a default name.
(set: $name to (prompt: "What is your name?", ""))

 

SugarCube v2.18.0+

You may use the <<textbox>> macro.  For example:

→ With a default name, that the player may change.
What is your name? <<textbox "$name" "default name">>

→ Without a default name.
What is your name? <<textbox "$name" "">>

 

Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...