我有以下lua代码,在an online interpreter中运行良好:
__sprite_properties = {
events = {}
}
function bind_event(event_name, fun)
table.insert(__sprite_properties.events, { event_name, fun })
print(__sprite_properties.events[1][1])
end
foo = function()
return 0
end
bind_event("foo_event", foo)
>> foo_event
但是当我尝试使用sol2库在C中加载和运行脚本时,我在 table.insert
语句中遇到以下错误:
script.lua:6:尝试索引全局'table'(一个零值)堆栈回溯:script.lua:6:在函数'bind_event'中 - script.lua:13:在主块中
我正在使用LuaJIT作为lua发行版 . 用于在lua中加载脚本的代码是以下代码段:
sol::state lua;
lua.open_libraries(sol::lib::base);
try {
lua.safe_script_file("script.lua");
}
catch (const sol::error& e) {
std::cout << e.what() << std::endl;
}
为什么这个代码在c中加载时无法正常执行?
1 回答
您是否在C代码中加载了标准的Lua库?您似乎只加载了基本库,而不是表库: