I figured out how to make Lua scripts require other Lua scripts in an Android environment.
However, that solution has the package.path configuration inside the top level Lua script itself. I want to move the package.path configuration to the Java code that runs the script instead, because the package.path is specific to the Android environment, while the script could be run anywhere.
Here is what I've tried:
InputStream input getAssets().open("lua/test/pathTest.lua");
Prototype prototype = LuaC.instance.compile(input, "pathTest.lua");
Globals globals = JsePlatform.standardGlobals();
globals.load(new BaseLib());
globals.load("package.path = package.path .. ';/assets/lua/?.lua;/assets/lua/test/?.lua'");
LuaClosure c = new LuaClosure(prototype, globals);
c.call();
I've also tried a few other forms like:
globals.load(new StringReader("package.path = package.path .. \";/assets/lua/?.lua;/assets/lua/test/?.lua\""), "pathTest.lua");
Or taking the LuaValue result from the load and passing that to the LuaClosure. They all result in the script failing at the first require
, not being able to find the referenced script.
Anyone have any ideas?
*edit - If anyone knows how to set the working directory to the /assets/lua/ folder, so I can use relative paths from there in the Lua script, that would be preferable.
Thought I had tried this before, but here's the solution.
InputStream input getAssets().open("lua/test/pathTest.lua");
Prototype prototype = LuaC.instance.compile(input, "pathTest.lua");
Globals globals = JsePlatform.standardGlobals();
globals.load(new BaseLib());
globals.load(new StringReader("package.path = '/assets/lua/?.lua;'"), "initAndroidPath").call();
LuaClosure c = new LuaClosure(prototype, globals);
c.call();
That will initialize the Android environment's path to the assets/lua/ folder. This allows me to use relative paths in the require
call in the Lua script.
For example, to require a file in subdirectory: assets/lua/test/luaScript.lua
The Lua require would look like:
require "test.luaFile"