Lua - understand setmetatable

I am trying to build a CNN using Torch 7. I am very new to Lua. I was trying to follow this link. I encountered something called setmetatable in the following code block: setmetatable(train_set, { __index = function(t, i) return {[i], t.label[i

Metamethods and courses

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 metat

How do we change how printing displays a table

Assuming I have a piece of code such as the following aTable = {aValue=1} aTable_mt = {} print(aTable) What must I do to make Lua print something like aTable current aValue = 1 as opposed to table: 0x01ab1d2. So far I've tried setting the __tostring

Lua 5.2 - C ++ object in an object (using lua_lightuserdata)

edit: [SOLUTION IN ANSWER 2] I am new to LUA and am having trouble trying to do what I want. I have a C++ object that looks like this: C++ Object definitions struct TLimit { bool enabled; double value; TLimit() : enabled(false), value(0.0) {} ~TLimit

What is the effective implementation of lua __pairs?

Does anybody know actual implementation of lua 5.2. metamethod __pairs? In other words, how do I implement __pairs as a metamethod in a metatable so that it works exactly same with pairs()? I need to override __pairs and want to skip some dummy varia

Lua metatables: class fields and instance fields

I'm working on a game in Corona/Lua, and implementing a class called "Item" which represents an in game weapon, armor, amulet, etc. And I'm new to object-oriented Lua. After creating a new instance of the class, I'm finding that setting certain

Compare a value with a table using metatags in Lua

I'm looking for a way in Lua 5.1 to compare with metatables, so I can compare any value with a table. If that value is in the table it returns true, and false if it is not in the table. like the following. if table == string then -- does something if

Specifying metadata on new objects in Lua

Method/1 Dog = {} function Dog:new() local newObj = {sound = 'woof'} return setmetatable(newObj, { __index = self }) end Method/2 Dog = {} function Dog:new() local newObj = {sound = 'woof'} self.__index = self return setmetatable(newObj, self) end Mo

Lua - calls only one operator

I'm new to Lua, so maybe missed something on tutorials but the problem is: I have original table and metatable with several operators that I'm applying to it: original = { 1, 2, 3 } test = setmetatable(original, { __add = function (lhs, rhs) print('a

Hiding a Lua meta and exposing only attributes of an object

How do you create a Lua object that only exposes its attributes and not its methods? For example: local obj = { attr1 = 1, attr2 = 2, print = function(...) print("obj print: ", ...) end, } Produces: > for k,v in pairs(obj) do print(k, v) end

Lua, c ++ and the disappearing metadata

Background I work with Watusimoto on the game Bitfighter. We use a variation of LuaWrapper to connect our c++ objects with Lua objects in the game. We also use a variation of Lua called lua-vec to speed up vector operations. We have been working to s

Lua Metatables - calling functions with colon-syntax

I have the following problem, somebody can help me? comp = {} comp.__index = function(obj,val) if val == "insert" then return rawget(obj,"gr")["insert"] end return rawget(obj, val) end = function() local ret = {} set

Does Lua allow metamethods with a strange number of arguments?

For example, can I declare a metamethod for __index which takes two arguments, then do something like myuserdata[somearg1, somearg2]? Not that I want to use it or that I have any idea of why it would be useful, I'm just wondering if in my library tha