lua-users home
lua-l archive

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


Lua calls __index only when you're trying to read from a field. When you're trying to write to a field (the case in your script) Lua calls __newindex instead. __index=get, __newindex=set. Please check the manual for more details (including Lua code for the field read and field write behavior.
 
For your proxy tables you'll be better off storing pointers to the structs in userdata, setting the __index metamethod to a C function that retrieves the pointer and returns the valueof the correct member and __newindex to a C function that retrieves the pointer and writes to the correct struct member. You can even use the __gc metamethod to free the struct.
 
--
Fabio
----- Original Message -----
Sent: Tuesday, December 30, 2003 7:27 PM
Subject: __index method not being called??

Hi everyone. I have a little issue with the __index metamethod. It seems to me I'm doing something really stupid, but I can't see where.
 
do
 
mt = {}
mt.__index = function(t, k) print("prueba2 --" .. k) end
 
ipStartData = {}
setmetatable(ipStartData, mt)
 
ipStartData.version = 256 -- 0x0100
ipStartData.delimiter = ","
ipStartData.num_boards = 1
ipStartData.board_list = ipBoardInfo
 
end
 
The __index method does not get called when I access the table. But __newindex does. Have I misunderstood the concept of __index?
What I'd like to achieve eventually is something like proxy tables (I need to set the members of C structs).
I'm using Lua 5.0.
 
Thanks in advance,
Ignacio Burgueño