I am currently developing a system based on C ++ plugins that provides a Lua scripting interface for which I decided to use luabind. I use Lua 5 and luabind 0.9, statically linked and compiled with MSVC ++ 8. I now have problems with binding functions to luabind when they are defined in a derived class, but not in its parent class.
In particular, I have an abstract base class called "IPlugin", from which all plugin classes are inherited. When the plugin manager is initialized, it registers this class and its functions as follows:
luabind::open(L); luabind::module(L) [ luabind::class_<IPlugin>("IPlugin") .def("start", &IPlugin::start) ];
Once at runtime it is known which effective plugin classes are available, I had to decide to load the plugins in some workaround. The plugin manager provides a factory function for Lua, which takes the name of the plugin class and the name of the required object. Then the factory creates an object, registers the plugin class as inheriting from the IPlugin base class, and immediately calls a function on the created object, which registers itself as global with the Lua state, for example:
void PluginExample::registerLuaObject(lua_State *L, string a_name) { luabind::globals(L)[a_name] = (PluginExample*)this; }
I initially did this because I had problems with Lua defining the most derived class of the object, as if I registered it from the plugin manager, it is known only by the IPlugin subtype, not the specific subtype. I'm not sure if this is even necessary, but it works, and the created object is subsequently accessible from Lua under "a_name".
However, the problem is that functions defined in a derived class that were not declared at all in the parent class cannot be used. Virtual functions defined in the base class, such as the "start" above, work fine, and calling them from Lua to a new object launches the corresponding overridden code from the "PluginExample" class. But if I add a new function to "PluginExample", here, for example, is a function that takes no arguments and returns void, and register it as follows:
luabind::module(L) [ luabind::class_<PluginExample, IPlugin>("PluginExample") .def(luabind::constructor<PluginManager&>()) .def("func", &PluginExample::func) ];
calling the func function for the new object results in the following Lua runtime error:
No matching overload found, candidates:
void func (PluginExample &)
I use the syntax ':' correctly, so the argument "I" is not needed, and it seems that Lua can no longer determine the derived type of the object. I'm sure I'm doing something wrong, perhaps dealing with the two-step binding required by my system architecture, but I cannot figure out where. I would really appreciate help =)