- Include the markup tag [var=?variable] in your dialogue entry's Dialogue Text. (See note below.)
- Or use the TextInput() sequencer command in your dialogue entry's Sequence field.
TextInput(textFieldUIName, label, variable[, maxLength[, clear]])
This command will use the GameObject within the dialogue UI's hierarchy whose name matches textFieldUIName. This GameObject will have a StandardUIInputField component on it that controls the behavior of the input field. Your dialogue UI can have different StandardUIInputField GameObjects for different purposes -- for example, one for plain text, another for numbers, and a third for passwords.
When the command runs, it will set the input field's label to label and wait for the user to enter input. Leave label blank if you don't want to use a label. If the label starts with var=variable, it will use the value of the variable >varName.
Once entered, it will set the value of the Dialogue System variable variable to what the user entered. If the variable's type is Number in the dialogue database, the user's input is stored in the variable as a number; otherwise the input is stored as a text string.
To limit the length of input, specify maxLength.
By default, the input field will start with the current value of variable. To clear the variable to an empty string before showing the input field, include the argument "clear".
If you're building to mobile, you can also tick the StandardUIInputField's Use Touch Keyboard checkbox to open the platforms' mobile touch keyboard. (More details)
The input field will accept input when the user presses the Accept Key or Accept Button, and it will cancel input if the user presses the Cancel Key or Cancel Button. You can also add onscreen UI button to your StandardUIInputField GameObjects and connect their OnClick() methods to StandardUIInputField.AcceptTextInput and/or CancelTextInput.
Some examples of TextInput():
- Prompt the player for their name (max 50 characters), storing the result in Variable["playerName"]:
To store the variable value in the Player actor's Display Name field, in the next node use the ChangeActorName() function in the Script field like this:
Code: Select all
TextInput(Text Field UI, Your Name:, playerName, 50, clear)
You may want to loop back and ask the player to re-enter text if they entered a blank string. This post explains how to do that. To show the player's name in text, use the [var=Actor] markup tag in text.Code: Select all
ChangeActorName("Player", Variable["playerName"])
- Prompt the player for their age, storing the result in Variable["playerAge"] (assumes another StandardUIInputField named "Number Field UI" configured only to accept numbers):
Code: Select all
TextInput(Number Field UI, Your Age:, playerAge)
- Prompt the player for an 8-character password, storing the result in Variable["password"] (assumes another StandardUIInputField named "Password Field UI" configured to show "*" instead of characters typed):
Code: Select all
TextInput(Password Field UI, Password:, password, 8, clear)
Example Scene:
DS_InputNameExample_2022-11-25.unitypackage
Note on [var=?variable]:
Internally, this runs the TextInput() sequencer command as TextInput(Text Field UI,variable,variable). Note that the text field UI must be named "Text Field UI", The dialogue UI prefabs that ship with the Dialogue System use the name "Text Field UI".
Validating Text
You may want to check if the user entered a blank or invalid string and, if so, loop back to the TextInput() node. You can use the str.len() Lua function or your own C# methods registered with Lua -- for example, to check for banned words. Here's an example of using str.len() to check if the user entered only zero or one characters:
Note: If you only want to disallow blank input, UNtick the StandardUIInputfield's Allow Blank Input checkbox.
Continue Buttons
To disable the continue button during TextInput(), you can use the SetContinueMode() sequencer command. For example, use this sequence:
Code: Select all
SetContinueMode(false);
TextInput(Text Field UI, Your Name:, playerName)
Code: Select all
SetContinueMode(original); {{default}}