Questions tagged [metatable]

Typically it refers to a feature of Lua which allows a programmer to change the behaviour of tables. The meaning may be dependant on other tags used with it.

In Lua a metatable is used to contain metadata or metafunctions for any Lua value.

There are certain fields within a metatable which (if they exist) have specific responsibilities when used with operators. These are __add, __sub, __mul, __div, __mod, __pow, __unm, __concat, __len, __eq, __lt, __le, __index, __newindex, and __call.

A metatable can have its own metatable and this is known as metatable chaining.

For more information see the Lua reference manual. For a detailed description about meta-tables and meta-methods, see PiL (Programming in Lua). A few of metamethods available are:

  1. Arithmetic Metamethods
  2. Relational Metamethods
  3. Library-Defined Metamethods
  4. Table-Access Metamethods

    • The __index Metamethod
    • The __newindex Metamethod
    • Tables with Default Values
    • Tracking accesses
181 questions
0
votes
0 answers

Lua Cant Create Object

My Code is: Target = {} Target.__index = Target function Target.new(isfound,distance,azimuth_angle,elevation_angle,time_since_detected) local instance = setmetatable({},Target) instance.isfound = isfound instance.distance = distance …
KRIShark
  • 17
  • 5
0
votes
1 answer

Importing class and using its methods accross files

Im trying to save myself some headache in a project im working on by splitting it up in files but I cannot seem to be able to cross call functions. (inside file_a, call a function in file_b which calls a function in file_a) Is this just not possible…
jmelger
  • 87
  • 5
0
votes
1 answer

Index a JS object like a Metatable in Lua

Is there a way to index an object in JavaScript like a Metatable in Lua? Like for example: var obj = {a:"a",b:"b",properties:{c:"c",d:"d"}} metatable(obj,obj.properties) // Make it so that if you try to index something that's not inside the object…
0
votes
1 answer

Metatable in Module Script not excetuting Roblox

so i made 2 module scripts here's the code: local base = {} base.__index = base base.Number = 5 function base:New() local t = setmetatable({}, self) t.__index = t return t end return base second: local base = script.Parent.Base local…
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
2 answers

Random number generation problem with meta table

Please look at the next code, Lua. randomNumber = { new = function(self,o) o = o or {} setmetatable(o, self) self.__index = self return o end, math.randomseed(os.time()), getNum = math.random() } for i = 1, 10 do x =…
0
votes
2 answers

luajit how to automatically set default metatable to every newly created table?

I have a personal project and a pure lua writed object module which provides metatable with methods filter map etc to a table , I don't want to require and setmetatable for every line local foo={}
tcpiper
  • 2,456
  • 2
  • 28
  • 41
0
votes
2 answers

function arguments expected

I'd like to accomplish one thing with metatablitz, but i don't understand how to do it right. More precisely, i implemented it, but i miss one little thing - so that in the right place i can use a colon. The code below is simple and speaks for…
Aquinary
  • 71
  • 5
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
0 answers

LUA get last table access/manipulation time

What I have now is this function: function MyAddon:New() local object = setmetatable({}, { __index = self }) object.elements = { } object.lastAccess = os.time() return object end Now I want to set lastAccess every time when…
M.K.
  • 3
  • 2
0
votes
0 answers

Convert Lua object to JSON in Corona SDK

I'm coming from more OO languages (Java, C++, C#), so it's a bit hard for me to wrap my head around how lua handles objects. I have a user object declared as follows in a User.lua file: local User = {align="neutral", justness=50, sceneData=nil}; I…
mtbyrd
  • 1
  • 1