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

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

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

Post by JimOfLeisure »

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

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

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

Post by ivan »

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?

Post by JimOfLeisure »

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!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

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

Post by ivan »

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.
User avatar
alexjgriffith
Prole
Posts: 30
Joined: Wed Jan 20, 2021 9:50 pm

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

Post by alexjgriffith »

If you want to call something real simple from your web page in the love2d environment check out Love.js-API-Player. Specifically the the override of console.log in consolewrapper.

If you get this puppy up and running you can write something like

Code: Select all

console.log("callJavascriptFunction setfullscreen()")
on your javascript side and have it run

Code: Select all

setfullscreen()
on your lua side.

While this is not the intended purpose of the API Player, it works! And, is really helpful if you want to have a button on a webpage that impacts the state of your game.

https://github.com/MrcSnm/Love.js-Api-P ... wrapper.js
Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests