Can we pass C++ object into Lua scrips -


i want pass c++ object lua , use same object in lua. have seen example of creating c++ objects in lua , use them. want below.

  • create c++ object in c++ code
  • pass lua scripts , return values.

i have gone through these examples here.

http://loadcode.blogspot.sg/2007/02/wrapping-c-classes-in-lua.html https://gist.github.com/kizzx2/1594905 

but creating c++ objects in lua , using them in lua scripts. can please give pointers.

given did read userdata manuals, easy achieve that. diference in regular ud manual create objects in lua function , have object. push new userdata of pointer size , set it's metatable lua class's metatable (which prepared anyway).

void **ud; ud = lua_newuserdata(l, sizeof(*ud)); *ud = object; // existing  lual_getmetatable(l, tname); lua_setmetatable(l, -2);  // ud @ top of stack  lua_setglobal(l, "foo"); 

where tname lua-side classes metatable name.


Comments