lua-users home
lua-l archive

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

Sorry for my late reply, but I can not quite get this to work. I am missing something fundamental, but what?

I have two C++ objects I want to read/write from Lua. One is a collection of the other. I want to be able to create/access objects of the subsidiary type, but (currently) only want to access the overall collection type (i.e. not create them).

For example:

class BoxPlots {

    void AddPlot (Plot *plot);
    void AddPlots (const std::vector <Plot *> &plots);
    Plot *GetPlot (const unsigned int index) const;
    const unsigned int GetNumberOfPlots () const;

class Plot {

The BoxPlots is a collection of Plot's. I want to create new Plot's and access existing Plot's. I want to access the existing BoxPlots, but I don't necessarily want to create them from Lua. The C++ variable that will be "bound" to Lua is of type BoxPlots and from then using the GetPlot method, return a Plot to Lua or creating new ones etc...

I feel like I need to add something to my .i file to be able to create the function like you mention below. I tried somewhat merging your advice and Andrew Zhilin's; however, I was unable to figure how to access all my types from Lua. Maybe some example Lua code would help?

I'm not sure if I've explained myself well enough, but I hope you get the idea. I did initially take Roger's advice and made my own binding which did work, but I'd rather automate this process since I hope to do more in the future.

Thanks to you all!


On May 6, 2007, at 9:26 PM, mark gossage wrote:

Hello Joey,

I do almost all my SWIG-LUA work from the lua side. Swig is really good for just wrappering up a C/C++ library to get it readable by lua. Getting the C++ to talk to lua is fairly easy, but not well documented.

You idea of lua_pushlightuserdata(), was close, but not there.
You probably want something like this:

Foo* p= new Foo();
lua_setglobal (L, "p");

The SWIG_NewPointerObj() creates a userdata (not a lightuserdata) for the foo object & pushes it to the stack. The last param (in this case 1) is whether you want lua to manage the memory (0 for no, 1 for yes).

The SWIG_NewPointerObj() and SWIGTYPE_p_Foo are both found in the wrapping file.

Once you have that you should be able to do in lua:

Let me know if you have any other questions.

I create an instance of my class variable in C++ and want to use Lua
to use some of the routines in my C++ class so I used SWIG to
generate the bindings for Lua and I think I have that right.

However, I can not figure out how to make Lua use my C++ variable
instance instead of creating a new one.  I was looking at the Wiki
and I am very close to the CppObjectBinding method, except I am using

I have a :

     lua_pushlightuserdata (global_Lua_state, this);
     lua_setglobal (global_Lua_state, "this");

to put the "this" pointer on the stack.  I want to interface that

with Lua:

print ("In the Lua script")
b = plot.Plot(this)  -- wrong!  How do I make b = this, but be of a
plot.Plot () type.
print (this)
print (b)
print (b:DumpV (0))
print (this:DumpV(0))

Two addresses are printed out, but they are different (which I
expect).  I want to do this:DumpV (0), but I get a:

In the Lua script
userdata: bc4dd8
userdata: b53330
dump.lua:7: attempt to index global 'this' (a userdata value)

I feel I am missing something simple, but I just can't get the next
step.  I feel like if I go to the direct way of binding like in the
wiki CppObjectBinding, I can get this to work, but then I have to do
all the bindings myself so I would like to stick with SWIG.

Thanks for any ideas!