int count = lua_len(L, -1); // how to get the member count of ignoreColor array int colors[count]; for (int i=0; i count; i++) { colors[i] = luaL_checkinteger(L, -1-i); }
The code segment for this seems incorrect (not paying attention to the missing comparison operator in the loop). The correct function to get the length of the lua_objlen table. It looks like you are trying to get numbers from "ignoreColor", but first don't push them onto the stack. As a result, luaL_checkinteger(L, -1-i); ends access to invalid indexes on the stack.
You probably want something closer to this, for example:
int count = lua_objlen(L, -1); std::vector<int> colors(count); for (int i = 0; i < count; lua_pop(L, 1)) { lua_rawgeti(L, 4, ++i); colors.push_back( luaL_checkinteger(L, -1) ); }
If you are using Lua 5.2, replace lua_objlen with:
int count = lua_rawlen(L, -1);
Make sure there is enough space on the stack if you are moving a lot of items from the table. eg. lua_checkstack
source share