Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Object Serialization For Saving Games On Android Platform  (Read 990 times)
0 Members and 1 Guest are viewing this topic.
Offline outlander78

Senior Newbie




Java games rock!


« Posted 2014-02-18 17:28:13 »

Hi folks.

Concise version:  Is loading and saving games using serialization an effective solution on Android?

Full version:

I have spent the last few years of my hobby time writing an RPG game in Java.  I now want to port it to Android, and have been reading up on Android development.

My lazy decision when writing the PC game was to implement save games using object serialization.  All of a game's data is under the object "Game", and is serializable, so saving and loading games was easy.  The risk is that a Java update will change the standard objects to the point that I cannot retrieve a saved game, and the player will have to start over.  Given the nature of the game (it's more rogue-like than anything else), this was not a problem.

Moving over to Android, I hope to preserve this approach rather than (finally) have to write toString and fromString methods, or figure out how to put all the data into SQLite.  I may use the DB later, but to get started, I am trying to avoid taking on more work - redoing the UI is enough.

So, my question is, in your collective experience, is loading and saving games using serialization an effective solution on Android?

thanks,
Andrew

Try my retro RPG computer game, oshs (Old School Hack and Slash), and let me know what you think.
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2014-02-18 19:01:09 »

Java serialization sucks. See Kryo.

Offline outlander78

Senior Newbie




Java games rock!


« Reply #2 - Posted 2014-02-18 19:38:56 »

Thanks for responding.  Could you elaborate on why Java serialization is bad?  Is it a question of performance, space consumption, reliability .... ?

Try my retro RPG computer game, oshs (Old School Hack and Slash), and let me know what you think.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ricardo

Senior Member


Medals: 2
Projects: 3



« Reply #3 - Posted 2014-02-18 20:28:32 »

Every time you update your code your saved files will become incompatible. That is why you should avoid serialization. (In my point of view)
For Android, check my answer here.
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #4 - Posted 2014-02-18 21:51:04 »

I rambled a bit here:
http://www.java-gaming.org/topics/serialization-by-various-means/27316/msg/244250/view.html#msg244250

Every time you update your code your saved files will become incompatible. That is why you should avoid serialization. (In my point of view)
Serialization is fine, Java's built-in serialization not so much. With Kryo's TaggedFieldSerializer you get backword compatibility -- you can add new fields without breaking old bytes. Instead of removing fields, you just @Deprecated them (and I also usually rename them to ignored1, ignored2, etc).

There are other ways to keep backward and forward compatibility, but they start to impact performance and I've found TaggedFieldSerializer is enough for games. When I make a change that is large enough that I can't evolve the class (which is rare), I write a serializer that transforms the old class to a new class. This consists of simply doing the old deserialization, then writing a method that populates the a new class instance with data from the old class instance.

For Android, check my answer here.
Saying the answer is shared preferences on Android is just as good as saying the answer is "write to a file" or "write to a database". This is only sufficient if you want to hand write all your serialization and handle compatibility yourself (hint: you don't Wink). If hand writing binary serialization, Kryo provides a lot of utility to reduce the effort, so I would still suggest it.

Offline outlander78

Senior Newbie




Java games rock!


« Reply #5 - Posted 2014-02-19 14:56:02 »

Thanks for the replies - they are very helpful.

Try my retro RPG computer game, oshs (Old School Hack and Slash), and let me know what you think.
Online Phibedy

Senior Member


Medals: 8



« Reply #6 - Posted 2014-02-19 15:14:16 »

I  prefer json to save data.
If you are using libgdx you could use their json-implementation.
Otherwise json-lib: http://json-lib.sourceforge.net/index.html
Offline Nate

JGO Kernel


Medals: 147
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2014-02-19 15:47:19 »

I also wrote the JSON stuff in libgdx. Smiley JSON is often fine, though not fast (so much string manipulation, especially string<->float) or small (floats again aren't efficient, eg I wouldn't use JSON for mesh data). It isn't the best for object serialization for a few reasons. If you have a JSON array, you can't embed type info, eg if your array is a LinkedList rather than an ArrayList. You'd have to wrap it in an object so you can annotate the concrete type to use for the array. Perhaps the biggest issue is JSON numbers are all floats, so you can have data loss with doubles or longs. libgdx handles this by treating JSON numbers as doubles and longs. libgdx's automatic JSON<->object serialization works for relatively basic classes (eg it doesn't allow using a subclass of the known class for collections because it doesn't wrap the JSON array with an object to annotate the concrete type, as explained above), while Kryo's works for all and is faster and much smaller. The downside is you can't read or hand edit Kryo output, though I have not found this to be necessary for most usage.

Xoppa added binary JSON to libgdx which helps in some ways, though writing field names still bloats the size.

Offline Gibbo3771
« Reply #8 - Posted 2014-02-19 16:20:32 »

I  prefer json to save data.
If you are using libgdx you could use their json-implementation.
Otherwise json-lib: http://json-lib.sourceforge.net/index.html

I've used this so far for saving small amounts of info (scores and what not) and it seems to be alright.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Ghidra

Senior Newbie


Medals: 3



« Reply #9 - Posted 2014-02-21 06:37:58 »

You could try JavaBeans Persistence, although almost nobody uses it.

The best link I could find was this:  http://my.safaribooksonline.com/book/programming/java/9780137144488/javabeans-components/ch08lev1sec9

On that note, I recommend that you check out the Core Java 2 books (Volume I and II) if you don't already have them.  Vol. II describes in detail how the Persistence mechanism works.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (28 views)
2014-09-21 02:42:18

BurntPizza (18 views)
2014-09-21 01:30:30

moogie (19 views)
2014-09-21 00:26:15

UprightPath (27 views)
2014-09-20 20:14:06

BurntPizza (29 views)
2014-09-19 03:14:18

Dwinin (45 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (100 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56

mitcheeb (71 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!