1

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)

Ronald joe
  • 339
  • 2
  • 9
  • Looks to me that you are doing things in the wrong order. You assemble your gridNode first and then you try to add it to the document. You should load your document first, call `scene.append_child("Grid")` to add an empty grid node to the document, and only then do your loops to add nodes to the grid node. – john Jun 26 '20 at 11:58
  • @john but the thing is this function is being called from another function called saveScene to avoid code duplication this is why you see the boolean saveToSceneFile at the top because the saveScene create a doc save other things to it then call this function to open the same file that saveScene was saving to and save the grid this is why i can't call append child at the top because this function do 2 jobs, save the grid to a grid file and save the grid to a scene file – Ronald joe Jun 26 '20 at 12:01
  • I have no experience with this particular XML library, but from reading the documentation is appears that you cannot do things in the way you want to, new nodes must be added empty, or moved from other documents (or the same document). You cannot create nodes outside of any document. At least that how it appears to me. I'm sure you can devise some refactoring of your code that avoids the duplication but also does things in the order this API requires. But as I say, I don't know this library so I might be mistaken. – john Jun 26 '20 at 12:04
  • @john ok thanks for helping I will try to go around this somehow – Ronald joe Jun 26 '20 at 12:06

0 Answers0