cocos2d-lua binding c++vector<class/struct>

std::vector<StructName, std::allocator<StructName> > ret = cobj->getVectorStruct();
lua_newtable(tolua_S);
if (ret.empty())
	return 1;
int index = 1;
for (const auto& obj : ret)
{
	//构建数组
	//lua_pushnumber(tolua_S, (lua_Number)index);
	
	//构建类
	////object_to_luaval<ClassName>(tolua_S, "ClassName", (ClassName*)ret);
	//int ID = (int)(obj->_ID);
	//int* luaID = &(obj->_luaID);
	//toluafix_pushusertype_ccobject(tolua_S, ID, luaID, (void*)obj, "ClassName");
	//lua_rawset(tolua_S, -3);

	//构建数组
	lua_pushnumber(tolua_S, (lua_Number)index);

	//构建结构体
	lua_newtable(tolua_S);

	lua_pushstring(tolua_S, "key");
	lua_pushnumber(tolua_S, (lua_Number)obj.key);
	lua_rawset(tolua_S, -3);

	lua_pushstring(tolua_S, "key");
	lua_pushnumber(tolua_S, (lua_Number)obj.key);
	lua_rawset(tolua_S, -3);

	//数组结束
	lua_rawset(tolua_S, -3);

	++index;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324456179&siteId=291194637