从需要在Android上使用LuaJ一个Lua脚本中的其他Lua脚本 [英] Requiring other lua scripts from within a Lua script using LuaJ on Android

查看:1658
本文介绍了从需要在Android上使用LuaJ一个Lua脚本中的其他Lua脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从Java通过LuaJ在Android上需要其他Lua脚本故障投诉Lua脚本。我认为它的东西与我的当前工作目录。

I'm having trouble calling Lua scripts from Java via LuaJ on Android that require other Lua scripts. I think it's something to do with my current working directory.

我试图用Java:

InputStream input = EvilApp.getContext().getAssets().open("lua/pathTest.lua");
Prototype p = LuaC.instance.compile(input, "pathTest.lua");
LuaValue g = JsePlatform.standardGlobals();
LuaClosure c = new LuaClosure(p, g);
c.call();

pathTest.lua:

pathTest.lua:

require "Foo"
local str = Foo.getString()
print(str)

对于这个特定的测试,无论是LUA文件是为简单起见同一目录中,但我需要其他LUA文件的相对路径。

For this specific test, both lua files are in the same directory for simplicity, but I will need relative paths to other lua files.

我试着打package.path,但没有我试过工作过。当运行Android,package.path ==?.lua默认情况下。

I've tried playing with package.path, but nothing I've tried has worked. When running in Android, package.path == "?.lua" by default.

我使用Koneki运行这个测试通过命令行相对路径,并在Eclipse中,他们做工精细。这是特别的那失败的Andr​​oid的环境。

I've run this test with relative paths via commandline, and inside Eclipse using Koneki, and they work fine. It's specifically the Android environment that's failing.

此外,我能够得到Lua脚本,没有需要在Android中很好地工作。

Also, I'm able to get Lua scripts with no requires to work fine in Android.

推荐答案

想通了一个解决方案。添加到package.path的绝对路径的工作。例如,我的Lua脚本都包含在我的资产文件夹中,因此增加:

Figured out one solution. Adding the absolute path to the package.path worked. For example, my Lua scripts are contained in my assets folder, so adding:

package.path = package.path .. ";/assets/lua/?.lua;"

在一个需要包含在文件/资产/ LUA / 的作品。

before a require for a file contained in /assets/lua/ works.

同样,除去最主要的反斜杠:

Similarly, removing the leading backslash:

package.path = package.path .. ";assets/lua/?.lua;"

也适用,因此它似乎LuaJ默认启动的Andr​​oid项目的根目录下的package.path。然而,我无法让其他相对路径工作,如:

also works, so it seems that LuaJ by default starts the package.path on the Android project's root directory. However, I was unable to get other relative paths to work, such as:

package.path = package.path .. ";assets/../assets/lua/?.lua;"

这篇关于从需要在Android上使用LuaJ一个Lua脚本中的其他Lua脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆