pugiXML append existing node into the document

503 Views Asked by At

this is my code:

void SaveHandler::saveGrid(TileGrid &grid, const char * Filename, bool saveToSceneFile)
{
  pugi::xml_node gridNode;

for (int L = 0; L < grid.m_tileLayers.size(); L++)
{
    for (int C = 0; C < grid.m_tileLayers[L].size(); C++)
    {
        for (int R = 0; R < grid.m_tileLayers[L][C].size(); R++)
        {
            std::string nodeName = "T" + std::to_string(L) + std::to_string(C) + std::to_string(R);

            if (grid.m_tileLayers[L][C][R].getisValid() == false) { break; }

            pugi::xml_node tileNode = gridNode.append_child(nodeName.c_str());
            tileNode.append_attribute("Name") = grid.m_tileLayers[L][C][R].getTextureName().c_str();
            tileNode.append_attribute("PosX") = C;
            tileNode.append_attribute("PosY") = R;
        }
    }
}
if (!saveToSceneFile)
{
    std::string savePath = std::string(GridsSaveLocation) + Filename + ".xml";
    pugi::xml_document doc;
    doc.append_child("Grid") = gridNode; //TODO append grid node into the file
    doc.save_file(savePath.c_str());
}
else
{
    std::string savePath = std::string(ScenesSaveLocation) + Filename + ".xml";
    pugi::xml_document doc;
    if(!doc.load_file(savePath.c_str()))Log("Grid Path not found", "SaveHandler.cpp", Type::Error);
    pugi::xml_node scene = doc.child("Scene");
    scene.append_child("Grid") = gridNode;
    doc.save_file(savePath.c_str());
}
}

I'm working on a big 2d game project for the first time and I'm trying to make a save grid function that save a grid of tiles inside a specific scene

what this does is basically create a node called gridNode and save every single tile in a grid as subNodes of it, but the problem here is that I can't add that node to the XMLdocument (please ignore the if statement the problem is at the else statement)

scene.append_child("Grid") = gridNode;

doesn't seem to work here, and I can't do

 gridNode = scene.append_child("Grid");

because this will empty the gridNode and then append it to the document

my question is how do I append an existing node to a document or to another node, I tried append_copy function but it didn't work either

(I apologies if my english is a bit broken I'm not a native english speaker)

0

There are 0 best solutions below