Questions tagged [meta-method]

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.

52 questions
3
votes
2 answers

Can I override a Lua table's return value for itself?

Is it possible for a table, when referenced without a key, to return a particular value rather than a reference to itself? Let's say I have the following table: local person = { name = "Kapulani", level = 100, age = 30, } In Lua, I…
Kapulani
  • 95
  • 5
3
votes
2 answers

Lua override # for strings

I'm trying to imlement my own length method for strings in Lua. I have successfully overriden len() method for string, but I have no idea how to do this for # operator. orig_len = string.len function my_len(s) print(s) return…
Xanx
  • 545
  • 1
  • 5
  • 17
3
votes
1 answer

Lua - specifying library for coroutine

Within my Lua scripts, I have multiple libraries using the same 'structure'. For example, I have a.lua what contains require('b') and require('c'). Both b.lua and c.lua have got an info function. b.lua let it print "b" and c.lua let it print "c". In…
scheurneus
  • 155
  • 5
2
votes
1 answer

Lua __eq on tables with different metatables

I have found the following quote on this site http://lua-users.org/wiki/MetamethodsTutorial: __eq is called when the == operator is used on two tables, the reference equality check failed, and both tables have the same __eq metamethod (!). Now I…
SkryptX
  • 813
  • 1
  • 9
  • 24
2
votes
1 answer

How do I check the argument type in a lua function call?

I have overloaded multiplication operator like this in a table metatable designed to imitate a class. function classTestTable(members) members = members or {} local mt = { __metatable = members; __index = members; } function…
sgowd
  • 946
  • 3
  • 10
  • 27
2
votes
1 answer

Why doesn't love2d use my custom-defined pairs metamethod?

I have the following code in the file main.lua: local table = {data={a=1,b=2,c=3}} setmetatable(table, table) function table:__pairs() return pairs(self.data) end function table:__tostring() return "a table" end print(table) for e in…
Josie Thompson
  • 5,608
  • 1
  • 13
  • 24
2
votes
3 answers

implementing __index metafunction in C/c++

I have a script to C++ callback/functor system that can call any "registered" C++ function using strings and/or variants. //REMOVED ERROR CHECKS AND ERRONEOUS STUFF FOR THIS POST int LuaGameObject::LuaCallFunction( lua_State *luaState ) { if (…
Danny Diaz
  • 230
  • 2
  • 10
2
votes
0 answers

"Object" generating function in Lua

I use this to generate "class" in Lua. function Class(...) local cls = {}; local bases = {arg}; for i, base in ipairs(bases) do for k, v in pairs(base) do cls[k] = v end end cls.__index = cls; cls._is_a = {[cls] = true} …
2
votes
1 answer

Lua - __newindex metamethod for existing index?

I recently learned the existence of methatables in lua, and i was toying around with them until an idea came to my mind: would it be possible to use those to try and avoid "duplicates" in a table? I searched and searched and so far couldn't find…
Thex
  • 31
  • 1
  • 5
2
votes
1 answer

attaching metatables within tabes

I have parser that parses a config file and produces a table. The resulting table can look something like: root = { global = { }, section1 = { subsect1 = { setting = 1 subsubsect2 = { } } } } The goal is to have a table I…
hookenz
  • 36,432
  • 45
  • 177
  • 286
2
votes
1 answer

Confusion of using "." notation with __index and namespace in Lua

I am confused of the following two syntaxes using "." From what I understand, __index is called when a key doesn't exist in a table but exists in its metatable. So why does the list table call __index and then assign itself to list.__index? …
LED Fantom
  • 1,229
  • 1
  • 12
  • 32
2
votes
1 answer

Calling Lua Functions From C++

I'm using the latest version of LuaJit and need some help getting started. What I need is to have a bunch of functions exposed to the Lua environment which can be overridden inside the scripts to run the user's supplied code, these functions would…
KKlouzal
  • 732
  • 9
  • 32
1
vote
1 answer

Lua __index method not executing on read

I'm trying to have my __index method fire anytime a key is read from this table, but it is apparently not firing since A) the contents read are never modified according to its intended math and B) its print statement never fires. What am I missing,…
Roland
  • 57
  • 5
1
vote
1 answer

Currying operations in lua

This question has some reference to the question Defining logical operator implies in lua. The following code works fine. local function _implies(a, b) if a == 1 and b == 0 then return 0 else return 1 end end local…
mathsbeauty
  • 364
  • 4
  • 13
1
vote
1 answer

__pairs() and __ipairs() metamethods does not work at all

I don't know what I do wrong. Basically the code looks like this: local t = setmetatable({}, {__pairs = function (self) print "Message from __pairs()" return function () ... end end}) for k, v in pairs(t) do ... end The same for…
Nail'
  • 23
  • 3