Loading text from XML file bug

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
darksmaster923
Posts: 51
Joined: Tue Jan 02, 2007 11:04 pm
Location: huntington beach

Loading text from XML file bug

Post 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....
Programmers are merely tools to convert caffeine into code.
CuteAlien
Admin
Posts: 9666
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Loading text from XML file bug

Post 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).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
darksmaster923
Posts: 51
Joined: Tue Jan 02, 2007 11:04 pm
Location: huntington beach

Re: Loading text from XML file bug

Post 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.
Programmers are merely tools to convert caffeine into code.
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Re: Loading text from XML file bug

Post 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.
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
darksmaster923
Posts: 51
Joined: Tue Jan 02, 2007 11:04 pm
Location: huntington beach

Re: Loading text from XML file bug

Post 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.
Programmers are merely tools to convert caffeine into code.
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Loading text from XML file bug

Post 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).
Working on game: Marrbles (Currently stopped).
darksmaster923
Posts: 51
Joined: Tue Jan 02, 2007 11:04 pm
Location: huntington beach

Re: Loading text from XML file bug

Post by darksmaster923 »

Sweet, I has done it. Thanks guys.
Programmers are merely tools to convert caffeine into code.
Post Reply