【发布时间】:2016-01-08 14:54:29
【问题描述】:
我有一张如下表:
ARQtable =
{
120, 250, 400, 500, 730,
790, 810, 935, 950, 999,
}
我正在尝试使用 c 读取 lua 中的表格。需要应用将在函数ARQSystem-> arqtable中生成的值
但是在阅读表格时我遇到了错误:PANIC: unprotected error in call to Lua API (attempt to call a nil value)
我的代码如下:
void read_table(void){
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
int val = 2000, i = 0;
if (luaL_loadfile(L, "tables.lua") || lua_pcall(L, 0, 0, 0))
{
ShowError("Error reading 'tables.lua'\n");
return;
}
lua_getglobal(L, "ARQtable");
if (lua_type(L, -1) == LUA_TTABLE) {
for (i = 0; i < val; i++) {
lua_pushnumber(L, i);
lua_gettable(L, -2);
ARQSystem->arqtable[i] = lua_isnumber(L, -1);
lua_settop(L, -2);
}
}
lua_close(L);
printf("Read Table complete.\n");
}
我想知道为什么在阅读时会出现这个错误,我还是个正在学习的 lua 新手,有人可以帮助我吗?
【问题讨论】:
-
请在
lua_typeif 语句中使用LUA_T*常量之一,而不是幻数(5)。 -
您的
for-loop 缺少大括号。顺便说一句,lua_close不应该在里面。 -
好的,我做了引用的修复,但恐慌错误仍在继续。
-
请更新问题中的代码。