-1

I created a file with the following code

Request = {
  TimeAdd = true;
  DaysAdd = true;
};

The source code is made in C

    lua_getglobal(L, "Request")
    lua_getfield(L, -1, "TimeAdd");

       time_request = lua_toboolean(L, -1);

    lua_getfield(L, -1, "DaysAdd");
        data_request = lua_toboolean(L, -1);

I do the compilation of the program normally more it occurs error lua

LUA PANIC: unprotected error in call to Lua API (attempt to index boolean value)

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Chozie
  • 73
  • 1
  • 9

1 Answers1

2

Step through the code, visualizing the Lua stack as you go, and the error will become apparent.

lua_getglobal(L, "Request"); // Stack: [] -> [(Request table)]
lua_getfield(L, -1, "TimeAdd"); // [>(Request table)<] -> [(Request table), true]

time_request = lua_toboolean(L, -1); // [(Request table), >true<]

lua_getfield(L, -1, "DaysAdd"); // [(Request table), >true<] ERROR: Cannot index `true`

When you call lua_getfield(L, -1, "DaysAdd"), the top of the stack is the value true, which is not indexable.

Either pop the true value off the stack before getting DaysAdd (lua_pop(L, 1)) or adjust the stack index to lua_getfield to point to the Request table (lua_getfield(L, -2, "DaysAdd"))

Colonel Thirty Two
  • 23,953
  • 8
  • 45
  • 85
  • Thank you Colonel, it's working now. I'm new in lua, have some indication of some stuff for me to read more about respect? – Chozie Nov 04 '15 at 23:18
  • @Chozie If you haven't, try reading the Lua book. (The old version is available online at http://www.lua.org/pil/contents.html – slightly outdated by now, but still mostly relevant.) Also useful is reading the source of Lua libraries written in C, see e.g. the section "Libraries" of the [Lua source](http://www.lua.org/source/5.3/) for Lua's built-in libraries, or whatever other libraries you use or find via luarocks. (Not everything you'll find will be good code, but if you look at many different things you'll probably see some good code, too.) – nobody Nov 05 '15 at 11:50