我一直在阅读一些关于 luabridge 的教程,以便从 lua 访问 c++,但我遇到了一个问题,我似乎无法通过搜索谷歌找到答案。
我已经设置了一个示例程序来运行脚本并从 C++ 访问 lua,它运行良好。但是当我尝试在全局命名空间中注册一个函数时,它会在运行时提示 - 编译正常。
#include <iostream>
#include <string>
#include "../LuaBridge/LuaBridge.h"
using namespace luabridge;
void printMessage(const std::string& s) {
std::cout << s << std::endl;
}
int main() {
lua_State* L = luaL_newstate();
getGlobalNamespace(L).addFunction("printMessage", printMessage);
luaL_dofile(L, "script.lua");
luaL_openlibs(L);
lua_pcall(L, 0, 0, 0);
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;
}
所以,这段代码与 addFunction 调用给了我这个错误
Lua: /home/linuxxon/Programming/kingdoms-online/Script/Lua/../LuaBridge/detail/Namespace.h:1080: luabridge::Namespace& luabridge::Namespace::addFunction(const char*, FP) [with FP = void (*)(const std::basic_string<char>&)]: Assertion `(lua_type(L, (-1)) == 5)' failed.
在没有调用 addFunction 的情况下,我得到了脚本的预期结果。
因为我没有找到类似的东西,是否可能有什么明显的东西被我遗漏了?
非常感谢所有帮助!
最佳答案
问题是
luaL_openlibs(L);
是在脚本运行之后。问题出在我正在学习的教程中,我之前在尝试使用 lua 时遇到过同样的事情。
在创建新的 lua 状态后调用它,这一切都完美地工作。
关于c++ - LuaBridge 断言错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25120227/