[luabind] How to create a Lua-defined subclass from C++
Brought to you by:
arvidn,
daniel_wallin
From: Glen F. <hol...@gm...> - 2013-07-22 15:43:24
|
Hi there, I've got subclassing working pretty well between Lua and C++. So I have a C++ base class, and can create subclasses in Lua and instantiate them there, add them to my "world", have them traversed and their methods called from C++, etc. What I don't understand how to do is how to create instances of Lua classes from C++. Is this possible? I have the name of the classes, because I call my own "register" method from Lua, so my app knows the names of available subclasses. But how can I create a new instance of one of them from C++? Lua: class 'Derived' (app.Base) function Derived:__init(str) app.Base.__init(self, str .. " (Derived)") end app.registerSubClass('Derived') Later on, in C++, I'd like to do the Lua equivalent of: x = Derived("hello") Then I can do: world = app.getWorld() world:addObject(x) All I can think of is something like: shared_ptr<Base> x = luabind::call_function<shared_ptr<Base> >(luaState, "createObject", "Derived", "hello"); world->addObject(x); // and so on... Any ideas? I can't just call new on my luabind::wrap_base - derived object, then register that with Lua/luabind somehow (and tell it what Lua subclass it should be using)? (that would be nice ;-) Thanks, Glen. |