lua-users home
lua-l archive

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


I have been looking at this page from the wiki http://lua-users.org/wiki/UserDataExample
 
I got this working OK, then thought I would extend it a bit to prove I understood things. I think I just proved I didn't understand things :(
 
If have another example of using the C API for userdata where instead of having a table of functions I could manipulate items directly, for example
 
a = myNewThing()
a. fred = 123   -- uses __newindex
print(a.fred)    -- uses __index
 
So I tried to extend the wiki page example to allow me to call functions on the userdata  but also access items directly
 
typedef struct Foo {
  int x;
  int y;
  int fred;    <-- my new item I added
} Foo;

local a = Foo.new()
a:setx(3)  -- this works as per the example
 
a.fred = 123  <-- how do I get this to work
print(a.fred) <-- how do I get this to work
 
Any tips please on how I would change the C api code to make this work in the above example. My failed attempt I added a __newindex and __index into the Foo_meta table, and some associated C code.
 
The __newindex worked but the __index didn't work as I had overlooked the fact it was already pointing at the methods table.
 
Thanks for any help
 
Geoff