Lua脚本尝试调用nil

我有一个简单的Lua函数,我已经从在线源中拿来并修改它去修改另一个Lua文件中的特定行。当 PANIC: unprotected error in call to Lua API (attempt to call a nil value) 弹出我的控制台时,这个函数似乎正在尝试调用一个nil值。这个函数是由另一个C++函数调用的。我几乎没有关于Lua的经验,也不知道发生了什么。

脚本:

function ModLine(value, line)
    print("Modding...")
    local file = io.open("Image/DM2240_HighScore.lua", "r")
    sContents = file:read() -- 捕捉文件并保存在字符串中
    file:close()
    tContents = textutils.unserialize(sContents) -- 将字符串转换为表格

    --打印特定行
    print(tContents[line])

    --修改特定行
    table.remove(tContents, line) -- 将原行删除以便我们可以插入新行
    table.insert(tContents, line, value) -- 在表格中的特定行插入值。

    --将表格转换为字符串并保存文件
    sContents = textutils.serialize(tContents)
    file = io.open("Image/DM2240_HighScore.lua", "w")
    file:write(sContents)
    file:close()
end

提前感谢!

编辑: 我检查了一些函数并发现问题出在这个函数中,

void LuaInterface::ModLine(const int value, const int line) {
    cout << "Modded" << endl;
    lua_getglobal(theLuaState, "ModLine");
    lua_pushinteger(theLuaState, value);
    lua_pushinteger(theLuaState, line);
    lua_call(theLuaState, 2, 0);
}
点赞