lua script example with gui.show_keyboard() (Defold engine)
Assistant
User
shorter and more concise example
Assistant
Sure! Here's a more concise example of a Lua script for Defold that uses gui.show_keyboard() to allow text input into a GUI text node:
lua
User
why on_text function is not not called?
Assistant
The on_text function in a Defold script is called in response to text input if you have properly set up the input focus and the gui.KEYBOARD_TYPE_TEXT keyboard type in your call to gui.show_keyboard().
If your on_text function is not being called, check the following potential issues:
User
I can't find any mention in the documentation about the existence of the on_text function
Assistant
In the Defold engine, gui.show_keyboard() is used to display the virtual keyboard on mobile devices when you want to gather text input from the user. Below is an example Lua script for a Defold project that demonstrates how to work with gui.show_keyboard().
First, create a .gui_script file to handle the text input logic:
lua
-- input_handler.gui_script
function init(self)
-- Set the keyboard type to 'default' or any supported type
self.keyboard_type = 'default'
-- Determine whether the return key should be shown as 'done'
self.return_key_type = 'done'
-- Keep track of the current text
self.current_text = ""
-- Register the input field node that will trigger the keyboard
self.input_field_node = gui.get_node("input_field")
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed then
-- Check if the input field is touched
if gui.pick_node(self.input_field_node, action.x, action.y) then
-- Show the virtual keyboard
gui.show_keyboard(self.keyboard_type, self.return_key_type)
-- Set the selected state for the input field
gui.set_color(self.input_field_node, vmath.vector4(1, 1, 1, 0.5)) -- Highlight the input field
else
-- Hide the virtual keyboard
gui.hide_keyboard()
-- Reset the selected state for the input field
gui.set_color(self.input_field_node, vmath.vector4(1, 1, 1, 1)) -- Remove highlight from the input field
end
end
end
function on_text(self, text)
-- Update the text when input is received
self.current_text = self.current_text .. text
-- Set the text of the input field
gui.set_text(self.input_field_node, self.current_text)
end
function on_text_done(self)
-- Handle event when the return key is pressed or keyboard is dismissed
gui.hide_keyboard() -- Hide the keyboard
print("Text input finished:", self.current_text)
-- Reset the selected state for the input field
gui.set_color(self.input_field_node, vmath.vector4(1, 1, 1, 1)) -- Remove highlight from the input field
end
function final(self)
-- Called when the script is being deleted
gui.hide_keyboard() -- Ensure the keyboard is hidden
end
After writing the .gui_script, attach it to a GUI component:
Create a new GUI file if you haven't already.
Add a text node within your GUI editor by right-clicking in the Outline and selecting Add ▸ Text.
Give the text node an id such as "input_field".
In the GUI, under the Properties window, set the Script field to point to your .gui_script.
Make sure to provide interaction to your GUI by setting up the correct input bindings:
Go to game.input_binding in your project settings.
Create a new trigger with the name touch (or if one exists, use it) and bind an appropriate input, such as MOUSE_BUTTON_LEFT or TOUCH.
Please note that this example assumes a setup where the input is triggered by a screen touch. If you're targeting platforms other than mobile, you will need to adjust the code and input bindings accordingly.
When you run the game on a mobile device and tap the input field, it should trigger the virtual keyboard to appear, allowing the user to enter text. The script will update the text node with the entered text and hide the keyboard when the user is done.
function init(self)
self.input_node = gui.get_node("input_field") -- Assume you have a text node named "input_field"
end
function on_input(self, action_id, action)
if action_id == hash("click") and action.pressed then
if gui.pick_node(self.input_node, action.x, action.y) then
-- User clicked the input field, show keyboard
gui.show_keyboard(gui.KEYBOARD_TYPE_DEFAULT, false)
else
-- User clicked outside the input field, hide keyboard
gui.hide_keyboard()
end
end
end
function on_text(self, text)
-- Append the text to the current content
local current_text = gui.get_text(self.input_node) .. text
gui.set_text(self.input_node, current_text)
end
function final(self)
-- Ensure the keyboard is hidden when the component is deleted
gui.hide_keyboard()
end
Remember to attach this script to your GUI component and then bind a trigger in your input bindings file, such as:
This example will open the keyboard when you click or touch the input_field node and hides it if you tap somewhere else. It appends incoming text to the input_field node's text. The final function ensures that the keyboard is hidden if the GUI is unloaded.
Input Focus: Ensure your script has input focus. A GUI script receives input focus by default, but if the focus has been shifted elsewhere, you need to acquire it back by calling gui.acquire_input_focus() in the init function or where appropriate:
lua
function init(self)
gui.acquire_input_focus() -- Acquire input focus for the GUI component
end
function final(self)
gui.release_input_focus() -- Release input focus when the component is destroyed
end
Appropriate Keyboard Type: When showing the keyboard with gui.show_keyboard(), ensure you're using gui.KEYBOARD_TYPE_TEXT or another appropriate keyboard type:
lua
gui.show_keyboard(gui.KEYBOARD_TYPE_TEXT, false)
Correct Input Binding: Verify that your input bindings are set correctly. If you are using touch or mouse actions to trigger opening the keyboard, make sure the input binding file includes the correct mappings, like MOUSE_BUTTON_1 for left-click or a touch trigger mapping.
Ensure Keyboard Compatibility: The virtual keyboard will typically work on mobile devices and might not work as expected in desktop builds or in the Defold editor.
Check Script Attachment: Make sure your GUI script is correctly attached to the GUI file in the properties window.
Keyboard Overlay: Ensure no other GUI layer is obscuring the input field, intercepting the input focus.
Error-Free Script: Make sure there are no runtime errors in your script that could be causing the on_text function to be skipped.
If you have checked all these points and the on_text function is still not being called, consider posting a question on the Defold forums or checking the documentation for any updates or changes to the API that may not be covered here. There might be platform-specific nuances or a version change that affects how input is handled.
I apologize for the misunderstanding. In Defold, there's no built-in on_text function that gets called automatically for text input unlike in some other frameworks or engines. Instead, you have to manage text input using the on_input callback and check the action_id and action parameters to determine if text has been entered.
The on_input function in your GUI script is where you handle all input, including key presses. Here's how you could modify it to handle text input:
lua
function init(self)
gui.acquire_input_focus()
self.text = "" -- Keep track of the entered text
end
function on_input(self, action_id, action)
if action_id == hash("touch") and action.pressed then
-- Check for touch/click on the input field
local input_node = gui.get_node("input_field")
if gui.pick_node(input_node, action.x, action.y) then
gui.show_keyboard(gui.KEYBOARD_TYPE_DEFAULT, false)
else
gui.hide_keyboard()
end
end
-- Check for text input
if action_id == nil and action.text then
-- Concatenate the existing text with the new input
self.text = self.text .. action.text
local input_node = gui.get_node("input_field")
gui.set_text(input_node, self.text)
end
-- Check for backspace (use your backspace action_id)
if action_id == hash("backspace") and action.pressed then
-- Remove the last character
self.text = self.text:sub(1, #self.text - 1)
local input_node = gui.get_node("input_field")
gui.set_text(input_node, self.text)
end
end
function final(self)
gui.release_input_focus()
gui.hide_keyboard()
end
The main difference is that instead of an on_text function, you need to look for unfocused actions (where action_id is nil) with an action.text property. This detects text input, and you can then append it to your text string.
You'll also want to set up an action binding for backspace (or any other control actions), so you can handle text deletion.
For this to work, ensure that your input bindings are set up correctly, like so:
This will handle key events for text, but note that using text input from keyboards typically applies to desktop platforms. On mobile platforms, the virtual keyboard's inputs are usually automatically added to the input field once it's active. For Defold, you may need to set up a native extension or a specific input method for text fields to correctly accept and process text input from a virtual keyboard on mobile devices.