在Java中执行Lua脚本

我正在尝试在Android Studio中执行Lua脚本,我使用了http://www.luaj.org/luaj/3.0/README.html中的Luaj库。

String script = "assets/hello.lua";
Globals globals = JsePlatform.standardGlobals();
LuaValue chunk = globals.loadfile(script);
m.setText(chunk.call(LuaValue.valueOf(script)).tojstring());

我在assets中使用了我的lua文件,我只是在我的lua文件中放了print("hello"),所以我应该在我的文本视图中得到hello作为结果,但我总是得到"nil",有没有解决方案?

点赞
用户7509065
用户7509065

print("hello") 将会打印出 "hello",然后返回 nil。从 chunk.call 中获取的值是你的 Lua 代码返回的值,而不是它打印出来的。

2019-09-16 20:49:45