lua-users home
lua-l archive

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


On Sat, Nov 16, 2013 at 3:56 PM, Andrew Starks <andrew.starks@trms.com> wrote:
> mybtn.callbackfunctions.action = function() end
> ```
>
> to do this, in C, just make your __index function retrieve the
> uservalue from the userdata (which will automatically be at index 1 on
> the stack) and return the callbackfunctions table from the uservalue
> table.

One last thing was bugging me...

you want to make your userdata __index function look in the userdata's
uservalue table for the actions, so that you can use colon notations
for the actions.

Therefore, skip the callbackfunctions table and put the actions
directly into the uservalue table. The code to that I posted earlier
looks like this:
```
lua_newtable(L); // you could also let them optionally pass this table
in as an argument to the constructor
lua_setuservalue(L, 8); //position 8 is where your user value was.


and the common metatable for your userdata will check the uservalue
table for the requested index, which returns a unique function for
each userdata.

This does mean that __newindex needs to be written to pull up the
uservalue table as well, when the user assigns the action.


You might provide a way to assign a default function that would work
the same on all userdata. You could do this with a special function or
a magic character in the name...

Okay. I may still be off. Four emails is probably the max, until I
know if I'm still misunderstanding things.