5
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ Binary files are human readable too, it just requires a sufficiently skilled and motivated human. \$\endgroup\$ – Adam Sep 15 '13 at 0:11
9
\$\begingroup\$

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.

\$\endgroup\$
  • 5
    \$\begingroup\$ While better than plaintext base64 is visually recognisable and web based en/decoding tools are a search away. \$\endgroup\$ – Kelly Thomas Apr 16 '13 at 20:53
3
\$\begingroup\$

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."));
\$\endgroup\$
1
\$\begingroup\$

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
}
\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
  • \$\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_TM Feb 17 '12 at 15:16
  • 1
    \$\begingroup\$ Read this badlogicgames.com/wordpress/?p=2305 I believe it could be what you need. \$\endgroup\$ – arielsan Mar 19 '12 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\$ – ashes999 Apr 2 '12 at 10:57
-3
\$\begingroup\$

I resolved it with callbacks, with that I can use Android methods, that way I can write to the internal memory.

\$\endgroup\$
  • 5
    \$\begingroup\$ You should show what what callbacks you used. It'll be more useful for future visitors. \$\endgroup\$ – MichaelHouse Apr 2 '12 at 19:54
  • 1
    \$\begingroup\$ Does the Internal file type not solve your problem? code.google.com/p/libgdx/wiki/FileModule \$\endgroup\$ – Chuck D Apr 2 '12 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\$ – XiaoChuan Yu Apr 3 '12 at 0:14
  • \$\begingroup\$ I didnt know, i did the code like 2 months ago :/ i will change it :) thaks \$\endgroup\$ – Rudy_TM Apr 3 '12 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_TM Apr 3 '12 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.