lua-users home
lua-l archive

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

Thanks for the suggestion, but I am already using SWIG in the embedded project I am working on. I chose Swig as it had the advantage of not having any Windows dependencies, so it was pretty easy to use in an embedded environment. I have found it has some limitations, for example as far as I can see it cannot really handle arrays of data, hence my question here.
Besides, I wanted to gain an understanding of the Lua C API, so I was happy to go with a hybrid approach, SWIG for some bindings and manuallly written binding code for some of the things that SWIG didnt handle.  So far its working out well, and its easy to mix and match SWIG bound code and manual code.
I just need to figure out this userdata stuff as it is obviously pretty important in getting a good handle on understanding the Lua/C API. Pity I dont think I can use the light userdata as I now understand that sufficiently.  I wonder if anyone has got time to post a full user data example to contrast with the light userdata example earlier in this thread ? I am sure it would help other folks in the future apart from just me !

Date: Sun, 28 Aug 2011 16:46:35 -0400
Subject: Re: Struggling with userdata concepts

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. 

On Sat, Aug 27, 2011 at 10:46 AM, Jeff Smith <> wrote:
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