Windows 10 x64,MSVC 2017,LuaJIT 2.0.5。
我在网上搜索了一下,但答案没有帮助。
基本上,我试图遵循这本手册,除了我必须将#include <LuaBridge.h>
放在Lua-includes之后,因为否则说LuaBridge应该放在Lua includes之后是行不通的。
悬停时,我得到以下错误:PANIC: unprotected error in call to Lua API (attempt to call a nil value)
。
我不知道为什么。如果你需要更多信息,就说吧。
#include "stdafx.h"
#include <iostream>
#include <lua.hpp>
#include <LuaBridge/LuaBridge.h>
using namespace luabridge;
using namespace std;
int main()
{
lua_State* L = luaL_newstate();
luaL_dofile(L, "script.lua");
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
string luaString = s.cast<string>();
int answer = n.cast<int>();
cout << luaString << endl;
cout << "And here's our number:" << answer << endl;
system("pause");
return 0;
}
script.loa:
testString = "LuaBridge works!"
number = 42
教程中的代码有错误。lua_pcall
没有什么可调用的,因为luaL_dofile
和luaL_openlibs
没有将函数推送到堆栈上,所以它尝试调用nil
并返回2
(宏LUA_ERRRUN
的值(。
我通过更改教程中的代码并使用g++进行编译来验证这一点。无论出于什么原因,我都没有犯恐慌性错误;可能是因为它使用的是Lua 5.3:
#include <iostream>
extern "C" {
# include "lua.h"
# include "lauxlib.h"
# include "lualib.h"
}
#include <LuaBridge/LuaBridge.h>
using namespace luabridge;
int main() {
lua_State* L = luaL_newstate();
luaL_dofile(L, "script.lua");
std::cout << "type of value at top of stack: " << luaL_typename(L, -1) << std::endl;
luaL_openlibs(L);
std::cout << "type of value at top of stack: " << luaL_typename(L, -1) << std::endl;
std::cout << "result of pcall: " << lua_pcall(L, 0, 0, 0) << std::endl; // Print return value of lua_pcall. This prints 2.
LuaRef s = getGlobal(L, "testString");
LuaRef n = getGlobal(L, "number");
std::string luaString = s.cast<std::string>();
int answer = n.cast<int>();
std::cout << luaString << std::endl;
std::cout << "And here's our number: " << answer << std::endl;
}
正如您所注意到的,该代码也是错误的,因为Lua标头必须包含在LuaBridge标头之前!