lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Just figured out that you don't need to set the metatables for all the subtables, if you just set the main table like this: setmetatable(eq, get_mt_mirror(_eq)) it will work for both setting a single element, as well as a whole subtable. For setting the entire table I couldn't figure out how to get that last bit of code you gave me to work, however it's possible to use a pairs() loop to iterate through the tables and transfer one subtable at a time.

_eq = {
   band1 = {op1 = 1,op2 = 2,op3 = 3,op4 = 4,op5 = 5},
   band2 = {op1 = 1,op2 = 2,op3 = 3,op4 = 4,op5 = 5},
   band3 = {op1 = 1,op2 = 2,op3 = 3,op4 = 4,op5 = 5}
   }
eq = {}

function table_print(table)
   for k, v in pairs(table) do
       if type(v) == "table" then
           table_print(v)
       else
           print(k,v)
       end
   end
end

function get_mt_mirror(orig)
   return {
   __index = function(t,k)
print("index") --only leaving this in for now in order to check that the metamethods are being triggered
       return orig[k]
   end,
__newindex = function(t,k,v)
       print("newindex")
       orig[k] = v
   end
   }
end

setmetatable(eq, get_mt_mirror(_eq))

preset1 = {
   band1 = {op1 = 66,op2 = 66,op3 = 66,op4 = 4,op5 = 66},
   band2 = {op1 = 66,op2 = 66,op3 = 66,op4 = 4,op5 = 66},
   band3 = {op1 = 66,op2 = 66,op3 = 66,op4 = 4,op5 = 66}
   }

b = {op1 = 99,op2 = 99,op3 = 99,op4 = 99,op5 = 99}

print("one")
eq.band1.op1=88
table_print(eq.band1)
print("")

print("two")
eq.band1 = b
print(eq.band1.op1)
print("")

print("three")
for k in pairs(preset1) do
   eq[k] = preset1[k]
end
print(eq.band1.op1)

One thing I'm still a little confused about though. As you can see from the output from this script, using "eq.band1.op1=88" triggers __index, but using eq.band1 = b triggers __newindex?


Mauro Iazzi wrote:
On 14/07/07, Merick <Merick_TeVaran@comcast.net> wrote:
Thanks, that works nicely. It should help me out with automating the
equalizer settings for the sound library I'm using.

One thing though, how do I need to change it to  get it to work for
something like this:

b = {op1 = 9,op2 = 8,op3 = 7,op4 = 6,op5 = 5}
eq.chan1 = b

Depends on what you want it to do... You should think well of the
structure you want to generate.

having eq mirror of _eq is not the same as having their fields mirror
each other.

try the recursive approach.

function get_rec_mirror(orig)
 return {
  __index = function(t,k)
      print("index",t,k)
      if  type(orig[k]~='table' then
        return orig[k]
      else
         local ret = {}
         setmetatable(ret, get_rec_mirror(orig[k])
         return ret
      end
  end,
    __newindex = function(t,k,v)
      print("newindex",t,k,v)
      orig[k] = v
  end
  }
end

this or something similar could work as you intend.


or even this:

preset1 = {
    chan1 = {op1 = 66,op2 = 66,op3 = 66,op4 = 4,op5 = 66},
    chan2 = {op1 = 66,op2 = 66,op3 = 66,op4 = 4,op5 = 66},
    chan3 = {op1 = 66,op2 = 66,op3 = 66,op4 = 4,op5 = 66}
    }

eq = preset1

this simply won't work, unless you set the metatable for globals which
you probably don't want.
Else just use a function that copies and sets metatables too according
to your wishes.

--mi