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
0
votes
2 answers
Is there a way to know if there is a key added or removed from an array in lua?
local t = {}
local mt = setmetatable({
-- some meta method to know when a key is added or lost and prints a message
}, t)
Is there a way of doing this. I talked about this with someone and they said i couldn't just do it with meta…

baked
- 3
- 1
0
votes
2 answers
how to make then __index and __newindex detect table.index
I'm still a little newbie with metatables, and there is something that makes me confused
when I use metamethods like __index and __newindex in my metatable, they are only called when I invoke an element of the table as follows:
print(table[index])…

Vinni Marcon
- 606
- 1
- 4
- 18
0
votes
0 answers
Assigned value suddenly dissapeared, any explanation?
function iterator(N)
local i = i or 0 --Start from 0 so we can add it up later
if type(N) == "table" then --Check if N is a table or not, if not, error
print("Iterating through all values inside the table")
else
error("This is not…

BlaztOne
- 180
- 1
- 10
0
votes
1 answer
what kind of metamethods are these, why they exist and how they are created
Hello everyone!
I've been studying metamethods and I realized something strange!
I already know all the metamethods presented in the Lua documentation as __add, __index, __newindex, etc... But I see around in forums and in questions around here…

Vinni Marcon
- 606
- 1
- 4
- 18
0
votes
1 answer
Point table to another table in Lua
is there any way to point table to another table? for example:
local a = {}
local b = {}
a.name = "Josh"
print(a.name) -- Prints 'Josh'
print(b.name) -- Prints 'Josh' aswell
a.name = "I don't have a name"
print(a.name) -- Print 'I don't have a…

Feche1320
- 25
- 1
- 6
0
votes
1 answer
Create new instance of certain user type
I'm using tolua++ to automatically expose C++ types to Lua. It seems that when I expose some type, e.g.
struct TestComponent
{
float foo;
string bar;
}
What tolua does (at least this is what it seems like to me) is add a new metatable to the…

TravisG
- 2,373
- 2
- 30
- 47
0
votes
1 answer
Store inherited table member into child class if __index is invoked by object of child class
My goal is to have a standard way of creating classes with full-scaled multiple inheritance and the ability to not only inherit to other classes but also to instances of themselves via the new()-constructor. If I call a missing value from the class…

Florian R. Klein
- 1,375
- 2
- 15
- 32