I hope you all can help me. So, I want to know when I call function in table like this which metamethod is activating:
local myTbl = setmetatable({
myIndex = function(myParam)
print(myParam)
end
}, {
-- Which is the metamethod that activates itself while using myIndex
})
I tried EVERYTHING!!! I mean. I made metatable and I used every single metamethod and nothing is called from the metamethods I used.
If you remove
myIndexfrom the table,myTbl.myIndex ()will activate the__indexmetamethod, which can either be a function(table, key)or a table.myIndex ()will only be called if__index (table, key)takes care of that, or if there is__index.myIndexfunction doing what you want.See 13.4.1 – The __index Metamethod from Programming in Lua.
Update, after reading comments: By manipulating metatables and redefining functions you can effectively add new metamethods (e.g., by redefining the globals
pairs()andipairs()you can enable in Lua 5.1__pairsand__ipairsmetamethods, introduced in Lua 5.2).The metatable is usually set in a 'wrapper' function that receives a table and returns its extended / enriched version. Like this: