• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Simple Metamethods - lua

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
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 :)
 
You are using no metamethods at all.
A metatable is a regular Lua table containing a set of metamethods, which are associated with events in Lua. Events occur when Lua executes certain operations, like addition, string concatenation, comparisons etc. Metamethods are regular Lua functions which are called when a specific event occurs. The events have names like "add" and "concat" (see manual section 2.8) which correspond with string keys in the metatable like "__add" and "__concat". In this case to add (+) or concatenate (..) two Lua objects.

If you want more clarification, read the manual: http://www.lua.org/manual/5.1/manual.html#2.8

Your code is also semantically flawed, have you tried calling the second version of "getTasks" more than once?

Code:
ghouls, slimes, necromancers, dwarves, amazons, demons skeletons, scarabs, rotworms, valktries, minotaurs, demons, goblins, larva, vampires, giant spiders, dragon lords, wasps, dragons, trolls, orcs, cyclops, ancient scarabs
ghouls, slimes, necromancers, dwarves, amazons, demons skeletons, scarabs, rotworms, valktries, minotaurs, demons, goblins, larva, vampires, giant spiders, dragon lords, wasps, dragons, trolls, orcs, cyclops, ancient scarabs, ghouls, slimes, necromancers, dwarves, amazons, demons skeletons, scarabs, rotworms, valktries, minotaurs, demons, goblins, larva, vampires, giant spiders, dragon lords, wasps, dragons, trolls, orcs, cyclops, ancient scarabs
ghouls, slimes, necromancers, dwarves, amazons, demons skeletons, scarabs, rotworms, valktries, minotaurs, demons, goblins, larva, vampires, giant spiders, dragon lords, wasps, dragons, trolls, orcs, cyclops, ancient scarabs, ghouls, slimes, necromancers, dwarves, amazons, demons skeletons, scarabs, rotworms, valktries, minotaurs, demons, goblins, larva, vampires, giant spiders, dragon lords, wasps, dragons, trolls, orcs, cyclops, ancient scarabs, ghouls, slimes, necromancers, dwarves, amazons, demons skeletons, scarabs, rotworms, valktries, minotaurs, demons, goblins, larva, vampires, giant spiders, dragon lords, wasps, dragons, trolls, orcs, cyclops, ancient scarabs
 
You are using no metamethods at all.


If you want more clarification, read the manual: http://www.lua.org/manual/5.1/manual.html#2.8

Your code is also semantically flawed, have you tried calling the second version of "getTasks" more than once?
Yes I know, it stores it in the table more then once if called again, BUT this teaches someone hey.. mytable is an object, an instance of a table, if you want to think of it in terms of OOP.

Sure there are no checks to make sure if the data exists, but that is for the next test phase.. gotta get people thinking about how the code works.

There is no such thing as bad code :)

-- Double Post Merged, at 29 Feb 2016 14:48 PM --

I don't know it seems like everyone on this forum that challenges me with things I release for your benefit you want to to add in your opinion.. no it has to be this way..

I was going to give an extensive reason, but you are not even worth it.

Maybe I am just too old school and you all are just too much about the I want it now, no explanation, just now.

Well best of luck to all of you non-thinkers.
 
Last edited by a moderator:
Im seeing you act like a kid as soon as your methods are put ikto questions. If you say its a metadata function it best be that. There is nothing wrong with your code but it doesnt seem to have any prooer usage.

I see you complain all over the place, about everything you see and then you cannot handle your owb code being questioned. If you dont want opinions dont post stuff. Releasing code made for "us" is bullshit. Make it for yourself and it Will likely be useful, just making functions to make them is useless.
 
Back
Top