Codex NG
Recurrent Flamer
- Joined
- Jul 24, 2015
- Messages
- 2,994
- Solutions
- 12
- Reaction score
- 1,657
If you are confused about metamethods or how to create them this little thread will attempt to show you, you don't need to create an entire metatable to use metamethods.
Someone had an issue in support where they wanted to get all of the string index values of a table, I presumed they wanted to turn these index value into a string to use in a sentence.
So I took this table
And wrote this function
Which produced this result
So a thought came to mind, could I create a metamethod of a table which is a non-metatable.
So to test this theory I created an empty table.
I then took 2 commonly used table functions (table.insert & table.concat) and re-wrote them to reflect that of a metamethod
Then I re-wrote the original function use to generate the string, while using the newly created metamethods.
Now when I call the print(getTasks(config, ', ')) it produces the same results.
I am sure you are wondering what this value self is.. well think of it as a reference to the table myTable.
Well I hope this has been educational
Someone had an issue in support where they wanted to get all of the string index values of a table, I presumed they wanted to turn these index value into a string to use in a sentence.
So I took this table
Code:
local config = {
["wasps"] = {storage = 45003, experience = 1000, reward = 2160, count = 1},
["trolls"] = {storage = 45005, experience = 1000, reward = 2160, count = 1},
["goblins"] = {storage = 45007, experience = 1000, reward = 2160, count = 1},
["slimes"] = {storage = 45009, experience = 1000, reward = 2160, count = 1},
["rotworms"] = {storage = 45011, experience = 1000, reward = 2160, count = 1},
["amazons"] = {storage = 45013, experience = 1000, reward = 2160, count = 1},
["valktries"] = {storage = 45015, experience = 1000, reward = 2160, count = 1},
["larva"] = {storage = 45017, experience = 1000, reward = 2160, count = 1},
["orcs"] = {storage = 45019, experience = 1000, reward = 2160, count = 1},
["dwarves"] = {storage = 45021, experience = 1000, reward = 2160, count = 1},
["minotaurs"] = {storage = 45023, experience = 1000, reward = 2160, count = 1},
["ghouls"] = {storage = 45025, experience = 1000, reward = 2160, count = 1},
["scarabs"] = {storage = 45027, experience = 1000, reward = 2160, count = 1},
["ancient scarabs"] = {storage = 45029, experience = 1000, reward = 2160, count = 1},
["demons skeletons"] = {storage = 45031, experience = 1000, reward = 2160, count = 1},
["cyclops"] = {storage = 45033, experience = 1000, reward = 2160, count = 1},
["vampires"] = {storage = 45035, experience = 1000, reward = 2160, count = 1},
["necromancers"] = {storage = 45037, experience = 1000, reward = 2160, count = 1},
["giant spiders"] = {storage = 45039, experience = 1000, reward = 2160, count = 1},
["dragons"] = {storage = 45041, experience = 1000, reward = 2160, count = 1},
["dragon lords"] = {storage = 45043, experience = 1000, reward = 2160, count = 1},
["demons"] = {storage = 45045, experience = 1000, reward = 2160, count = 1}
}
And wrote this function
Code:
function getTasks(t)
local x = {}
for k, v in pairs(t) do
table.insert(x, k..', ')
end
local s = table.concat(x)
return s:sub(1, #s - 2)..' '
end
Which produced this result
Code:
ghouls, demons, slimes, cyclops, goblins, dwarves, ancient scarabs, orcs, dragons, larva, vampires, wasps, amazons, necromancers, rotworms, dragon lords, valktries, demons skeletons, giant spiders, scarabs, minotaurs, trolls
So a thought came to mind, could I create a metamethod of a table which is a non-metatable.
So to test this theory I created an empty table.
Code:
local myTable = {}
I then took 2 commonly used table functions (table.insert & table.concat) and re-wrote them to reflect that of a metamethod
Code:
function myTable:insert(value)
table.insert(self, value)
end
function myTable:concat()
return table.concat(self)
end
Then I re-wrote the original function use to generate the string, while using the newly created metamethods.
Code:
function getTasks(table_, seperator)
for value, _ in pairs(table_) do
value = seperator and value .. seperator or value
myTable:insert(value)
end
local s = myTable:concat()
return s:sub(1, #s - 2)..' '
end
Now when I call the print(getTasks(config, ', ')) it produces the same results.
Code:
ghouls, demons, slimes, cyclops, goblins, dwarves, ancient scarabs, orcs, dragons, larva, vampires, wasps, amazons, necromancers, rotworms, dragon lords, valktries, demons skeletons, giant spiders, scarabs, minotaurs, trolls
I am sure you are wondering what this value self is.. well think of it as a reference to the table myTable.
Well I hope this has been educational