1
votes

I've been writing Lua integration into a game project, and frustratingly, every time I try and use lua_pcall, I get the error "attempting to call nil value."

When I try to look up the error, most of the examples are of people who have forgotten to use pcall after loading their lua file, or some error down the line. But for me, it's as soon as I even attempt to load the lua standard libs. I don't understand what's going wrong with so little code to show for it. Other things I've tried are skipping openLibs and going straight to loading my file with doFile, but even in that case, anything I do at all will result in a nil error.

Any help is greatly appreciated.

EDIT:

Thanks to some help I determined that I didn't need to do a pcall for luaL_openlibs, but as I wrote above, trying to run any function at all results in errors. In this case, it can't find "init" in the global lua namespace

c++

void LuaScriptingInterface::init()
{
    m_luaState = std::shared_ptr<lua_State>(luaL_newstate());

    luaL_openlibs(m_luaState.get());

    if (luaL_dostring(m_luaState.get(), m_script.c_str())) // script below
    {
        const char* errStr = lua_tostring(m_luaState.get(), -1);
        ASSERT(false, errStr ); // not hitting this case
    }

    lua_getglobal(m_luaState.get(), "init");
    if (!lua_isfunction(m_luaState.get(), -1))
    {
        ASSERT(false); // hit this case! Why can't we find "init"?
    }
}

lua

init function ()

end

update function (dt)

end

It doesn't do anything yet of course, but I expect it should at least be able to find the function.

1
It doesn't look like you're actually putting a function to call on the stack?! - Kerrek SB
oh! does luaL_openlibs not put the lib functions on the stack? maybe I just misunderstood how this works. - Duncan
How do you interpret the documentation, in particular the +0? - Kerrek SB
Is that saying that it doesn't change the stack position? - Duncan
Correct. And if you're not changing the stack position, then you haven't added anything to the stack! - Kerrek SB

1 Answers

0
votes

Hilariously the issue was in my FileReader code, there was an omitted newline character