In Love.js, can Lua functions be called from JS?
Posted: Thu Dec 30, 2021 8:37 pm
Hi, I think this is possible, but I'm not sure how easy or hard it is, so I thought I'd ask in case anyone else knows already.
If I define a function in the game that isn't called, can I call it from JavaScript after exporting to web with Love.js ? I made the example below as a test for the idea. Can I run toggle_circle() in the game from JavaScript in the browser or page?
Love file is at https://www.jimofleisure.com/trigger-fr ... om-js.love
Example is viewable at https://www.jimofleisure.com/trigger-from-js/
Edit: I'm starting to poke around. Basically I think I need to have Emscripten ensure that any Lua functions I want access to are exported, but because Lua is an embedded language I think there are probably some hoops to jump through to translate from Lua to Love2D's compiled WASM to an export pointer to trigger the function. My initial hopes were that the functions were auto-exported, but there's absolutely no reason to presume that would be the case.
Edit 2: The more I think this through, the more I think there probably isn't a reasonable path to having a Love2d Lua-defined function exported to WASM via Love.js export that function as invocable to the browser environment. First of all, Love2d has no built-in reason to export Lua functions anywhere, and the only functions it really needs invokable references to itself are love.load, love.update, and love.draw. Then there is the export by Love.js/Emscripten.
I had this little idea on having the user open up the dev console and make things happen kind of like Doki Doki Literature Club which has you open a file browser and delete some of its files as part of game play. Getting a dev console reference to a Love2D Lua function just doesn't seem reasonably doable without recompiling Love2d *and* tweaking Love.js .
I have another idea on how to influence the Love2d environment from the dev console, but it's kind of "out there", and I'm not sure it's feasible, either. Since I mentioned it, the idea is to see if I can swap lua file references while the game is running to alter behavior in-game. That's probably not going to work, either, as I can't imagine a reason Love2d would read a Lua file more than once per execution.
If I define a function in the game that isn't called, can I call it from JavaScript after exporting to web with Love.js ? I made the example below as a test for the idea. Can I run toggle_circle() in the game from JavaScript in the browser or page?
Love file is at https://www.jimofleisure.com/trigger-fr ... om-js.love
Example is viewable at https://www.jimofleisure.com/trigger-from-js/
Code: Select all
draw_circle = true
function love.load()
end
function love.update(dt)
end
function love.draw()
if draw_circle then
love.graphics.setColor(128, 128, 128)
love.graphics.circle("fill", 200, 200, 50)
end
end
function toggle_circle()
draw_circle = not draw_circle
end
Edit 2: The more I think this through, the more I think there probably isn't a reasonable path to having a Love2d Lua-defined function exported to WASM via Love.js export that function as invocable to the browser environment. First of all, Love2d has no built-in reason to export Lua functions anywhere, and the only functions it really needs invokable references to itself are love.load, love.update, and love.draw. Then there is the export by Love.js/Emscripten.
I had this little idea on having the user open up the dev console and make things happen kind of like Doki Doki Literature Club which has you open a file browser and delete some of its files as part of game play. Getting a dev console reference to a Love2D Lua function just doesn't seem reasonably doable without recompiling Love2d *and* tweaking Love.js .
I have another idea on how to influence the Love2d environment from the dev console, but it's kind of "out there", and I'm not sure it's feasible, either. Since I mentioned it, the idea is to see if I can swap lua file references while the game is running to alter behavior in-game. That's probably not going to work, either, as I can't imagine a reason Love2d would read a Lua file more than once per execution.