logic behind adding child objects based on xml file

Mar 10, 2018 at 9:07pm
Hi
I'm writing a game and i want to load in the various Scenes and Objects from an xml file.

Heres an example of my game.xml file:
1
2
3
4
5
6
7
8
9
10
<game>
  <scene id="main_menu">
    <object id="play_button">
      <object id="child_of_play_button"></object>
    </object>

    <object id="something">
    </object>
  </scene>
</game>


As you can see there is one scene and 2 objects one of which has a child object inside of it.

In my code i already have the scene and object classes as well as the functionality to add child objects to an object by calling Object.AddChild()

The problem i have is that i cant figure out the logic behind creating the objects and child objects based on the xml file.


Heres the code that reads the xml file and initializes the scenes / objects:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool MyGameSystem::loadGame(std::string file)
{
   XMLError err = this->gameDoc->LoadFile(file.c_str());

   if(err != XML_SUCCESS)
   {
      DEBUG_LOG("ERROR LOADING GAME FILE:  %s", gameDoc->ErrorStr());
      return false;
   }

   //find game node
   XMLNode* gameNode = gameDoc->FirstChildElement("game");

   if(!gameNode)
   {
      DEBUG_LOG("ERROR NO GAME ELEMENT:  %s", gameDoc->ErrorStr());
      return false;
   }


   //Load nodes
   this->loadNode(gameNode);

   return true;
}

void MyGameSystem::loadNode(XMLNode* node)
{
   //handle node
   std::string type = std::string(node->ToElement()->Name());
   DEBUG_LOG("NODE: %s", type.c_str());

   if(type == "scene")
      this->AddScene(node->ToElement()->Attribute("id"));

   if(type == "object")
      this->AddObject(node->ToElement()->Attribute("id"));



   //Go through each sibling of this node
   XMLNode* child = node->FirstChild();

   while(child != nullptr)
   {
      //Load child nodes of this sibling
      this->loadNode(child);
    
      //Move on to the next sibling
      child = child->NextSibling();
   }
}


First I'm calling the "loadGame" from somewhere else in my code during initialisation.

loadGame then finds the "game" node and proceeds to call "loadNode" with its first childNode.

loadNode just goes through each of the nodes siblings one by one and loads them by callign itself with its first child node. (so i end up going through every single node)


Since I wont ever have scene nodes withing other scene nodes, i can just simply add a new scene by calling "AddScene" every time i come across a node of type "scene"

AddScene just calls SceneManager.addScene() and sets a member pointer (activeScene) to the newly added scenes address for later reference when adding objects.
this->activeScene = SceneManager.AddScene(id)

for now, i just call "AddObject" every time i see a node of type "object"

AddObject just adds an object to the activeScene by calling
this->activeScene->AddObject(id)


So right now there is no child objects. Only independant objects.

I thought about having a pointer (activeObject) to the last created object (just like activeScene) and then every time i come across a "object" node: calling activeObject->AddChild() in the AddObject function

but that will just result in every object becoming a child of the previous object..

Can somebody please help me with how i should go about this? :)
Last edited on Mar 10, 2018 at 9:23pm
Mar 11, 2018 at 3:52am
The point of XML is that you can organize data into a hierarchy. From the way you're describing it, it sounds like your data has no hierarchy. If that's the case then XML has no benefits over a custom format like
1
2
3
scene main_menu
object play_button /*list play_button properties*/
object something /*list play_button properties*/
Last edited on Mar 11, 2018 at 8:40am
Mar 11, 2018 at 8:12am
From the way you're describing it, it sounds like your data has no hierarchy.

Why don't you consider this hierachial if an object has children of type object.
Mar 11, 2018 at 8:40am
Ah, my bad. I misunderstood what OP was trying to say here:
So right now there is no child objects. Only independant objects.
Topic archived. No new replies allowed.