有很多关于这种错误的帖子,大多数人说它与表和数组索引问题有关。但我根本没有使用表,我只是想调用我制作的库函数,我得到了这个错误。这是从java调用的lua脚本:
String script = new String (
"function event_touch ( )"
+ " pb.open_form ('view_monster');"
+ " print ('I ran the lua script');"
+ "end");
PBLUAengine.run_script(script, "event_touch");
捕获异常时,这给了我以下错误:
“function event_touch () pb.open_form ('view_monster'); print ('I run the lua script');end:1 尝试索引?(一个零值)”
run_script() 函数像这样调用脚本(我使用的是 luaj):
public static LuaValue run_script ( String script )
{
try
{
LuaValue chunk = s_globals.load( script );
return chunk.call();
}
catch ( Exception e)
{
Gdx.app.error ("PBLUAengine.run_script()", e.getMessage() );
}
return null;
}
库方法是这样的,当从 java 调用时,同样的代码可以工作:
static class open_form extends OneArgFunction
{
public LuaValue call (LuaValue formname)
{
String tmpstr = (String ) CoerceLuaToJava.coerce(formname, java.lang.String.class );
try
{
PBscreen_game.hide_subscreen(PBscreen_game.MENU_SUBS);
PBscreen_game.show_subscreen ( PBscreen_game.FORM_SUBS);
PBsubscreen_form.open_form ( new PBform_regular ( tmpstr ) );
}
catch (Exception e)
{
Gdx.app.error("PBLUAlibrary.open_form", e.getMessage());
}
return valueOf ( 0 );
}
}
它基本上将 lua 参数转换为字符串,创建一个新参数并传入参数字符串。
库函数的声明如下:
public LuaValue call( LuaValue modname, LuaValue env )
{
LuaValue library = tableOf();
library.set( "open_form", new open_form() );
library.set( "open_system_form", new open_system_form() );
env.set( "pb", library );
return library;
}
这可能是我在整个系统中可以看到的唯一“表”。这通常用于将正确的类与正确的函数名称联系起来。
有人有想法吗?