Love.js Easy API Player

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Hipreme
Prole
Posts: 15
Joined: Wed Jun 03, 2020 9:57 pm
Contact:

Love.js Easy API Player

Post by Hipreme »

Hello guys, I know how the Love.js is quite innactive, but I'm using it anyway and today I could integrate it with kongregate's api and call the api from inside .lua files

How to
- It is really simple to use, you just need to follow the readme from the repo and call the function from the api provider
- Example

Code: Select all

if(key == "p") then
        callJS("kongregate.stats.submit('Score', 1000);")
    end
Here's the result:

https://github.com/MrcSnm/Love.js-Api-Player
Attachments
apiTest.png
apiTest.png (6.33 KiB) Viewed 10017 times
Hipreme
Prole
Posts: 15
Joined: Wed Jun 03, 2020 9:57 pm
Contact:

Re: Love.js Easy API Player

Post by Hipreme »

Passing by to update that the Lib now supports retrieving data that came from Javascript, it as simple as doing the the post-only requests, firstly , in your main.lua file

Code: Select all

require 'js'
After that, you just need to call

Code: Select all

gUsername = ""
JS.newRequest('kongregate.services.getUsername()', 
function(data)
    gUsername = data
end)
That will enqueue the request, for actually completing it, you will need to update the request status, you do that by append to your love.update the following code

Code: Select all

if(retrieveData(dt)) then
    return
end
Doing so will make your code synchronous, but as I show here, it is fairly easy to make it assync

Every request has a timeout value, defaulted to 2 seconds, you can pass the timeout value in the same func, the entire func prototype is

Code: Select all

JS.newRequest(funcToCall, onDataLoaded, onError, timeout, optionalId)
There is a function in error case(timeout), this function can be nil or it can be set to a default function by calling

Code: Select all

JS.setDefaultErrorFunction(errorFunc)
It receives a function that receives the id that generated the error, the Id can either be a string or number(default and incrementable)
abhimonk
Prole
Posts: 6
Joined: Sat Sep 25, 2021 3:30 pm

Re: Love.js Easy API Player

Post by abhimonk »

Hi, thanks for posting this. Sorry to revive a very old thread. Overridding the JS console is super clever, I tried your technique so I could run ArmorGames API function calls from my love.js game and it worked great for a Love2D->JS call.

However, I'm running into issues with the reverse: Requesting data (JS->Love2D). From what I understand of your repo, it looks like when you request data, you write to an IndexedDB with the return value of the user's JS function call. Then we loop in Love2D until that file is populated, and read from that file. The crux here is that: The indexedDB written to by the custom JS must match the indexedDB that love.js writes to.

It looks like the code in your repo creates a database called "__LuaJSDB" in "js.lua", but the current implementation of love.js creates an indexedDB database called "/home/web_users/love". As a result, I can't access the data written by JavaScript. My first question here is: Do you know of a way to make love.js point to a different database for its file IO?
Either way, I thought I could just update this to use the "/home/web_users/love" database to match love.js.

However, even if I update the code in js.lua to use the "/home/web_users/love" database, Love2D still seems unable to access any entries created in the database by webdb.js (the file isn't found, I get 'nil' when using love.filesystem.getInfo on the __temp0 files created by webdb.js).

Similarly, if I write a file using love.filesystem.write, this creates an entry in /home/web_users/love database in the browser. If I then try to use standard JavaScript apis to read from this database, it also doesn't seem to work (I get an error stating that the result of the transaction is undefined, basically the key isn't in the database and the file isn't found).

Do you have any pointers on getting this to work? I think I'm just missing something with the IndexeDB calls. When I look at chrome devtools at the database, I don't see any entries populated by love.js when doing fileIO, and the love.js filesystem can't see the entries I add using my own custom javascript or your webdb.js.

Thanks in advance, sorry for asking such a long question!

TL;DR:
Love2D->JS works great (callJS function works well!)

However if I do this:
JS->write to indexedDB -> try to access indexedDB using Love.js file IO
love.js/love2d is unable to find the __temp file created by the JS code
Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests