1

I've been learning Luabind recently with the intention of integrating into our software. I've run into a couple of problems and i've been looking at the Rasterbar Software documentation of Luabind and have not been able to solve it. Basically, i'm exposing a function which takes a string and an abstract base class as parameters. Firstly, i'm not sure if i'm going about this the correct way or there maybe some special treatment required in lua for this to work. Anyway, here's the code

class UIFactory
{
    void addComponentFactory(std::string name, BaseFactory* factory);
}

BaseFactory is an abstract base class that returns a UIComponent (button, text etc) and we have derived factory called TemplateFactory which basically can be instantiated like so..

TemplateFactory<Button> buttonFactory = new TemplateFactory<Button>();

Then we would pass these to the UIFactory in c++ like this...

uiFactory.addComponentFactory("Buttons", buttonFactory);

in luabind...

module(state)
[
    class_<UIFactory>("UIFactory")
    .def(constructor<>())
    .def("AddFactory", &UIFactory::addComponentFactory)
];

and in lua...

uiFactory = UIFactory()

buttonFactory = ButtonFactory()

uiFactory:AddFactory("Button", buttonFactory)

That last line does not execute, i've checked that the button factory and ui factory were being created and they were. Is there anthing i'm missing?

Help is greatly appreciated.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
rocklobster
  • 609
  • 2
  • 10
  • 23
  • 1
    What do you mean by "does not execute"? You mean that the Lua script errors out, or that your function doesn't get called, or what? Have you verified (with say, `print` statements) that Lua is getting that far? – Nicol Bolas Apr 09 '12 at 16:24
  • I've verified that the previous statement "buttonFactory = ButtonFactory()" works so it must be working up to that point. I'm quite new to lua and luabind so i'm not really sure how to get the lua print function to work when not running it through the interpreter. – rocklobster Apr 09 '12 at 16:26

1 Answers1

1

Turns out I needed to expose the base class to lua and also indicate that buttonFactory was derived

luabind::module(state)
[
    luabind::class_<BaseFactory>("BaseFactory")
];

luabind::module(state)
[ 
    luabind::class<TemplateFactory<Button>, BaseFactory>("ButtonFactory")
    .def(constructor<>())
]
rocklobster
  • 609
  • 2
  • 10
  • 23