Question

Recently I've ran into a big problem with pugiXml (used within cocos2d-x engine).

Shortly, I've made a quiz game (in mentioned Cocos2d-x). I keep my questions (and some other data) in an Xml file. On new game they get parsed and inserted into dictionaries. If a question is answered, a short string indicating the answer (whether it was good or not - Y/N) is inserted into that Xml file (below that particular question). Later, I use this data to show statistics (I count the percentage of questions with good answers - which is counting the amount of Y's divided by the amount of questions, multiplied by 100).

I use:

CCFileUtils::sharedFileUtils()->fullPathForFilename(o_QA);

to get the file and later

pBuffer = CCFileUtils::sharedFileUtils()->getFileData(fullPath.c_str(), "rb", &bufferSize);

to put file into buffer and

pugi::xml_parse_result result = doc.load_buffer(pBuffer,bufferSize);

to parse data and start working on it.

Finally, I save the file with:

doc.save_file(fullPath.c_str());

Android:

  • It's working very well, although I had to copy the file with questions to /data/data/app/Files/ . Still, It's keeping the changes on many devices.

iOS:

  • Unfortunately, on iOS it's not working. Data get loaded and parsed (which means, that I can actually play the game), but they are not saved. I've tried moving the files to other folders (started from Resources/Documents, then main Resources folder, Resources/Library/Application Support). It's still not saving the data and I don't know what to do. The result is my statistics not being counted well (it doesn't matter how you answer the questions - all of them are false, because the Xml file is not being updated).

Did anyone run into similar problem? Can you, please, help me?

Was it helpful?

Solution

I am doing something similar in my own app, using both pugixml and cocos2d-x. So I can confirm this combination works well.

Rather, because on iOS you cannot both read and write the data from and to the app bundle (which is read only), you will need to implement a simple check in the writable document directory - If you a saved file there, load it, if not, load from the app bundle.

So in essence for loading, if your saved filename is "my_save.xml", here is an example flow:

1) Construct a path for your save file in the writable folder by concatenating the writable folder path + your filename. CCFileUtils should have something like getWritablePath() for that.

2) if the file exists in the folder, load it. Otherwise, go to 3).

3) Construct a path to your original data file from the app bundle, using CCFileUtils::sharedFileUtils()->fullPathForFilename(). Load the file from there.

For saving, simply do step 1 and save the file there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top