lua-users home
lua-l archive

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


Have you considered using SWIG? It generates all that code for you. AND allows you to export your C++ to several other languages without extra coding. Why would you ever want to do that? For instance, you have a C++ library, that you want to use from a C# GUI. In addition, that GUI is scriptable in Lua, so you export parts of the C++ lib to Lua. One SWIG interface definintion file exports to both Lua and C#. Crazy awesome. 
Oliver

On Sat, Aug 27, 2011 at 10:46 AM, Jeff Smith <spammealot1@live.co.uk> wrote:
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