I understand how to expose my own classes to lua, like this:
lua_State* L = luaL_newstate();
luaL_openlibs(L);
getGlobalNamespace(L)
.beginClass<Foo>("Foo")
.addConstructor<void(*)(void)>()
.addProperty(/*Property Definition*/)
.addFunction(/*Function Definition*/)
.endClass()
But, since I am trying to move as much of my code into lua scripts as possible, I would like to have lua classes that have SFML objects, like sf::Text
or sf::Texture
. I haven't done very much experimentation with Luabridge, and I'm not sure if I'd be able to do something like this:
lua_State* L = luaL_newstate();
luaL_openlibs(L);
getGlobalNamespace(L)
.beginClass<sf::Text>("Text")
.addConstructor<void(*)(void)>()
.addFunction("setCharacterSize", &sf::Text::setCharacterSize)
.addFunction("getCharacterSize", &sf::Text::getCharacterSize)
//ETC...
.endClass()
If doing this doesn't work (as I worry it may), would I need to create a wrapper class, like this:
class Text
{
private:
sf::Text textObj;
public:
void setCharacterSize(const int& size) {textObj.setCharacterSize(size);}
int& getCharacterSize() {return textObj.getCharacterSize();}
}
//Then do the same as the second snippet, without sf::Text but with Text class
UPDATE:
After attempting to expose the sf::Text
class, I get an error when trying to do the sf::Text::setPosition
function:
lua_State* L = luaL_newstate();
luaL_openlibs(L);
luabridge::getGlobalNamespace(L)
.beginClass<sf::Text>("Text")
.addConstructor<void(*)(void)>()
.addFunction("setCharacterSize", &sf::Text::setCharacterSize)
.addFunction("getCharacterSize", &sf::Text::getCharacterSize)
.addFunction("setColor", &sf::Text::setColor)
.addFunction("getColor", &sf::Text::getColor)
.addFunction("setFont", &sf::Text::setFont)
.addFunction("getFont", &sf::Text::getFont)
.addFunction("setPosition", &sf::Text::setPosition)
.addFunction("getPosition", &sf::Text::getPosition)
.addFunction("setScale", &sf::Text::setScale)
.addFunction("getScale", &sf::Text::getScale)
.addFunction("setString", &sf::Text::setString)
.addFunction("getString", &sf::Text::getString)
.endClass()
Error message:
no matching function for call to ‘luabridge::Namespace::Class<sf::Text>::addFunction(const char [12], <unresolved overloaded function type>)’
note: candidate is:
note: template<class MemFn> luabridge::Namespace::Class<T>& luabridge::Namespace::Class<T>::addFunction(const char*, MemFn) [with MemFn = MemFn; T = sf::Text]