1

I am making my first app in android, and am trying to deal with how to save changes the user makes to certain object variables in an interactive fiction game.

In the game, there are three main classes: Room, Item, and Player. Each class contains many attributes (i.e. booleans, strings, and lists) and these attributes can change throughout the course of the game as the player interacts with them, such as if a door is open/closed, or if an unlit lantern is used with a match, it becomes a lit lantern, with changed attributes. As of now, I've placed all the objects within the main activity class, and changed attributes simply by coding something like "shedDoor.isopen = false" within various methods. That's worked fine until now.

I ignored dealing with any kind of save function as I'm a beginner and assumed it would be simple to implement, but now that I've nearly completed the game I realize it's much more difficult to deal with than I thought.

So, my question is, "What is the best way to store my game data so that changes persist even after the app is killed?" I plan to have a load and save button for the user to bring them back to their place in the game, as well as a new game button if they want to start over from the beginning. I've looked at sharedPreferences, but I'm not sure that would work with multiple classes of different data types.

My intuition leads me to believe that making an SQLite database would be a way to go, but I would have to overhaul a lot of my coding to implement that and wanted to get some more expert advice before starting such a big undertaking.

As always, thanks for helping a beginner.

Dr. Fishopolis
  • 79
  • 1
  • 10

3 Answers3

0

i dont know much about android but i should be almost similar to desktop java so here are some methods to save your data (in java):

save them with writing your data in a string to some file:

BufferedWriter writer = new BufferedWriter(new FileWrite(new File(path));

writer.write("string you want to write");

save object with seralizing your data to some file (more efficient):

ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("save.dat"));
os.writeObject(objectyouwanttostore)

EDIT 1:

Btw i would suggest to use a engine like libgdx if you just want to develop an app for android because it simplifies a lot just take a look at their homepage

BlakkM9
  • 400
  • 4
  • 17
0

For complex data, you should use a flat file with your own format or a SQLite database. Either way, you should judiciously load into memory the data which is needed at any given time rather than loading everything at once. Even though mobile devices are getting more powerful every year, they are still quite limited in the resources that are available.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
-1

You can use SharedPreferences for storing your game state data. To handle different data types, you can serialize your game data object with gson then save it as string to your SharedPreferences. You only need to make your game state data object implement serializable.

Read more about about gson at google-gson.

Here the step for using gson (taken from https://stackoverflow.com/a/38089938/4758255) you can add GSON dependency in Gradle file with:

compile 'com.google.code.gson:gson:2.7'

Here the snippet:

First, create your usual sharedPreferences:

//Creating a shared preference
SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);

Saving from serializable object to preference:

 Editor prefsEditor = mPrefs.edit();
 Gson gson = new Gson();
 String json = gson.toJson(YourSerializableObject);
 prefsEditor.putString("SerializableObject", json);
 prefsEditor.commit();

Get serializable object from preference:

Gson gson = new Gson();
String json = mPrefs.getString("SerializableObject", "");
yourSerializableObject = gson.fromJson(json, YourSerializableObject.class);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
  • SharedPreferences is intended for simple data which can be represented as key/value pairs. – Code-Apprentice Feb 17 '18 at 03:27
  • No, you're slightly mistaken. `The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).` – ישו אוהב אותך Feb 17 '18 at 03:52
  • "primitive data" is what I mean by "simple data". Storing complex data types like lists and objects is near impossible with SharedPreferences. – Code-Apprentice Feb 17 '18 at 03:54
  • `"primitive data" is what I mean by "simple data".` this is still not quite right. SharedPreferences assume that String is a primitive data which isn't true. using SharedPreference assumption that string is a primitive, then a complex json string is primitive data too. Which is not true. I agree with your last statement. Storing a complex data types into SharedPreferences isn't a good decision, though is not impossible. – ישו אוהב אותך Feb 17 '18 at 04:22
  • According to the quote you gave, strings are considered as "primitive data". Clearly the author does not use this in the technical sense. Regardless, my main point still stands. SharedPreferences are not meant for the complex data which the OP is asking about. @ישואוהבאותך – Code-Apprentice Feb 17 '18 at 04:24