LuaBridge断言错误

时间:2014-08-04 13:47:58

标签: c++ lua luabridge

我一直在浏览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调用,我得到了对脚本的期望。

是否有一些我错过的明显事实,因为我还没有找到类似的东西?

我将非常感谢所有的帮助!

1 个答案:

答案 0 :(得分:1)

问题在于

luaL_openlibs(L);

是在脚本运行之后。问题出现在我关注的教程中,我在尝试lua之前遇到了同样的事情。

在创建新的lua状态后调用它可以完美地工作。