我正在尝试使用LuaJ在Java程序中调用lua函数.当我没有向闭包传递任何参数时,它工作正常:
String script = "print 'Hello World!'";
InputStream input = new ByteArrayInputStream(script.getBytes());
Prototype prototype = LuaC.compile(input, "script");
LuaValue globals = JsePlatform.standardGlobals();
LuaClosure closure = new LuaClosure(prototype, globals);
closure.call();
Run Code Online (Sandbox Code Playgroud)
但是现在我正在尝试一个带有顶级函数的lua脚本,该函数接受一个参数而我无法弄清楚如何从Java中传入参数.这是我到目前为止所得到的:
String script = "function something(argument)\n"+
"test_string = 'Hello World!'\n"+
"print(test_string)\n"+
"print(argument)\n"+
"end";
InputStream input = new ByteArrayInputStream(script.getBytes());
Prototype prototype = LuaC.compile(input, "script");
LuaValue globals = JsePlatform.standardGlobals();
LuaClosure closure = new LuaClosure(prototype, globals);
closure.invokemethod("something", CoerceJavaToLua.coerce("Foo"));
Run Code Online (Sandbox Code Playgroud)
这会导致invoke方法行出现异常:
org.luaj.vm2.LuaError:尝试索引?(函数值)
谢谢你的帮助!
在我的 libgdx 游戏中,我有用于地图和玩家对象的 3D BoundingBox 和球体。我想计算它们是否相互碰撞,以便正确模拟这些物体的运动。我可以用什么方法来计算这些物体是否碰撞/相交?