Page 1 of 1

Loading text from XML file bug

Posted: Sun Feb 26, 2012 12:53 am
by darksmaster923
Alright, so I'm saving some attributes into an xml file, namely text, in order for me to load them and assign them to an object in my game. Saving them, everything goes alright, theres nothing wrong in the XML file. However, when I load the file and assign the text to the object that I want to name, the text is extremely bugged up. Heres an example:

http://oi41.tinypic.com/b46erc.jpg

The red outlined areas are the spots that the name should be placed, but instead its a bunch of characters that I can't read.

This is my loading code from the xml file

Code: Select all

void GameLoop::loadGame()
{
 
        //xml stored save file
        //read data
        io::IXMLReader *reader = graphics->getFileSystem()->createXMLReader("saves/save.xml");
        stringw currentSection;
        int num_ships;
        int num_cargo;
        while(reader->read())
        {
                switch(reader->getNodeType())
                {
                case io::EXN_ELEMENT:
                        {
                                //read from playerstats
                                if(core::stringw(L"playerStats").equals_ignore_case(reader->getNodeName()))
                                {
                                        CPlayer->loadObject(reader);
                                }
                                //load from cargo elemeent
                                //the extra code is because there are nested elements
                                if(currentSection==L"" && core::stringw(L"playerCargo").equals_ignore_case(reader->getNodeName()))
                                {
                                        currentSection=L"playerCargo";
                                        num_cargo = reader->getAttributeValueAsInt(0);
                                }
                                else if (currentSection.equals_ignore_case(L"playerCargo"))
                                {
                                        CPlayer->loadCargo(reader,num_cargo);
                                }
                                //read from shipstats element
                                if(currentSection==L"" && core::stringw(L"shipStats").equals_ignore_case(reader->getNodeName()))
                                {
                                        currentSection=L"shipStats";
                                        num_ships = reader->getAttributeValueAsInt(0);
                                }
                                else if (currentSection.equals_ignore_case(L"shipStats"))
                                {
                                        Manager->loadObjects(reader,num_ships);
                                }
                        }
                        break;
                case io::EXN_ELEMENT_END:
                        {
                                currentSection=L"";
                        }
                        break;
                }
        }
        
}
 
And this is from creating the objects and assigning them names derived from the xml file

Code: Select all

void gameManager::loadObjects(io::IXMLReader *reader, int numobjects)
{
        CShip *newship;
        //for num of ships inside of file
        for(u32 i = 0;i<numobjects;i++)
        {
                if(core::stringw(intToString(i)).equals_ignore_case(reader->getNodeName()))
                {
                        //create ship
                        //and set ship stats
 
                        ship_faction faction;
                        int t = reader->getAttributeValueAsInt(L"faction");
                        if(t==0)
                        {
                                faction = FACTION_TERRAN_FEDERATION;
                        }
                        if(t==1)
                        {
                                faction = FACTION_PROVIAN_CONSORTIUM;
                        }
                        if(t==2)
                        {
                                faction = FACTION_PIRATE;
                        }
                        if(t==3)
                        {
                                faction = FACTION_NEUTRAL;
                        }
                        ship_base *sclass;
                        int s = reader->getAttributeValueAsInt(L"ship");
                        for(unsigned int i=0; i<ships().ship_list.size();i++)
                        {
                                if(ships().ship_list[i]->index==s)
                                {
                                        sclass = ships().ship_list[i];
                                }
                        }
 
                        vector3df pos;
                        vector3df rot;
                        pos.X = reader->getAttributeValueAsFloat(L"posX");
                        pos.Y = reader->getAttributeValueAsFloat(L"posY");
                        pos.Z = reader->getAttributeValueAsFloat(L"posZ");
 
                        rot.X = reader->getAttributeValueAsFloat(L"rotX");
                        rot.Y = reader->getAttributeValueAsFloat(L"rotY");
                        rot.Z = reader->getAttributeValueAsFloat(L"rotZ");
 
                        const wchar_t *name = reader->getAttributeValue(L"name");
                        //why the hell does the name keep bugging up
                        newship = new CShip(graphics,sound,pos,rot,sclass,faction,name);
 
                        newship->setHullPoints(reader->getAttributeValueAsInt(L"hullPoints"));
                        newship->setShieldPoints(reader->getAttributeValueAsInt(L"shieldPoints"));
                        newship->setArmorPoints(reader->getAttributeValueAsInt(L"armorPoints"));
 
                        addShip(newship);
                        //newship->setVelocity(reader->getAttributeValueAsFloat(L"velocity"));
                }
        }
}
Can any of you guys help me out? I'm assuming I'm doing something wrong somewhere....

Re: Loading text from XML file bug

Posted: Sun Feb 26, 2012 1:04 am
by CuteAlien
Set a breakpoint at the place where you get the name. Check the content of name after that in the debugger. (In case you don't know yet much about debugging just try it - it's basically as simple as telling your IDE the line where it should stop (the line with "new CShip" for example would be a good point to set a breakpoint) and then you can display the values of all variables at that moment when the program is running).

Re: Loading text from XML file bug

Posted: Sun Feb 26, 2012 6:42 am
by darksmaster923
Hm, after placing a breakpoint at that point, the variable name seems absolutely fine. Could it be something else in the program that is causing this? I'm not running any functions that affects the name variables afterward though, and if I don't use the loaded text from the xml file, and replace it with some text that I assigned the variable to it works fine.

Re: Loading text from XML file bug

Posted: Sun Feb 26, 2012 10:19 am
by bitplane
In CShip's constructor are you actually copying "name" and storing it, or are you just using the pointer that's passed into the XML reader? At some point the XML reader is destroyed and that memory is freed up and overwritten, so if you're not copying it you're asking for trouble.

Re: Loading text from XML file bug

Posted: Sun Feb 26, 2012 11:53 pm
by darksmaster923
I am storing it

I would also like to add that during the loop inside of loadObjects, the text is fine, but once the reader is done reading, but before it is dropped, the text goes awry.

Re: Loading text from XML file bug

Posted: Mon Feb 27, 2012 10:33 am
by serengeor
darksmaster923 wrote:I am storing it

I would also like to add that during the loop inside of loadObjects, the text is fine, but once the reader is done reading, but before it is dropped, the text goes awry.
as he said, you must copy it over to your own buffer (probably could use irrlicht wide string class).

Re: Loading text from XML file bug

Posted: Mon Feb 27, 2012 10:12 pm
by darksmaster923
Sweet, I has done it. Thanks guys.