【发布时间】:2019-04-06 07:54:36
【问题描述】:
Windows 10 x64、MSVC 2017、LuaJIT 2.0.5。
我搜索了网络,但答案没有帮助。
基本上我正在尝试关注this manual,除了我必须在 Lua 包含之后放置 #include <LuaBridge.h>,否则说 LuaBridge 应该在 Lua 包含之后是行不通的。
但是,我收到以下错误: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
【问题讨论】:
-
你的代码是什么样的?
-
另外,你用的是
luaL_loadfile还是luaL_dofile? -
加了代码,我用的是
luaL_dofile -
你所有的 .lua 文件是否和你的 c++ 源代码和头文件在同一个目录中?
-
在
lua_pcall(L, 0, 0, 0);之前,您需要将一些东西压入堆栈(例如,您将要调用的函数)。你的script.lua什么都不返回,所以堆栈中没有任何内容。