lua-table

How to work with tables passed as an argument to a lua C function?

て烟熏妆下的殇ゞ 提交于 2019-11-30 21:55:30
I'm going to implement a function with C language and which will be called by Lua script. This function should receive a lua table as the argument, so I should read the fields in the table.I try to do like below, but my function is crashing when I run it. Can anyone help my find the problem? /* function findImage(options) imagePath = options.imagePath fuzzy = options.fuzzy ignoreColor = options.ignoreColor; end Call Example: findImage {imagePath="/var/image.png", fuzzy=0.5, ignoreColor=0xffffff} */ // implement the function by C language static int findImgProxy(lua_State *L) { luaL_checktype(L

An Interesting phenomenon of Lua's table

主宰稳场 提交于 2019-11-30 19:21:55
I'm new to Lua, and I'm learning the usage of table these days. From tutorials I know that Lua treats numeric indexed items and non-numeric indexed items differently, so I did some tests myself, and today I found an interesting phenomenon and I can't explain it: The code t = {1, 2, 3, a='a', b='b'} print(#t) gets 3 because the # operator counts numeric indexed items only. Then I tested the following code t = {1, 2, 3, a='a', b='b'} print(#t) for i = 100,200 do t[i] = i end print(#t) I get 3 3 Till now I think Lua treats those discontinuous items added later as non-numeric indexed ones. However

In Lua, how do you find out the key an object is stored in?

人盡茶涼 提交于 2019-11-30 09:23:36
问题 How would you print() out or find out the index of an object? For example, if I spawned 20 random rock objects on screen into an array RockTable = {}; Like this RockTable[#RockTable + 1] = rock; And all 20 rocks are displayed on screen how would I find out what key or index each rock has by clicking on them? I'm using Corona SDK. Any help would be greatly appreciated. 回答1: Invert the table: function table_invert(t) local u = { } for k, v in pairs(t) do u[v] = k end return u end You can then

Share Array between lua and C

五迷三道 提交于 2019-11-30 08:35:28
问题 I have really Googled this question but I never really got an solution. I want to share an Array between C and Lua, for performance I will avoid copying Arrays to and from Lua. So I want to pass a pointer to the Array from C to Lua. And then from Lua I want to set/modify values in this array directly. Example in C code I want to define my array int mydata[] = {1,2,3,4} set it global to access it from Lua with the name mydata . In Lua I want to change the values like this mydata[3] = 9 and

Iterate through Lua Table

﹥>﹥吖頭↗ 提交于 2019-11-30 07:10:40
问题 I am trying to iterate through a lua table but I keep getting this error: invalid key to 'next' I know that index starts off as -8 and I know that there is a table there because it gets the first (and only) value in it. However, it tries to loop round again even though I know there is only one string in the table. if (lua_istable(L, index)) { lua_pushnil(L); // This is needed for it to even get the first value index--; while (lua_next(L, index) != 0) { const char *item = luaL_checkstring(L,

An Interesting phenomenon of Lua's table

孤人 提交于 2019-11-30 03:39:49
问题 I'm new to Lua, and I'm learning the usage of table these days. From tutorials I know that Lua treats numeric indexed items and non-numeric indexed items differently, so I did some tests myself, and today I found an interesting phenomenon and I can't explain it: The code t = {1, 2, 3, a='a', b='b'} print(#t) gets 3 because the # operator counts numeric indexed items only. Then I tested the following code t = {1, 2, 3, a='a', b='b'} print(#t) for i = 100,200 do t[i] = i end print(#t) I get 3 3

How to check if a table contains an element in Lua?

折月煮酒 提交于 2019-11-29 19:41:07
Is there a method for checking if a table contains a value ? I have my own (naive) function, but I was wondering if something "official" exists for that ? Or something more efficient... function table.contains(table, element) for _, value in pairs(table) do if value == element then return true end end return false end By the way, the main reason I'm using this functions is to use tables as sets, ie with no duplicate elements. Is there something else I could use ? interjay You can put the values as the table's keys. For example: function addToSet(set, key) set[key] = true end function

table inside table in Lua

核能气质少年 提交于 2019-11-29 13:49:38
How can I get the data which is a table inside a table, i mean like this: t = { {a, b, c}, {d, e, f} }; if I write this line of code: print( t[1] ) the result will be —–>>> {a, b, c} BUT how can I print just the letter “a”? without using ipairs I mean is there any way to use something like t[1] ? Have you tried t[1][1] ? That should get you the first index in the table you get from t[1] 来源: https://stackoverflow.com/questions/6807969/table-inside-table-in-lua

Share Array between lua and C

你离开我真会死。 提交于 2019-11-29 06:58:38
I have really Googled this question but I never really got an solution. I want to share an Array between C and Lua, for performance I will avoid copying Arrays to and from Lua. So I want to pass a pointer to the Array from C to Lua. And then from Lua I want to set/modify values in this array directly. Example in C code I want to define my array int mydata[] = {1,2,3,4} set it global to access it from Lua with the name mydata . In Lua I want to change the values like this mydata[3] = 9 and when I return to C, mydata[3] is 9 because it is a pointer to the array. How is this possible? You can

Iterate through Lua Table

自闭症网瘾萝莉.ら 提交于 2019-11-29 01:30:23
I am trying to iterate through a lua table but I keep getting this error: invalid key to 'next' I know that index starts off as -8 and I know that there is a table there because it gets the first (and only) value in it. However, it tries to loop round again even though I know there is only one string in the table. if (lua_istable(L, index)) { lua_pushnil(L); // This is needed for it to even get the first value index--; while (lua_next(L, index) != 0) { const char *item = luaL_checkstring(L, -1); lua_pop(L, 1); printf("%s\n", item); } } else { luaL_typerror(L, index, "string table"); } Any help