Writing my game in libgdx, I arrived at the point when I need to save the player stats and the info of the levels. However, in libgdx it's not allowed to write the file inside folder of the application, only external (on the SD) is allowed. The point is that I don't want the file to be seen by anyone, or if they can see it, how can I convert it to a binary file so it's not human readable? I just want to hide the file.
5 Answers
Libgdx has built in decoding and encoding functionality. Gustavo Steigert has written a nice article about this on his blog: http://steigert.blogspot.be/2012/03/5-libgdx-tutorial-files.html. Scroll down a bit to the section "2. Persist Profile operation".
In this blogpost, he explains that by using the class com.badlogic.gdx.utils.Base64Coder you can encode and decode files in the Base64-format. Leaving the unreadable and unmodifiable for end users.
-
5\$\begingroup\$ While better than plaintext base64 is visually recognisable and web based en/decoding tools are a search away. \$\endgroup\$ Apr 16, 2013 at 20:53
It sounds like using a "preference" is what you need to do. It allows you to save small bits of data (like a high score, or other settings) which persist when the game is shut down.
And it's a lot easier than trying to write to a file; you don't really have to concern yourself where the file is.
See: http://code.google.com/p/libgdx/wiki/Preferences
On Windows it gets put in the user's home directory. Android uses a SharedPreference system.
If you don't want users being able to read what's in there, you can always save your values in an encoded way. Ex:
prefs.putInteger("HighLevel", 15);
or
prefs.putString("SecretInfo", myEncodeFunction("here's the secret text to save."));
The following code will create a text file where you can store basic data
FileHandle file= Gdx.files.local("files/myfile.txt");
file.writeString("my_first_file", false);
to read from it
FileHandle file= Gdx.files.local("files/myfile.txt");
String text = file.readString();
now
if (text.equals("my_first_file")) {
//your code
}
if you'd like to check first whether not the file exits,
first, initialize :
boolean my_file_exits = Gdx.files.local("files/myfile.txt").exists();
then
if (exists) {
FileHandle file= Gdx.files.local("files/myfile.txt");
String text = file.readString();
if (text.equals("my_first_file")) {
//your code
}
} else {
//create a textfile and store default value
}
Again, try asking here..
http://www.badlogicgames.com/forum/
Basically, you may want to read up on how Android handles directories in your app. There is a file handle class in libgdx that should work for you. If you just want it saved in a protected place, use the internal process and it should be local to your app directory and not visible. It doesn't have to be on an sd card. Read the android docs and ask on the badlogic forum if you still need help and don't get any answers here
-
\$\begingroup\$ libgdx doesnt let you write files internally, i resolved it making a callback to a method in Android, because Android can write it. Thanks \$\endgroup\$– Rudy_TMFeb 17, 2012 at 15:16
-
1\$\begingroup\$ Read this badlogicgames.com/wordpress/?p=2305 I believe it could be what you need. \$\endgroup\$– arielsanMar 19, 2012 at 2:55
-
\$\begingroup\$ @Rudy_TM please add your comment as an answer and select it as the accepted answer so other people who read this later can clearly see it. \$\endgroup\$– ashes999Apr 2, 2012 at 10:57
I resolved it with callbacks, with that I can use Android methods, that way I can write to the internal memory.
-
6\$\begingroup\$ You should show what what callbacks you used. It'll be more useful for future visitors. \$\endgroup\$– HouseApr 2, 2012 at 19:54
-
1\$\begingroup\$ Does the Internal file type not solve your problem? code.google.com/p/libgdx/wiki/FileModule \$\endgroup\$– Chuck DApr 2, 2012 at 20:08
-
2\$\begingroup\$ You actually shouldn't use callbacks unless you don't want to use latest LibGDX... "local" files have been added as mentioned on blog to allow saving to private app directory \$\endgroup\$ Apr 3, 2012 at 0:14
-
\$\begingroup\$ I didnt know, i did the code like 2 months ago :/ i will change it :) thaks \$\endgroup\$– Rudy_TMApr 3, 2012 at 20:46
-
\$\begingroup\$ @RubberMallet no, that doesnt do the trick, because it only reads, it doesnt write, see the part of the blog that put XiaoChuan Yu, thats the new stuff for files (its of type local) :) \$\endgroup\$– Rudy_TMApr 3, 2012 at 20:49