Anything related to Lua meta-methods (a.k.a. metamethods), i.e. functions that alter how a Lua table behaves in the context of some operations (e.g. math operations, element access, garbage collection, etc.). Meta-methods can be used to implement object oriented programming and other powerful techniques using Lua tables.
Questions tagged [meta-method]
52 questions
1
vote
2 answers
Make __call metamethod as iterator
I'm trying to emulate execution of certain functions from scripts of other app. That app have Lua library which contains function list(), it returns table where key is string UUID and value just string, like local tbl = { "0000..-0000-..." =…

craftersmine
- 41
- 7
1
vote
1 answer
Lua - trying to create a good Vec2 class
I'm learning how to use Lua and Love2d and I want to create a Vec2 class using metamethods and metatables. This is what I have so far:
class.lua: (The base class files)
local Class = {}
Class.__index = Class
-- Constructor
function Class:new()…

roydbt
- 91
- 1
- 8
1
vote
1 answer
How to get __metatable function to be called?
My goal
Get getmetatable to return the return value of the function assigned to the __metatable field.
Code:
local x, m = {}, {__metatable = function() return nil end};
setmetatable(x, m);
io.write("Let's get the metatable:",…

incapaz
- 349
- 1
- 3
- 11
1
vote
0 answers
in Lua 5.x what is Lua 4.0 "gettable" and "settable" event used to set tagmethods (like metamethods)?
In lua 4.0 the tagmethod "gettable" allows to intercept the access to table elements.
Each time there is an attempt to access a table element, the linked tagmethod for "gettable" event is called:
local t = { a=123 }
local tg =…

Laurent
- 68
- 4
1
vote
2 answers
Access to pointers inside __eq meta method?
I have Lua objects which share a metatable which has a __eq metamethod. Inside this meta method, I want to check if the two objects are the same object before even comparing them. Similar to how in java you would do a == b || a.compareTo(b). The…

Hatefiend
- 3,416
- 6
- 33
- 74
1
vote
1 answer
Emulating c++ 'using namespace' via _ENV
if I have a file foo.lua:
local foo = {}
foo.add = function(a, b) return a+b end
foo.sub = function(a, b) return a-b end
foo.multiply = function(a, b) return a*b end
return foo
and in bar.lua I make heavy use of code from foo.lua I am bothered by…

Ace shinigami
- 1,374
- 2
- 12
- 25
1
vote
2 answers
Metamethods and Classes
I created a similar function to what is shown here, and am having trouble with the __add metamethod. I want to be able to use the __add metamethod on two instances of a Class, but the only way it seems to work is if I add the metamethods to the…

eliw00d
- 321
- 2
- 12
1
vote
1 answer
How does setmetatable() work and why the metatable is needed in the linked list in lua
I'm learning how Lua's metatables work in OOP, and I am confused of the code I read from the object orientation tutorial on lua-users wiki. Could someone helps to explain the following questions? Thanks.
Question 1:
The wiki's explanation: Here we…

LED Fantom
- 1,229
- 1
- 12
- 32
1
vote
0 answers
Print all metatable content
I have table:
[кнк] = (table)
[20-6-2014] = (table)
[16] = Steve,Maria
[16-6-2014] = (table)
[17] = Elice, Hans
[18] = Steve, Maria, Hans
And i have dirty but working code:
function af.info(farm_name,date,time,name_string)
…

stillru
- 11
- 3
1
vote
2 answers
Lua: Interpreting undefined variable by its name
I have a function foo that can get nil values under certain circumstances, i.e. foo(VarA) while VarA is undefined. This undefined VarA should get interpreted as "VarA" but I can't invoke foo("VarA") because VarA should act as an option param…
user3472162
1
vote
0 answers
Lua __index metamethod not binding from C++
I'm developing a software library for electronics projects in Lua, which is quit object oriented. My objects have properties with getters and setters, which are implemented with the __index and __newindex metamethods respectively. The problem is the…

Dirk
- 2,094
- 3
- 25
- 28
1
vote
1 answer
make lua method that can directly change arguments
How to create a method like string.gsub(...) in lua ?
I want my function can change Arguments that I pass them to the function.
I know that string and number Type Variables pass by name ( CALL BY VALUE ) in functions,
but I don't know how gsub can…

NIMIX 3
- 13
- 2
0
votes
2 answers
Setting addition operator through metatable in Lua
vals = { i=1, j=2}
setmetatable(vals, {
__add = function (a, b)
return a*b
end,
})
sr = vals.i+vals.j
print(sr)
It prints sr as 3. The expected answer is 2 as 1*2 equals to 2. Why the addition operation (metamethod) is not getting…

mathsbeauty
- 364
- 4
- 13
0
votes
1 answer
Setting multiplication operator through metatable in specific environment in Lua
local names = setmetatable({},
{__mul = function(a,b) return a|b end}
)
names={i=0,j=1}
tr=load("return i*j",nil,"t",names)()
print(tr)
It prints tr as 0. The expected answer is 1 as 0|1 results to 1. Where is the code wrong?

mathsbeauty
- 364
- 4
- 13
0
votes
1 answer
Is it possible to add custom methods to types/classes?
Like asked in the title, lets say I want to add a custom method to the table type, lets say table:printContent(), is there any way in Lua to achieve this? I mean only, pure, Lua. In C#, for example, you can use extensions to do that:
using…

FrostDracony
- 1
- 1