How to create a Lua-defined subclass from C++

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to create a Lua-defined subclass from C++

Glen F
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.


------------------------------------------------------------------------------
See everything from the browser to the database with AppDynamics
Get end-to-end visibility with application monitoring from AppDynamics
Isolate bottlenecks and diagnose root cause in seconds.
Start your free trial of AppDynamics Pro today!
http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user