I’ve embedded Lua into my C application, and am trying to figure out why a table created in my C code via:
lua_createtable(L, 0, numObjects);
and returned to Lua, will produce a result of zero when I call the following:
print("Num entries", table.getn(data))
(Where “data” is the table created by lua_createtable above)
There’s clearly data in the table, as I can walk over each entry (string : userdata) pair via:
for key, val in pairs(data) do
...
end
But why does table.getn(data) return zero? Do I need to insert something into the meta of the table when I create it with lua_createtable? I’ve been looking at examples of lua_createtable use, and I haven’t seen this done anywhere….
table.getn(which you shouldn’t be using in Lua 5.1+. Use the length operator#) returns the number of elements in the array part of the table.The array part is every key that starts with the number 1 and increases up until the first value that is
nil(not present). If all of your keys are strings, then the size of the array part of your table is 0.