The Defold engine uses Lua as its scripting language, and it provides a module named html5 for interaction with the browser environment when building HTML5 games. However, as of my last update in early 2023, the html5 module does not have methods called run() or set_interaction_listener().
If these are new methods introduced after my last update, I will be unable to provide precise information. However, if you are referring to creating interaction with HTML5 in Defold using functions from the html5 module, I can show you how to use available methods like html5.run() to evaluate JavaScript code and html5.add_listener() to set up an interaction listener.
Here is an example of how you might use these functions or their hypothetical newer counterparts:
-- main.script
local function on_interaction(self, event)
if event.type == "click" then
print("Canvas was clicked!")
end
end
function init(self)
-- Set up an interaction listener using (hypothetical) set_interaction_listener
-- Note: The following code assumes 'set_interaction_listener' is an actual method.
-- Replace with 'html5.add_listener()' if 'set_interaction_listener' doesn't exist.
html5.set_interaction_listener(on_interaction)
-- Run some JavaScript code using the html5.run method
local js_code = [[
document.getElementById('canvas').addEventListener('click', function() {
Module.ccall('html5_interaction_callback', null, ['string'], ['click']);
});
]]
html5.run(js_code)
end
-- External callback function to be called from JavaScript
function html5_interaction_callback(event_type)
if event_type == "click" then
print("Canvas was clicked! (Called from JS)")
end
end
-- Note: You need to make `html5_interaction_callback` available to JavaScript by
-- adding it to the `custom_html5_api` section in your game.project file:
-- [html5]
-- custom_html5_api = html5_interaction_callback
In the game.project file, you would need to expose the html5_interaction_callback for it to be callable from JavaScript:
[html5]
custom_html5_api = html5_interaction_callback
The html5.run() method is used to execute JavaScript code from Lua, and in this example, it sets up an event listener directly in the browser's document object model (DOM). The callback function html5_interaction_callback is a placeholder for the function you want to execute when the event is triggered.
Please note that my explanation is based on existing functions of the html5 Defold module as of my knowledge cutoff date. You should refer to the official Defold documentation or check the updated API references for the latest information and correct usage, especially if new methods like set_interaction_listener() have been introduced.