Windows 10 x64、MSVC 2017、LuaJIT 2.0.5。
我在网上搜索了一下,但答案没有帮助。
基本上,我正在尝试遵循this manual,除了我必须将#include <LuaBridge.h>放在Lua include之后,否则说LuaBridge应该放在Lua include之后是行不通的。
Hovewher,我得到了以下错误: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.lua:
testString = "LuaBridge works!"
number = 42发布于 2018-11-03 06:06:28
教程中的代码有问题。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头之前!
https://stackoverflow.com/questions/53112327
复制相似问题