## In Love.js, can Lua functions be called from JS?

Discuss any ports of LÖVE to different platforms.
JimOfLeisure
Prole
Posts: 9
Joined: Wed Dec 29, 2021 8:01 pm

### In Love.js, can Lua functions be called from JS?

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/

Code: Select all

draw_circle = true

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: 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.
ivan
Party member
Posts: 1757
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: In Love.js, can Lua functions be called from JS?

Hello. It's an interesting idea although to be honest I'm not sure if it is good from a design standpoint. It is not good practice to have multiple programming languages being used in the same file (for example a single PHP file containing SQL and JavaScript). It becomes very hard to maintain code in this way.
In a recent update to the love.js standalone player, I added the option to pass URL arguments to Lua:
https://github.com/2dengine/love.js
So it would look something like:

Code: Select all

https://example.com/love.js/?g=./mygame.love&arg=1,2,3
You can catch the arguments from Lua like so:

Code: Select all

for i, v in pairs(arg) do
print(i,v)
end
JimOfLeisure
Prole
Posts: 9
Joined: Wed Dec 29, 2021 8:01 pm

### Re: In Love.js, can Lua functions be called from JS?

It's definitely not a "best practice". If I pursue the idea I'll probably just do it all in JS or something that compiles to WASM so I can explicitly export functions.

The URL thing is very interesting, though. I've been using the Davidobot Love.js so far; I just found out about yours and haven't yet figured out what the differences are.

Thanks for the info and your work on the project!
ivan
Party member
Posts: 1757
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### Re: In Love.js, can Lua functions be called from JS?

Thank you, but Davidobot and Tanner deserve most of the credit. The main difference is that the standalone version works directly with .love files without building your games. You just drop your .love file in the same folder and it should work.

### Who is online

Users browsing this forum: No registered users and 4 guests