0
\$\begingroup\$

I want to save the state of the game in a file, so I can read it at the next start. The Problem is, that I get a exception, if I try to save something internal. If I try to save it local all work fine. I save the file as a Json.

Bird bird = new Bird();
bird.setName("Foo");
bird.setSkin(new Texture("data/foo.png"));
bird.save(Gdx.files.internal("bird.json"));

The save mthod:

Json json = new Json();
file.writeString(json.prettyPrint(this), false);

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Error writing file: bird.json (Internal) at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:349) at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:335) at com.buschmannmichael.fattybird.core.Bird.save(Bird.java:31) at com.buschmannmichael.fattybird.game.FattyBirdGame.create(FattyBirdGame.java:16) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:137) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:115) Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Cannot write to an internal file: bird.json at com.badlogic.gdx.files.FileHandle.writer(FileHandle.java:315) at com.badlogic.gdx.files.FileHandle.writeString(FileHandle.java:346) ... 5 more

Whats the problem of LibGDX, where I should save the files, so I can run the game on android, desktop and html ? (I tested this method I showed above only on desktop, maybe it works on android but I want a solution for all platforms)

Maybe someone can help me :)

\$\endgroup\$
6
\$\begingroup\$

Gdx.file.internal is read-only. You can use local file or preferences (creates an internal .xml file where you can store small amount of data). It is perfect for game saving.

Take a look at : https://github.com/libgdx/libgdx/wiki/Preferences

You can also use an external file (stored on sdcard), but you can optionally encrypt it, as it can be edited by the user.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Sometimes allowing something to be edited by the user isn't such a bad thing. \$\endgroup\$ – Vaughan Hilts Mar 28 '14 at 13:08
  • \$\begingroup\$ Yes, but I think you should create in-game features for the editable parts, because if you give him full privileges, he could completely manipulate the game process :) \$\endgroup\$ – Ion Farima Mar 28 '14 at 13:20
  • \$\begingroup\$ If this is a single player game, that's not an issue. They are only cheating themselves. If this is a multiplayer game and a local file can affect the state of the game for all other player, you have failed to design a secure game. \$\endgroup\$ – Vaughan Hilts Mar 28 '14 at 13:36
  • \$\begingroup\$ It's true ! In online multiplayer games I think you should store important files on a server, if the framework has this feature. Is it a good idea ? \$\endgroup\$ – Ion Farima Mar 28 '14 at 13:44
  • \$\begingroup\$ Yes is it, the local way is to unsafe. :D \$\endgroup\$ – user2933016 Mar 31 '14 at 16:10

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.