[HOWTO] How To: Read Input From Player During Conversations

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Read Input From Player During Conversations

Post by Tony Li »

Your conversations can prompt the user to enter input during conversations. There are two ways to prompt the user:
  • 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.
The TextInput() sequencer command's syntax is:

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"]:

    Code: Select all

    TextInput(Text Field UI, Your Name:, playerName, 50, clear)
    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

    ChangeActorName("Player", Variable["playerName"])
    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.
  • 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:


Image


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)
and in the next node, restore the original continue mode:

Code: Select all

SetContinueMode(original); {{default}}
talofangus
Posts: 5
Joined: Mon Apr 01, 2024 9:51 pm

Re: [HOWTO] How To: Read Input From Player During Conversations

Post by talofangus »

UNtick the StandardUIInputfield's Allow Blank Input checkbox.
Where? How?
ChangeActorName("Player", Variable["playerName"])
And where does this go? I've tried the sequence and script fields for the "what's your name?" and delay evaluation nodes, but if I then reference 'Actor', the name is still the default. The docs say the name will change when that actor speaks, but adding a couple new player and npc nodes didn't help either.
Last edited by talofangus on Sun Apr 14, 2024 12:59 pm, edited 1 time in total.
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Read Input From Player During Conversations

Post by Tony Li »

Hi,
talofangus wrote: Sun Apr 14, 2024 12:23 pm
UNtick the StandardUIInputfield's Allow Blank Input checkbox.
Where? How?
allowBlankInput.png
allowBlankInput.png (92.95 KiB) Viewed 1076 times
talofangus wrote: Sun Apr 14, 2024 12:23 pmAnd where does this go? I doesn't seem to do anything if I put it in various sequence fields.
ChangeActorName("Player", Variable["playerName"])
In the Script field. It's a Lua function. Lua functions that perform actions should go in the Script field. Lua functions that check conditions should go in the Conditions field.
talofangus
Posts: 5
Joined: Mon Apr 01, 2024 9:51 pm

Re: [HOWTO] How To: Read Input From Player During Conversations

Post by talofangus »

Hi thanks. I edited my post again after you responded. I've tried the script field as well and am not getting it. If I reference 'Actor', as in var=Actor, should that be updated?
User avatar
Tony Li
Posts: 22104
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Read Input From Player During Conversations

Post by Tony Li »

ChangeActorName() doesn't update Variable["Actor"], which is what the [var=Actor] markup tag references. Instead, in your Script field:

Code: Select all

ChangeActorName("Player", Variable["playerName"]);
Variable["Actor"] = Variable["playerName"]
Post Reply