lua-users home
lua-l archive

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



Michael T. Richter wrote:
When would I want to use light userdatas instead of full-fledged ones?

The classic example is when you want store a lua value in a C structure, but then you realize you can't because you can only store lua values in the registry, the globals, upvalues, or a subtable these.

So what you do is make a lookup table. You put a regular lua table someplace that you have easy access to from C, such an upvalue or the lua registry. Use a lightuserdata of a pointer to your structure as a key to the lua value you want to associate with that pointer.

Then in your C code wherever you have just the pointer, you can get the lua value from the lookup table.

- Peter Shook