lua-table

Using a coordinate pair as a key in a Lua table

旧城冷巷雨未停 提交于 2021-01-03 06:28:20
问题 As the title says, I'm trying to use a coordinate pair (x, y) as a key for a table. Here is what I have done so far local test = {_props = {}} local mt = {} local xMax = 5 local yMax = 5 local function coord2index(x, y) return ((x-1) * xMax) + y end mt.__index = function(s, k) if s._props[coord2index(k[1], k[2])] ~= nil then return s._props[coord2index(k[1], k[2])] end end mt.__newindex = function(s, k, v) s._props[coord2index(k[1], k[2])] = v end mt.__call = function (t, k) if type(k) ==

Check if Table contains a value in lua

[亡魂溺海] 提交于 2020-12-12 12:14:30
问题 I am looking for a method to see if the value is in array (table) The example table has 3 entries, each entry containing a table with multiple entries Say I am checking if 'apple' is in 'data' data = { {"alpha","bravo","charlie","delta"}, {"apple","kiwi","banana","pear"}, {"carrot","brocoli","cabage","potatoe"} } This is the code I have, a recursive query. The problem is the function breaks somewhere as it drops the positive value local function hasValue(tbl, str) local f = false for ind, val

Passing table values as function arguments in Lua 4 without the “call” function

蹲街弑〆低调 提交于 2020-08-07 06:02:49
问题 I have a table of values that can in principle be of any length: Points = { "Point #1", "Point #5", "Point #7", "Point #10", "Point #5", "Point #11", "Point #5", } I want to pass them as arguments to a function. addPath(<sPathName>, <sPoint>, <sPoint>, ...) Now, normally you could use the "call" function. But in the software I am using this function is unavailable and not in scope. How do I get around this problem in Lua 4? [edit] Here are the functions I can use. 回答1: In newer versions of

Passing table values as function arguments in Lua 4 without the “call” function

余生颓废 提交于 2020-08-07 06:02:40
问题 I have a table of values that can in principle be of any length: Points = { "Point #1", "Point #5", "Point #7", "Point #10", "Point #5", "Point #11", "Point #5", } I want to pass them as arguments to a function. addPath(<sPathName>, <sPoint>, <sPoint>, ...) Now, normally you could use the "call" function. But in the software I am using this function is unavailable and not in scope. How do I get around this problem in Lua 4? [edit] Here are the functions I can use. 回答1: In newer versions of

Sorting a Lua table by key

余生颓废 提交于 2020-07-02 18:24:42
问题 I have gone through many questions and Google results but couldn't find the solution. I am trying to sort a table using table.sort function in Lua but I can't figure out how to use it. I have a table that has keys as random numeric values. I want to sort them in ascending order. I have gone through the Lua wiki page also but table.sort only works with the table values. t = { [223]="asd", [23]="fgh", [543]="hjk", [7]="qwe" } I want it like: t = { [7]="qwe", [23]="fgh", [223]="asd", [543]="hjk"

Sorting a Lua table by key

橙三吉。 提交于 2020-07-02 18:23:55
问题 I have gone through many questions and Google results but couldn't find the solution. I am trying to sort a table using table.sort function in Lua but I can't figure out how to use it. I have a table that has keys as random numeric values. I want to sort them in ascending order. I have gone through the Lua wiki page also but table.sort only works with the table values. t = { [223]="asd", [23]="fgh", [543]="hjk", [7]="qwe" } I want it like: t = { [7]="qwe", [23]="fgh", [223]="asd", [543]="hjk"

Passing table with subtable to Lua function from C++

久未见 提交于 2020-02-25 05:14:07
问题 I'm trying to pass table with subtable to Lua function as argument from C++. Here's my code that doesn't work but shows what I'm trying to do. class DragInfo{ public: std::vector <std::string> files; glm::vec2 position; }; //a callback that passes DragInfo to a Lua function as a table which has 2 subtables void callDragged(DragInfo &info) { lua_getglobal(L, "dragged"); if (!lua_isfunction(L, -1)) { lua_pop(L, 1); return; } lua_newtable(L); for (size_t i = 0; i < info.files.size(); ++i) { lua