lua-users home
lua-l archive

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


It would seem that for your purposes (accessing a C++ member variable as
if it were a lua table member) the last method you suggest would be the
best.

Can you elaborate a little more on the problem you are seeing with this
method?

Additionally, you should have a __newindex handler in your metatable to
handle writing to the C++ member var.  Search for "proxy" in the
archives for more info.

-Kevin

> 
> 
> I have a pretty noob question but I have several ways to 
> solve my problem and I think I just
> need a nudge in the right direction.
> 
> I found Virgil Smith's post here: 
> http://lua-users.org/lists/lua-l/2003-12/msg00483.html
> 
> That describes in detail what I am trying to do, but I must 
> be missing something.
> 
> Here is what I am trying to do ( pretty common ), I know I 
> can use all the binding libraries
> and add-ons out there, but I prefer to write my own.
> 
> Lets say I have a C++ class like the following:
> 
> class A
> {
>     public:
> 	 A() : iSomeVal(0){}
>         int iSomeVal;
> 	int doSomething(int i);
> }
> 
> And I want to be able to do this in lua:
> 
> A("myA");
> 
> myA.iSomeVal = 10;
> 
> local x = myA.iSomeVal;
> 
> myA:doSomething(x);
> 
> 
> I can get pieces of the above to work, but I am missing 
> something somewhere.
> 
> If I use a method table to register my object and methods, 
> something like this:
> 
>     lua_pushliteral(L, "__index");
>     lua_pushvalue(L, methodtable);
>     lua_settable(L, metatable);
> 
> Where methodtable is a table filled with cfunctions and 
> upvalues something like:
> 
> myA:doSomething(x)
> 
> works great, but I am at a loss at how to implement the 
> reading of member data
> because something like:
> 
> local x = myA.iSomeVal;
> 
> returns whatever is in the table field, so if I store it as a 
> fuction; x now holds a function
> reference, so I'm unsure how to implement it. Of course if I call:
> 
> local x = myA.iSomeVal();
> 
> It works, but is messy.
> 
> If I take the other route and do something like:
> 
>     lua_pushliteral(L, "__index");
>     lua_pushcfunction(L, &myGetter);
>     lua_settable(L, metatable);
> 
> And do my decision making about function or member in the 
> 'myGetter' function, it works
> well but lua throws an error after the execution of the 
> statement, if I call a function
> complaining of a nil value, even though it executes fine.
> 
> Any advice would be great...
> 
> Thanks,
> Scott
>