Cocos2d-x save data with UserDefault always lost when game update?

Cocos2d-x save data with UserDefault always lost when game update?
0.0 0



Can any one tell me how to save game data with a good way without lost data when game update or user clear/delete data in android app setting?. I saw in cocos2d-x has UserDefault but when my game update to new version my old data like score, diamonds alway lost.

Anyone help.

UserDefault* def = UserDefault::getInstance();
highScore = def->getIntegerForKey("xxx", 0);
if (score > highScore) {
    highScore = score;
    def->setIntegerForKey("xxx", highScore);


it works for me


I know it worked but the thing that I care the most is about game update. example if i release first version on google play and I want give old user update to next version then all data for those old user will lost. or UserDefault also work with that situation too.

some more idea?

thank adesuluh for reply.


No it will not lost. For both Android & iOS
You can use it without any hesitate.


If you update no.

If the user deletes the app, yes.


Thank you all.


What is equivalent of CCUserDefault in javascript?


is UserData safe?, or can this data be easily edited/“hacked”


If the device is rooted/jailbreaked the data is visible, because it’s not encrypted. Without root/jailbreak the data is only “app visible”, so no other app can access it.


Does cocos provide a way of saving data, which even exist after deleting the game?


Every data which should stay after game delete has to be world readable (at least on Android). So every app can read/edit it. Do you really want that?

The best way to save data even after a game delete, is to use server side savegames and on your device you just save a UUID (key on sever) in the UserDefaults (which are backed up by the system and reinstalled even on a new device).

This is the way I would implement it.



what about data in the resourcefolder, I’ve seen its possible to Create data in the resource Folder and read/write from it, but not sure how data from the resoruce folder is handled? is everything there transformed to binary into the apk?

this would be interesting for Level data


Everything in resource will be unaltered in the assets folder of the APK. But this folder ist readonly at runtime, because it’s never unpacked elsewhere.


yeah thats perfectly fine, but it Fails to read from the file, for example:
where Levels is in the Resource folder

auto file_path = "Levels/level1.txt";


it always says the file does not exist.
Not sure what the correct path for this would be: Resource/Levels/level1.txt


kinda solved this myself, Cocos will always try to load from the resource file in the apk/release Folder, and writing only happens to the Project resource Folder.
In my case i want to be able to modify Levels in the game and save them to the resource Folder for PC Debug only (kinda like a Custom Editor).

The solution to this was just to Create a save function that ready from the full Project Resource path (just enter the full path to the prohect reosurce Folder, so it doesnt try to read from the release resource Folder) but for saving use the normal path (since saving will write to the Project resource path anyway)

And for the reading function just use the normal relative path, which will be the release resource Folder.

const std::string content = FileUtils::getInstance()->getStringFromFile(file_path); //reads from release Resource

const std::string file_path =  "E:/Projects/myproject/myproject/Resources/" + target_file; //read first
auto content = FileUtils::getInstance()->getStringFromFile(file_path);
//find Something in the fild and replace
FileUtils::getInstance()->writeStringToFile(content , target_file);; //write the Changes to file


I would prefer to always use lower case file-/folder-names. And if it’s Resource/level/level1.txt while compile time, you just use level/level1.txt to reference it, because there is no Resource folder on Android.

I would be confused if this will be working in the release build, because you can’t change any files on runtime in the assets store of the APK.

If you use it only on the PC, it’s a different story and was not your question at first.


not sure maybe i wrote it a Little worng:

the Write function is only used locally on debug, to build Levels and saved to the resource Folder. Only reading the files is used for the release