lua-users home
lua-l archive

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


you can use __newindex,__index like:

-- vector
v={prop={x=5,y=4,z=6}}

-- metatable
m={
  __index=function(t,k)
     print("Getting value of "..k)
     return rawget(t.prop,k)
  end,
  __newindex=function(t,k,v)
     print("Setting value of "..k.."="..v)
     rawset(t.prop,k,v)
  end
}
setmetatable(v,m)

Your C function must crate a metatable with "__index" and "__newindex" 
functions, those functions will catch assignments. In fact, there's no reason
to carry a "prop" subtable, __index and __newindex could use your C-struct 
directly if they are C-functions.

   PpluX

On Mon, Jun 14, 2004 at 10:48:02AM -0400, Dan East wrote:
>   Is there some non-trivial implementation issue that prevents an __assign
> metatable event within Lua?  Or is there some other mechanism already
> providing the same functionality?
> 
>   My C function pushes a table into Lua that represents a struct.  If I
> could catch assignments to the table elements then I could easily allow
> intuitive (ie non Get / Set function oriented) writing to my structs.  Of
> course __newindex doesn't work, because I need to know when existing
> elements are assigned new values, not when new indices are created.
> 
>   Thanks.
> 
>   Dan East