Currently Lua is the only supported language for coding LÖVR projects.
But there are several languages available that
trans-compile into Lua.
This document covers only some of them (in alphabetical order):
:::note A dialect means the language is inspired by the parent but not fully syntax compatible. :::
In general there are two ways of using one of them in a LÖVR project:
:::note The more elegant and recommended solution is the just-in-time compilation:
(TODO: Is CSharp.lua really a dialect? It feels more like a full implementation.)
(TODO)
(TODO)
$ fennel --compile yourProject
(TODO: error line translation)
(TODO: Line Translation?)
Haxe does not offer runtime compilation.
Compiles the whole project:
$ moonc yourGame
(TODO: Write more about error line rewriting.)
Setup in conf.lua:
-- Check for moonscript/moonscript/init.lua.
-- The operating specific LPeg library must be at the right location as well.
local lua_path = '?.lua;?/init.lua;moonscript/?/init.lua;'
lovr.filesystem.setRequirePath(lua_path)
-- MoonScript rewrites and sets its require paths from package.path when first required.
-- If you want to alter it afterwards redefine package.moonpath.
package.path = lua_path
local moonscript = require'moonscript'
-- This loader uses lovr.filesystem instead of io.open.
-- Enables require() from LOVR's save directory and fuse mode.
local moon_loader = function(name)
local name_path = name:gsub("%.", "/")
local file, file_path
for path in package.moonpath:gmatch("[^;]+") do
file_path = path:gsub("?", name_path)
file = lovr.filesystem.read(file_path)
if file then break end
end
if file then
local res, err = moonscript.loadstring(file, "@" .. file_path)
if not res then error(file_path .. ": " .. err) end
return res
end
return nil, "Could not find moon file"
end
moonscript.remove_loader()
local loaders = package.loaders or package.searchers
table.insert(loaders, moon_loader)
-- This will require the first .lua or .moon file matching the require paths.
-- If both are present the Lua file is required.
require'myConf'
The main.lua file:
-- Requires either .moon or .lua module files.
-- If both are present the Lua one is required.
require'myMain'
(TODO: Write about error line translation.)
There is no jit-compiler available for Wu.
Use the -l
flag which enables commenting each line of the Lua file with the corresponding
line-number in the source file:
$ yue -l yourGame
A special error handler can extract this information and point to the correct line in the source file when an error is thrown.
(TODO: Write more about this error handler function. I guess some Yuescript function can be reused.)
Yuescript comes also (beside the standalone executable) in form of a Lua c library. You need to ship a different one for each operating system you want to support.
The Yuescript compiler takes care of the line-number translation, no extra work needed in this setup.
After the setup (in conf.lua) Lua and Yuescript modules can be required from each other in the usual way.
Setup in conf.lua:
require('yue')
-- Yuescript's uses of io.open need to be replaced with lovr.filesystem ones.
yue.file_exist = lovr.filesystem.isFile
yue.read_file = function(fname)
contents, bytes = lovr.filesystem.read(fname)
if contents == nil then
return nil, 'File not found.'
end
return contents
end
-- It makes sense to keep Lua and Yuescript search paths in sync.
yue.options.path = '?.yue;?/init.yue'
lovr.filesystem.setRequirePath('?.lua;?/init.lua')
-- Requires either yueConf.yue or yueConf/init.yue.
-- Use yue() instead of require() to enable error line translation.
-- In yueConf require() works like expected.
yue('yueConf')
The main.lua file:
-- Requires file yueMain.yue or yueMain/init.yue.
-- Use yue() instead of require() to enable error line translation.
-- In yueMain require() works like expected.
yue'yueMain'