【发布时间】:2014-09-27 00:26:06
【问题描述】:
我一直在阅读一些关于 luabridge 的教程,用于从 lua 访问 c++,但我遇到了一个问题,我似乎无法通过搜索 google 找到答案。
我已经设置了一个示例程序来运行脚本并从 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 调用,我得到了脚本的预期。
是否有什么明显的东西我错过了,因为我没有找到类似的东西?
非常感谢所有帮助!
【问题讨论】:
-
你能在调试器中运行它并提供完整的回溯吗?
-
这就是你想要的吗? pastebin.com/eEbJWQ5n
-
script.lua中有什么内容? -
@Linuxxon:无需编辑您的问题标题即可说明已解决。相反,将其中一个答案标记为已接受。顺便说一句,接受你自己的答案是完全可以的。