lua-users home
lua-l archive

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


Robert Sadedin wrote:

I am storing a c++ object in a lua user data, and binding a meta table to it for lookup on method calls.
...
What I would like to do is allow a user to add lua variables to these c++ objects wrapped by a user data and accessed through the meta table.

Currently a user can call, for instance:
obj = createSomeObject()
print(obj:getCVariable1())

I'd like the user to be able to say:
obj.x = 6
print(obj.x)

you could try this :

1/ implement an upvalue attributes table in the metatable
2/ hook meta.__newindex() to add new attribs to the local table
3/ check attributes table first on meta.__index()

this would require a seperate metatble per userdata, but they can all share the __index and __newindex function

nb pairs will not see the attributes if you do this.

Adrian