[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: SWIG/Lua/C++ object instance
- From: "mark gossage " <mark@...>
- Date: Sun, 06 May 2007 20:26:23 -0600
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();
SWIG_NewPointerObj(L,p,SWIGTYPE_p_Foo,1);
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:
print(p)
print(swig_type(p))
p:some_function()
Let me know if you have any other questions.
Regards,
Mark
> 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
> SWIG.
>
> 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!
>
> Joey