3

I've recently updated my old Lua 5.1 project to the newest version of the library, and I'm having problems with LUA_GLOBALSINDEX - it became undefined. I only used it in lua_getfield functions, like so:

void luastartgame(void)
{
    if(startgamefunction.empty())return ;
    lua_getfield(globalL, LUA_GLOBALSINDEX, startgamefunction.c_str()); // go to function in Lua script
    int numArgs = 0;
    int res = lua_pcall(globalL,numArgs,0, 0);

    if(!luaresf(res)) // did the function call result in an error?
    {
        return;
    }
}

I tried replacing it with some constant integers - if it is something other than 0, my program crashes. If it is 0, it runs oddly, complaining about "attempting to access a nil value".

My source cose is available here. How should I handle the LUA_GLOBALSINDEX? What should I change it to?

ThePiachu
  • 8,695
  • 17
  • 65
  • 94
  • Are you talking about Lua 5.2, which is *not a backwards incompatible upgrade*? Also, the answer to your question can be found in the 5.2 documentation, [cunningly labeled "Changes to the API"](http://www.lua.org/manual/5.2/manual.html#8.3). – Nicol Bolas Jun 19 '12 at 01:35
  • See also http://stackoverflow.com/questions/10087226/lua-5-2-lua-globalsindex-alternative and http://stackoverflow.com/questions/9057943/porting-to-lua-5-2-lua-globalsindex-trouble – lhf Jun 19 '12 at 01:39

1 Answers1

3

Use lua_getglobal(globalL,startgamefunction.c_str()), which works in both 5.1 and 5.2.

lhf
  • 70,581
  • 9
  • 108
  • 149