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


#1

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.


First Impressions Feature Requests - Plugins, LuaSec, Game Center/Services, TextBoxes
Chat ability?
#2

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.


#3

works!! thank you!! appreciate your help.


Any way of communication between Defold HTML5 game and my own JS?
#4

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.


#5

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!


#6

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.