我了解如何将我自己的类公开给 lua,如下所示:
lua_State* L = luaL_newstate();
luaL_openlibs(L);
getGlobalNamespace(L)
.beginClass<Foo>("Foo")
.addConstructor<void(*)(void)>()
.addProperty(/*Property Definition*/)
.addFunction(/*Function Definition*/)
.endClass()
但是,由于我试图将尽可能多的代码移动到 lua 脚本中,所以我希望 lua 类具有 SFML 对象,例如sf::Text
or sf::Texture
。我没有对 Luabridge 做过很多实验,我不确定我是否能够做这样的事情:
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()
如果这样做不起作用(我担心它可能),我是否需要创建一个包装类,如下所示:
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
更新:
尝试公开sf::Text
该类后,尝试执行该sf::Text::setPosition
功能时出现错误:
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()
错误信息:
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]