My Lua table is looking like this:
qt={
bid_number=20;
ask_number=20;
bid=table of 20 elements;
ask=table of 20 elemens;
}
So #qt=0
, I want to send this table to C++ dll and work with its fields. How can I do it? For now I can work in C++ dll only with such tables as tbl={a,b,c}
. I do it like this:
static int forLua_SumArray (lua_State* L) { // Get the length of the table (same as # operator in Lua)
int n = luaL_len(L, 1);
double sum = 0.0;
// For each index from 1 to n, get the table value as a number and add to sum
for (int i = 1; i <= n; ++i) {
lua_rawgeti(L, 1, i);
sum += lua_tonumber(L, -1);
lua_pop(L, 1);
}
lua_pushnumber(L, sum);
return 1;
}
Help me please to start working with more complex tables.
In the past I have used sol2 https://github.com/ThePhD/sol2 to make complexities in Lua/C++ interfaces much more straightforward.
Mike
Convert the Lua value at the given acceptable index to a C string. The Lua value must be a string or a number, otherwise, the function returns null.
const char *lua_tolstring (lua_State *L, int index, size_t *len);
then lua_tostring
also changes the actual value in the stack to a string. luaL_checkstring
calls lua_tolstring
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.