LoveJS Player

Yet another lovejs player. This player is based on Love 11.5 and Emscription 3.1.68. It outputs a single html file that contains the engine and game you pass in.

Parameters

You can parameterize the output file by providing lovejs.json in the top level of your .love file

You can customize the HEIGHT and WIDTH of the default html shell using the appropriate tags, or you can provide your own custom html shell.

Note that if you are passing in your own html shell or style sheet those files should be present in your .love file and their paths should be provided by STYLE_FILE and HTML_FILE.

Currently only love115-compat-beta1 is the only available version of love.

Recommendations on further customizable parameters are welcome.

{
   "TITLE":"Your Game",
   "WIDTH":1280,
   "HEIGHT":720,
   "VERSION":"love115-compat-beta1",
   "STYLE_FILE":"/path/to/style.css",
   "HTML_FILE":"/path/to/index.html"
}

Extensions

This bundler extends Love2D to accept incoming signals from the JS environment.

JS: love_send_event

Sending Signals from JS to Lua

love_send_event(key-string,value-string, value-number) => nil

To create an event "key" with arguments "value" and 10 call the following from javascript.

Note the types are fixed, the first two variables must be strings and the last must be a number. This is a limitation of the SDL_UserEvent struct. This may be ameliorated in the future by replacing the event integration with polling.

LoveState['love_send_event']("key","value",10)

On the love2d side you can catch this event using an event handler

function love.handlers.key (value, code)
    print(value)
    print(code)
end

Lua: js.call

Calling JS from Lua, no returns

js.call(js-string) => nil

You can use js.call to execute a js string. No return value is provided. Coupling this with love_send_event lets you create asynchronous callbacks into the js environment.

Be careful, there are no checks on the string being executed. Errors may crash your game.

-- js is preloaded so it can be required anywhere
local js = require ("js")
js.call("console.log(\"hello\");")

Lua: js.send-event

Send event to canvas in JS environment

js["send-event"](event-name-string) => nil

js["send-event"] sends an event that can be picked up by event listeners attached to the canvas element. No return value is provided. Coupling this with love_send_event lets you create asynchronous callbacks into the js environment.

-- js is preloaded so it can be required anywhere
local js = require ("js")
js["send-event"]("game-start")
function gameStartEvent (_e){
    console.log("Game Start!");
}

LoveState["canvas"].addEventListener("game-start",gameStartEvent);

Lua: js.send-custom-json-event

Send event to canvas in JS environment with a custom json object

js["send-custom-json-event"](event-name-string, json-string) => nil

js["send-custom-json-event"] sends an event that can be picked up by event listeners attached to the canvas element. A custom json object will be available under the events detail key. No return value is provided. Coupling this with love_send_event lets you create asynchronous callbacks into the js environment. You must format the outgoing json-string as json or there will be an error in the JS environment.

-- js is preloaded so it can be required anywhere
local js = require ("js")
-- load your own json library
local json = require ("lib.json")
local message = {}
message["name"] = "Test Name"
js["send-custom-json-event"]("game-start",json.encode(message))
function gameStartEvent (e){
    console.log(e.detail.name);
}

LoveState["canvas"].addEventListener("game-start",gameStartEvent);

Lua: websocket.supported

Check if websockets are supported

websocket.supported() => bool


Lua: websocket.new

Open a new websocket

websocket.new(handle-string, address-string) => WS


Lua: websocket.deinitialize

websocket.deinitalize() => nil


Lua: WS.send_utf8_text

Send a string over the websocket

WS:send_utf8_text(utf8text-string) => nil


Lua: WS.send_close

Send close event over the websocket

WS:send_close(code-number,reason-string) => nil


Lua: WS.delete

Delete websocket

WS:delete() => nil


Lua: WS.get_protocol

Get the websocket protocol

WS:get_protocol() => string


Lua: WS.get_url

Get websocket connected URL

WS:get_url() => string


Lua: WS.get_extensions

Get websocket extensions

WS:get_extensions() => string


Lua: WS.get_ready_state

Get websocket ready state

WS:get_ready_state() => number


Websocket Events

Receiving signals via websockets

WS:get_ready_state() => number

Incoming websocket events are passed into Love2D via the event system.

There are four events passed in, websocketmessage, websocketopen, websocketerror, and websocketclose. Each event passes in a message (string) and a code (number). Write your own handlers to handle these incoming events.

The handle string passed to websockets.new will prepend the name of the four events generated by the websocket system.

-- websocket is preloaded so may be required anywhere
local websocket = require("websocket")

-- create a new websocket with handle "myhandle-" connected
-- to the your localhost port 9000
local WS = websocket.new("myhandle-","ws://localhost:9000")

love["handlers"]["myhandle-websocketopen"] = function (message)
    print(message) -- message for websocketopen should always be an empty string
    WS.send_utf8_text ("connected!!")
end

love["handlers"]["myhandle-websocketmessage"] = function (message)
    print(message) -- incoming message from server
end


love["handlers"]["myhandle-websocketclose"] = function (message)
    print(message) -- json formatted message to be parsed
end

love["handlers"]["myhandle-websocketerror"] = function (message)
    print(message) -- message from websocketerror is an empty string.
end