[英]Luaj attempt to index ? (a function value)
I am trying to compile Lua code that has two functions which I want to invoke and get some information from but when I use invokemethod on the LuaValue object, I get this error 我正在尝试编译具有两个函数的Lua代码,我想调用它们并从中获取一些信息,但是当我在LuaValue对象上使用invokemethod时,出现此错误
LuaError: attempt to index ?
LuaError:尝试索引? (a function value)
(函数值)
The code is inside a LuaScript class I created for convenience 该代码位于我为方便起见而创建的LuaScript类中
This method is first called to compile the file 首先调用此方法来编译文件
public void compile(File file) {
try {
Globals globals = JmePlatform.standardGlobals();
compiledcode = globals.load(new FileReader(file), "script");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
And then this is used to invoke the function getSameTiles
from my lua script 然后用于从我的lua脚本中调用函数
getSameTiles
public Object invoke(String func, Object... parameters) {
if (parameters != null && parameters.length > 0) {
LuaValue[] values = new LuaValue[parameters.length];
for (int i = 0; i < parameters.length; i++)
values[i] = CoerceJavaToLua.coerce(parameters[i]);
return compiledcode.invokemethod(func, LuaValue.listOf(values));
} else
return compiledcode.invokemethod(func);
}
The error LuaError: attempt to index ? (a function value)
错误
LuaError: attempt to index ? (a function value)
LuaError: attempt to index ? (a function value)
occurs at the line return compiledcode.invokemethod(func);
LuaError: attempt to index ? (a function value)
出现在return compiledcode.invokemethod(func);
的行中return compiledcode.invokemethod(func);
where "getSameTiles"
is passed as the string for func
其中
"getSameTiles"
作为func
的字符串传递
This is my Lua code 这是我的Lua代码
function getSameTiles()
--My code here
end
There are a couple of issues that needed fixing. 有几个问题需要解决。
Firstly, in lua, load()
returns a function which you'd then need to call to execute the script. 首先,在lua中,
load()
返回一个函数,然后您需要调用该函数来执行脚本。
Secondly, what the script does is add a function to the global table _G
. 其次,脚本要做的是向全局表
_G
添加一个函数。 In order to invoke that function you'll need to get the function from the Globals
table and call that. 为了调用该函数,您需要从
Globals
表中获取该函数并进行调用。
The following code does this 以下代码执行此操作
Globals globals = JmePlatform.standardGlobals();
public void compile(File file) {
try {
globals.load(new FileReader(file), "script").call();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public Object invoke(String func, Object... parameters) {
if (parameters != null && parameters.length > 0) {
LuaValue[] values = new LuaValue[parameters.length];
for (int i = 0; i < parameters.length; i++)
values[i] = CoerceJavaToLua.coerce(parameters[i]);
return globals.get(func).call(LuaValue.listOf(values));
} else
return globals.get(func).call();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.