lua-table

Use table variable in Lua class

送分小仙女□ 提交于 2021-02-13 17:02:46
问题 I need a table variable in a Lua class, which is unique for each instance of the class. In the example listed below the variable self.element seems to be a static variable, that is used by all class instances. How do I have to change the testClass to get a non static self.element table variable? Example: local testClass ={dataSize=0, elementSize=0, element = {} } function testClass:new (data) local o = {} setmetatable(o, self) self.__index = self -- table to store the parts of the element

Error when reading a two-dimensional table

允我心安 提交于 2021-02-11 07:12:29
问题 I'm trying to create an infinite game field by connecting the opposite edges of the field. I get the following error: Error: attempting to index field '?' (a nil value) The error is on the line in bold. As far as I understand, the array field does not contain any values when called in function drawField() despite it was filled with zeros in function clearField() . How can I fix the array so it keeps its values outside clearField() ? local black = 0x000000 local white = 0xFFFFFF local field =

Error when reading a two-dimensional table

拥有回忆 提交于 2021-02-11 07:12:24
问题 I'm trying to create an infinite game field by connecting the opposite edges of the field. I get the following error: Error: attempting to index field '?' (a nil value) The error is on the line in bold. As far as I understand, the array field does not contain any values when called in function drawField() despite it was filled with zeros in function clearField() . How can I fix the array so it keeps its values outside clearField() ? local black = 0x000000 local white = 0xFFFFFF local field =

Error when reading a two-dimensional table

大兔子大兔子 提交于 2021-02-11 07:10:47
问题 I'm trying to create an infinite game field by connecting the opposite edges of the field. I get the following error: Error: attempting to index field '?' (a nil value) The error is on the line in bold. As far as I understand, the array field does not contain any values when called in function drawField() despite it was filled with zeros in function clearField() . How can I fix the array so it keeps its values outside clearField() ? local black = 0x000000 local white = 0xFFFFFF local field =

How to print a table's contents within a table? [Lua]

大城市里の小女人 提交于 2021-02-08 07:18:24
问题 What I want to do is ONLY print a table's contents within a table. For example: local stats = { table1 = { tTable1 = { data = 1 }, tTable2 = { data2 = 2 }, tTable3 = { data3 = 3 }, } } I don't really care about table1 or all the tTables but rather the information in the data variables. How can I print them? This is a snippet of my real code: local stats = { [1] = { [1] = { [1] = 1, [2] = -1, [3] = -1, ["n"] = 3, }, [2] = { [1] = nuclearcraft:cooler, [2] = 10, ["n"] = 2, }, ["n"] = 2, }, [2] =

How to sort this lua table?

蓝咒 提交于 2021-02-07 19:05:36
问题 I have next structure self.modules = { ["Announcements"] = { priority = 0, -- Tons of other attributes }, ["Healthbar"] = { priority = 40, -- Tons of other attributes }, ["Powerbar"] = { priority = 35, -- Tons of other attributes }, } I need to sort this table by priorty DESC, other values does not matter. E.g. Healthbar first, then Powerbar, and then going all others. // edit. Keys must be preserved. // edit #2 Found a solution, thanks you all. local function pairsByPriority(t) local

How to sort this lua table?

梦想的初衷 提交于 2021-02-07 19:05:07
问题 I have next structure self.modules = { ["Announcements"] = { priority = 0, -- Tons of other attributes }, ["Healthbar"] = { priority = 40, -- Tons of other attributes }, ["Powerbar"] = { priority = 35, -- Tons of other attributes }, } I need to sort this table by priorty DESC, other values does not matter. E.g. Healthbar first, then Powerbar, and then going all others. // edit. Keys must be preserved. // edit #2 Found a solution, thanks you all. local function pairsByPriority(t) local

Lua - Delete non-empty directory

柔情痞子 提交于 2021-01-03 06:55:51
问题 I'm trying to remove non-empty directory in Lua but without success, I tried the following: os.remove(path_to_dir) And got the error: Directory not empty 39 When 39 is the number of files in path_to_dir Also tried: require ('lfs') lfs.rmdir(path_to_dir) And got the error: Directory not empty' Worth to mention that I did chmod -R a+rX * to path_to_dir Thanks for the help. 回答1: You can either follow @EgorSkriptunoff's suggestion and use OS-specific commands to remove non-empty directories or

Lua - Delete non-empty directory

若如初见. 提交于 2021-01-03 06:55:37
问题 I'm trying to remove non-empty directory in Lua but without success, I tried the following: os.remove(path_to_dir) And got the error: Directory not empty 39 When 39 is the number of files in path_to_dir Also tried: require ('lfs') lfs.rmdir(path_to_dir) And got the error: Directory not empty' Worth to mention that I did chmod -R a+rX * to path_to_dir Thanks for the help. 回答1: You can either follow @EgorSkriptunoff's suggestion and use OS-specific commands to remove non-empty directories or

Using a coordinate pair as a key in a Lua table

怎甘沉沦 提交于 2021-01-03 06:29:07
问题 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) ==