lua-users home
lua-l archive

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


Hi
 
I have done a little bit of work with Lua C API bindings, that all went pretty smoothly, but I am having great difficulty understanding userdata and light userdata. I have not found the examples online easy to follow or very helpful, so I am hoping someone on this list might be kind enough to help out. The example I want to implement is to expose to Lua a C structure so Lua can access the individual data variables
 
struct foo
{
   int x;
   int y;
   int z;
};
 
struct foo myFoo;
 
--------------------------------------------------------
-- In Lua I want to do something like
local x = myFoo.x
local y = myFoo.y
--------------------------------------------------------
 
As I dont want Lua to create myFoo objects, or manage their memory, I am thinking this example should be implemented with light userdata, but dont know how to go about this.
 
This example is a slightly simplified version of what I need to do,  in reality I have a C array, such as    struct foo myFoo[3];
so ultimately I would want to do something like this in Lua,    local x = myFoo[2].x.
 
I am not sure how much the array version complicates the solution for the non array version ?
 
Any example C API code and explanation would be most appreciated if anyone fancies brushing up their skills to try this out ?
 
Regards Geoff