LuaBridge assertion error

1.2k Views Asked by At

I've been going through some tutorials around luabridge for accessing c++ from lua but I've hit a problem I can't seem to find an answer to by searching google.

I've setup a sample program to run the script and access lua from c++, which works fine. But when I try to register a function in the global namespace it complains during runtime - compiling just fine.

#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;
}

So, this code with the addFunction call gives me this error

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.

Withouth the addFunction call I get what is expected of the script.

Is there perhaps something obvious that I have missed, since I haven't found anything like it?

I would greatly appreciate all help!

1

There are 1 best solutions below

1
On BEST ANSWER

The problem was that the

luaL_openlibs(L);

was after the script had been run. The problem was in the tutorial I was following, I encountered the same thing before in the beginning of my tries with lua.

It all works perfectly by calling it after creating the new lua state.