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

#1

Hi,

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.
thank


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

def->flush();

it works for me


#3

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.


#4

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


#5

If you update no.

If the user deletes the app, yes.


#6

Thank you all.


#7

What is equivalent of CCUserDefault in javascript?


#8

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


#9

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.


#10

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


#11

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.


#12

true,

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


#13

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.


#14

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";

FileUtils::getInstance()->getStringFromFile(file_path);

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


#15

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.

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

Write()
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

#16

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.


#17

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