This requires __index, __newindex, and method calling.
I tried a couple different approaches:
Method A
- Create metatable methods loaded with functions such as add and destroy.
- Assign methods.__index to a custom C function that examines it's arguments and returns the value of the instance variable of my object pointed to by userdata.
- Assign methods.__newindex to a custom C function that examines it's arguments and sets the value of the instance variable of my object pointed to by userdata.
- Assign methods as the metatable of the userdata object (objectA) after it is created.
What's Working - Setting and getting properties (objectA.x = objectA.x + 2)
What's Not Working - The functions contained in the methods metatable are not automatically called. I added some code to the methods.__index C function to catch the function calls, but the arguments only include the methods table, and function key - no function arguments are included (self, arg1, and arg2 are missing).
Method B
- Create empty metatable attributes.
- Assign attributes.__index to a custom C function that examines it's arguments and returns the value of the instance variable of my object pointed to by userdata.
- Assign attributes.__newindex to a custom C function that examines it's arguments and sets the value of the instance variable of my object pointed to by userdata.
- Create metatable methods loaded with functions such as add and destroy.
- Assign attributes as the metatable of methods metatable.
- Assign methods as the metatable of the userdata object (objectA) after it is created.
What's Working - The functions contained in the methods metatable are automatically called with the correct arguments
What's Not Working - Setting and getting properties (objectA.x = objectA.x + 2) does not work as the attrbutes.__index and attrbutes.__newindex custom C functions are not getting the proper arguments.
This would seem to be a common requirement: a userdata object requiring functions on self, along with custom setter and getter routines. This is all in C as I need to manipulate an Objective C object through it's methods and properties. The Obj-C object is pointed to by the userdata object.
Does it make more sense to use a table vs userdata? I'm literally on day #4 with this problem and would greatly appreciate the group's input.
Thanks in advance!
-Tom