You can compile Lua script files at runtime using:
Notice that you don't write the extension of the file, so:
Should be:
If your file is not in the root folder (or one of the default folders searched by require) then its:
Where you use the "." symbol to indicate a directory (instead of the typical "/" or "\\").
There's also "dofile" but I heard it's unsupported in Love2D.
Make sure your functions in the required file are global.
Otherwise they won't be accessible from your main file.
If your functions in the included file are NOT global, you can have several options.
You can return a "table" containing the functions from the included file or use the "module" keyword:
myfile.lua
Code: Select all
local function foo() end
local function bar() end
local interface = { foo = foo, bar = bar }
return interface
Your main file:
Code: Select all
interface = require("myfile")
interface.foo()
The module keyword:
myfile.lua
Code: Select all
module ("interface")
-- the following function are NOT global
function foo() end
function bar() end
Your main file:
These are just a couple of ways to avoid polluting the global namespace.