How to get text input from the user? (SOLVED)

I want to create an input form for the user to fill. The user has to fill in few details everytime the game starts, the game then takes the input given by the user and creates a scenario based on the user input. Please help me out thank you.

2 Likes

There’s a number of different things you need to do to deal with text input:

  1. Make sure you have added a text_trigger in your input_binding file (default is input/game.input_binding).
  2. Create a .gui and .gui_script with the text nodes to use in your form
  3. Detect using gui.pick_node() when the user clicks/taps the text nodes and keep track of which text node you have selected.
  4. If you’re on mobile you need to show the virtual keyboard using gui.show_keyboard() when a text node is selected and hide it using gui.hide_keyboard() when text nodes are deselected.
  5. Detect text input with the TEXT action_id (unless you changed the action name in the input_binding file) in your on_input() function. action.text will contain the pressed key.
  6. Add the entered character to the text in the text node. Detect backspace and add a trigger for the back key on an Android device to delete added characters.

If you want text selection, a cursor that you can move and so on things become more complex, but the steps above should be enough to give you basic text input.

7 Likes

works!! thank you!! appreciate your help.

Could any of you show me an example of the call to gui.show_keyboard(). I tried with gui.show_keyboard(gui.KEYBOARD_TYPE_DEFAULT, true) but got the error:

ERROR:SCRIPT: main/main.script:19: bad argument #4 to ‘show_keyboard’ (GuiScriptInstance expected, got userdata)

The api reference does not specify what values can be used for the autoclose parameter.

I think my issue was that I was not calling it from inside a gui_script. After moving the code into a gui_script and changing the autohide parameter from true to false the keyboard did show up!

Correct, if you are working with any of the functions in the gui module those functions need to be called from a .gui_script file or a Lua module required and run from a .gui_script file. The same thing goes for the go module and .script files.

2 Likes

thank you, this is exactly what I needed