LuaBridge c++返回map给lua
之前在项目中需要返回一个std::map<std::string,std::string>给lua,但是发现直接返回map到lua中是不行的。后来通过别人的帮助终于解决了这个问题。方法如下:
1 luabridge::LuaRef LoginXml::getlocalAccount() 2 { 3 XMLElement* rootEle = pDoc->RootElement(); 4 XMLElement* accountEle = rootEle->FirstChildElement("localAccount"); 5 XMLElement* childEle = accountEle->FirstChildElement(); 6 while (childEle) 7 { 8 std::string strName = childEle->FirstChildElement("account")->GetText(); 9 std::string strPwd = childEle->FirstChildElement("password")->GetText(); 10 accountMap.insert(std::map<std::string,std::string>::value_type(strName, strPwd)); 11 childEle = childEle->NextSiblingElement(); 12 } 13 14 lua_State* luastate = GlobalScriptManager.get_lua_state(); //获取lua_State 15 16 luabridge::LuaRef uniforms(luastate, luabridge::newTable(luastate)); //返回值 一个table 17 18 std::map<std::string, std::string>::iterator iter; 19 for (iter = accountMap.begin(); iter != accountMap.end(); iter++) 20 { 21 std::string strName = iter->first; 22 std::string strPwd = iter->second; 23 uniforms[strName] = strPwd; //给uniforms插入值 24 } 25 26 return uniforms; 27 }
在lua中直接调用就行了。。。
loginUI.accountList = LoginXml:getlocalAccount() for k,value in pairs(loginUI.accountList) do local txt = UIforLua:createText(k,"微软雅黑",20) loginUI.listAccount:pushBackCustomItem(txt) end