I was surprised to find that Lua require isn’t supported on the playdate, and that we’re expected to use import instead.
Anyone know why that is? I expect there to be a good reason for it, but haven’t been able to find it.
I was surprised to find that Lua require isn’t supported on the playdate, and that we’re expected to use import instead.
Anyone know why that is? I expect there to be a good reason for it, but haven’t been able to find it.
I wouldn't say it's a good reason per se, but the main reason is that we don't have a complete Lua runtime. In particular, we don't have package, os, or io because we're not running on an OS that provides the support for those.
Is there something you'd normally use require for and can't figure out how to get it done on Playdate? playdate.file.load() and playdate.file.run() allow for loading (compiled) code at runtime, might be useful here.
I have two gripes with the current lack of require support:
import, so they’re of limited use. This degrades the developer experience for me.import.I’ve also recently been experimenting with the fennelprogramming language, which is a lisp that compiles to lua. If you compile fennel with –require-as-include, it will turn an expression equal to local test = require(“map1”) into:
package.preload["map1"] = package.preload["map1"] or function(...)
<source of map1.lua>
end
local test = require("map1")
If it cannot determine the source of the module at compile time, compilation will fail.
Of course, this doesn’t work as neither package.preload or require is supported by playdate’s lua runtime, even if the above should work fine without os and filesystem apis.
Maybe this could be an inspiration on how to support require in playdate?
In any case, supporting require would measurably improve the dev experience. At least for me.