Sign in to follow this  
EmpireProductions

TinyXML Problems

Recommended Posts

Ok well I have TinyXML integrated into my game so my levels can be loaded from XML files. I am trying to just do a simple little test where get a Attribute and write it's value out into a text document. Everything compiles fine but when I go to run the game it freezes and comes up with a message saying it has to shut down. If some could take a look at my code and tell me what I am doing wrong that would be great! Code to load XML:
void GalaxyManager::loadGalaxy(const char* Galaxy)
{
   TiXmlDocument doc(Galaxy);
   doc.LoadFile();

   XMLRoot = doc.RootElement();
   TiXmlElement *pSolarSystem;
   pSolarSystem = doc.FirstChildElement("SolarSystem");
   TiXmlElement *pPlanet;
   pPlanet = doc.FirstChildElement("Planet");
   text = pPlanet->Attribute("id");


       myfile.open ("Logs/Error.txt");
      myfile << text;
      myfile.close();
   
   
}
XML file to load

<?xml version="1.0" encoding="utf-8"?>

<Galaxy>
  <SolarSystem>
    <Planet id="1" Type ="Lava">
      <Position x ="100" y ="0" z ="0" />
      <Seed ="200" />
      <entity name="Planet1" meshFile="Planet1" static="false" />
    </Planet>
    <Planet id="2" Type ="Rock">
      <Position x ="100" y ="0" z ="0" />
      <PlanetsSeed Seed ="1000" />
      <entity name="Planet2" meshFile="Planet2" static="false" />
    </Planet>
    <Planet id="3" Type ="Water">
      <Position x ="100" y ="0" z ="0" />
      <PlanetsSeed Seed ="300" />
      <entity name="Planet3" meshFile="Planet3" static="false" />
    </Planet>
    <Planet id="4" Type ="Moon">
      <Position x ="100" y ="0" z ="0" />
      <PlanetsSeed Seed ="600" />
      <entity name="Planet4" meshFile="Planet4" static="false" />
      <PlanetType  />
    </Planet>
    <Planet id="5" Type ="GasGiant">
      <Position x ="100" y ="0" z ="0" />
      <PlanetsSeed Seed ="50" />
      <entity name="Planet5" meshFile="Planet5" static="false" />
    </Planet>
    <Planet id="6" Type ="Ice">
      <Position x ="100" y ="0" z ="0" />
      <PlanetsSeed Seed ="20" />
      <entity name="Planet6" meshFile="Planet6" static="false" />
    </Planet>
  </SolarSystem>
</Galaxy>

Share this post


Link to post
Share on other sites
Quote:
Original post by EmpireProductions
Everything compiles fine but when I go to run the game it freezes and comes up with a message saying it has to shut down.

Sound like you need to learn how to use a debugger then. Place some breakpoints and step through your loading code and try and narrow down where it crashes.

I suspect you'll find that pSolarSystem and pPlanet are actually null. IIRC 'doc.RootElement' gets a kind of invisible super element, you'll have to call FirstChildElement("Galaxy") on that to actually retrieve the galaxy node.

Also FirstChildElement only searches through a nodes's immediate children, not recursively which is what your code seems to be expecting.

Share this post


Link to post
Share on other sites
Thanks. I know how to use the Debugger but I am using Ogre3D and for some reason it always complains that it can't find the needed dlls when I try to run through the debugger. I make sure the dlls are in the folder with the exe but it still complains about not being able to find them. I have giving up on trying to use the debugger with ogre.

Share this post


Link to post
Share on other sites
Ok well debug is NOT going to work. I got the debugger working but now instead of just freezing up the game it freezes up Visual Studio as well so I have to close both programs down. But I added the call on Galaxy like you said.

TiXmlElement *pGalaxy;
pGalaxy = XMLRoot->FirstChildElement("Galaxy");

I then removed everything else after that and it froze right up and comment that out and it works fine so the problem is with the very first call to FirstChildElement.

Share this post


Link to post
Share on other sites
I can offer no other suggestion other than you really need to resolve your debugger issues. You might be able to limp through this problem with copious use of printf but you can't seriously write a whole game without needing a debugger at some point. If it's an Ogre problem then perhaps search around their forums and see if it's come up before.

If you really, really can't get it working inside of Ogre then you could test your xml parsing in a separate console program (without Ogre) where you should be able to debug it and then move it into your proper project.

Share this post


Link to post
Share on other sites
Your XML isn't well-formed.

Quote:
<?xml version="1.0" encoding="utf-8"?>

<Galaxy>
<SolarSystem>
<Planet id="1" Type ="Lava">
<Position x ="100" y ="0" z ="0" />
<Seed ="200" />


You probably meant <PlanetsSeed Seed ="200" />.

The current XML would make doc.LoadFile return false and doc.RootElement return 0, giving a null pointer dereference on XMLRoot->FirstChildElement("Galaxy").

You should be checking these return codes/values, namely LoadFile's result and that RootElement and FirstChildElement aren't returning null pointers!

(As an aside, style-wise typically you don't have spaces between XML attribute names and values.)

Also, I just want to restate OrangyTang's comment. You really will need a functioning debugger at some point (again), so save yourself the pain and figure out why it's not working before you really need it. Try checking your debugger's path settings.

Share this post


Link to post
Share on other sites
I got the debugger working. But when I went to debug the game the game froze Visual studio as well as it self so I ended up not being able to use it because I had to end task it.

I changed the format of my XML file and got it mostly working. I am having a hard time figuring out how to convert from const char to int though so my seed values aren't able to be used yet. If some one could tell me then that would be great!

Share this post


Link to post
Share on other sites
With TinyXML you can call QueryIntAttribute on the element and it will convert the attribute to an int if it can. I think that is what you are asking. If not then disregard.

e.g.

int i;
pElement->QueryIntAttribute( "Seed", &i );

Share this post


Link to post
Share on other sites
Yup that is what I needed. It's working perfect now for the first planet but everything I try to do to get it to cycle to the next one doesn't work it just freezes up the game. Any one have any idea how to get it to cycle? I have tried using pElem3 = pElem2->NextSiblingElement("Planet"); and that doesn't work!

Share this post


Link to post
Share on other sites

TiXmlElement* pPlanet = doc.FirstChildElement( "SolarSystem" ).FirstChild().Element();

while ( pPlanet )
{
// do stuff with Planet node here

pPlanet = pPlanet->NextSiblingElement();
}




That is just off the top of my head, but something along those lines should work. Look at the tutorial on the TinyXML site.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this