how i can call lua function (with string argument) from JS code? Defold
- User
how i can call lua function (with string argument) from JS code? Defold
- Assistant
how i can call lua function (with string argument) from JS code? Defold
Defold is a game engine that uses Lua for scripting. To call a Lua function from JavaScript in the context of a Defold game, you would typically use the Defold HTML5 messaging system. This allows you to send messages between the JavaScript context and the Defold/Lua game context.
Here's how you can do it:
Create a Lua module in Defold with the function you want to call from JavaScript:
-- my_module.lua
local M = {}
function M.my_function(input_string)
print("Lua function called with input: " .. input_string)
-- ... do something with the input_string ...
end
return M
In a script attached to a game object, require this module and register a message handler:
-- main.script
local my_module = require "path.to.my_module"
function init(self)
-- Register a message handler for "call_lua_function"
msg.post(".", "acquire_input_focus")
end
function on_message(self, message_id, message, sender)
if message_id == hash("call_lua_function") and message.input_string then
my_module.my_function(message.input_string)
end
end
First, you'll need to build your Defold game to HTML5. After that, you can include the published game on a webpage and interact with it via JavaScript.
Let's say you have an HTML file with your Defold game embedded:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Defold Game</title>
</head>
<body>
<script src="dmloader.js" type="text/javascript"></script>
<canvas id="canvas"></canvas>
<script>
function startGame() {
// Initialize your game
let canvas = document.getElementById('canvas');
Module.start({canvas: canvas});
}
startGame();
function callLuaFunction(inputString) {
Module.onRuntimeInitialized = function() {
// Send a message to the Defold game
var message = {
input_string: inputString
};
Module.ccall('EXT_ExternalCall', // Name of the C function to call
null, // Return type
['string', 'string'], // Argument types
['call_lua_function', JSON.stringify(message)] // Arguments
);
}
}
// Call this function when you want to invoke the Lua function
callLuaFunction("Hello from JavaScript!");
</script>
</body>
</html>
In this code:
dmloader.js file is the Defold HTML5 runtime loader script.Module.start({canvas: canvas}); line initializes the game with the given canvas.callLuaFunction function uses the EXT_ExternalCall function (which needs to be implemented in native C code or via Emscripten's JavaScript bindings) to send a message to the Lua context.Module.onRuntimeInitialized.Please note that communication with the Defold game engine from JavaScript might require custom native extension code if the existing message-passing capabilities do not cover your needs. The EXT_ExternalCall function is just a placeholder for demonstrating the concept, and you will likely need to implement the actual C function that interfaces with the Defold engine's message-passing system in your native extension code.
As of my knowledge cutoff in early 2023, there might be updates or newer methods available, so always check the latest Defold documentation and community forums for updates.
Not yet included in a published release.