C++ 多Lua环境问题
我在创建多个Lua环境以在同一Lua_状态下运行多个类似脚本时遇到问题。 程序崩溃,出现以下错误C++ 多Lua环境问题,c++,c,lua,luabind,C++,C,Lua,Luabind,我在创建多个Lua环境以在同一Lua_状态下运行多个类似脚本时遇到问题。 程序崩溃,出现以下错误 PANIC: unprotected error in call to Lua API (attempt to index a nil value) 下面是Script.Compile和Script.runFunc方法: void Script::Compile() { if (m_filename.IsEmpty()) return; if (m_filename.Contai
PANIC: unprotected error in call to Lua API (attempt to index a nil value)
下面是Script.Compile和Script.runFunc方法:
void Script::Compile() {
if (m_filename.IsEmpty()) return;
if (m_filename.Contains("res:")) {
astd::String file = m_filename.Replace("res:", "");
astd::String filecontent = AEIO::OpenTextFromAssetPack(file);
m_lua->RunString(filecontent);
} else {
m_lua->RunScript(m_filename);
}
astd::String id = astd::String("file") + astd::String(UID);
lua_setfield(m_lua->State(), LUA_REGISTRYINDEX, id.CStr());
UID++;
}
void Script::runFunc(astd::String func) {
astd::String id = astd::String("file") + astd::String(UID);
lua_State* state = m_lua->State();
// Weird behaviour during debug.
// The debugger goes back and forth on these 2 lines.
lua_getfield(state, LUA_REGISTRYINDEX, id.CStr());
lua_getfield(state, -1, func.CStr()); // CRASH!
if (lua_isfunction(state, -1)) {
lua_pcall(state, 0, 0, 0);
}
luabind::globals(state)["self"] = m_owner;
}
这是脚本的基本结构:
print("Script Created") --Gets printed before the error occurs.
function onInit()
print("Script Initialized")
end
function onUpdate()
print("Script Update")
end
只有当我调用runFunc(“onInit”)
或runFunc(“onUpdate”)
时,它才会失败
提前感谢。(1)您几乎肯定需要显示比这更多的代码,如MWE,才能真正了解。(2) 如果我是你,我会尝试在这两行之间插入调试输出,并检查
lua_istable(state,-1)
的输出。如果顶部不是表,这是最简单的原因,lua_getfield(state,-1,func.CStr())代码>会崩溃。是的,lua_________________________________。我不明白,因为这段代码以前是可以工作的:你说“显示比这更多的代码,比如MWE”是什么意思?(1)“MWE”是“最小的工作示例”,我可以从问题中复制,粘贴到文本文件中,然后编译以再现问题。(2) 如果lua_istable(state,-1)
返回false,那么这绝对是一个阻碍:It’很幸运,请不要将答案编辑到问题中。你的答案应该作为答案贴在下面。