Jump to content
  • Advertisement
Sign in to follow this  
mattmurdock

LuaBind polymorphism

This topic is 3075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys,
I've been having some problems using polymorphism with luabind, here's the situation: I have an abstract Control class representing a GUI element and 2 concrete subclasses, Dialog and Button. Control has a method attach(Control* child) for nesting controls within eachother. So you can add Buttons to a Dialog, say.
I export them to a LUA state with the following code:

// Export Controls
luabind::module(vm) [
luabind::class_<Control>("Control")
.def("attach", &Control::attach)
//..
];

luabind::module(vm) [
luabind::class_<Dialog, Control>("Dialog")
.def(luabind::constructor<>())
//..
];

luabind::module(vm) [
luabind::class_<Button, Control>("Button")
.def(luabind::constructor<std::string>())
//..
];


So far so good. The problem happens in the LUA script when I try to call Control::attach(Control*) with a derived type:

menu = Dialog()
newGameButton = Button("New Game")
exitButton = Button("Exit")
menu.attach(newGameButton)


This results in the error message
No matching overload found, candidates:
void attach(Control&,Control*)

Can anyone give an example of how to do this properly? I've done a lot of searching and can't find a whole lot of good info about luabind except for the official docs and the tutorial at http://www.nuclex.org/articles/cxx/1-quick-introduction-to-luabind so any help would be much appreciated.
Thanks, Matt

Share this post


Link to post
Share on other sites
Advertisement
One of the interesting bits of lua table syntax is how to call member functions.

The . operator is simple indexing, so if you had a function SomeTable.doStuff(self, arg) ... and did tableInstance.doStuff(1); that would call doStuff(1, nil). If the function expects self to be a table instance; you explode and die. The proper way to do calls like that is to explicitly pass the self parameter, so you'd have to do tableInstance.doStuff(tableInstance, 1).

Lua does, however, offer a shortcut with the : operator. So that when you do tableInstance:doStuff(1), it calls doStuff(tableInstance, 1).

Uh; anyway. The answer is to do menu:attach(...). Just make sure you're consistent with your operators.

Share this post


Link to post
Share on other sites
Thanks for the explanation, it's working. So the colon operator is a shortcut for passing a pointer to the instance as the first argument. Can't believe I missed that, I was using : in all the rest of the script
Cheers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!